Waqas Ahmed
Waqas Ahmed

Reputation: 75

Background audio for one specific activity

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code

public class exp extends Activity {
    ImageView imview1,imview2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addexample);

        imview2 = (ImageView) findViewById(R.id.imageView2);

        imview2.setVisibility(View.INVISIBLE);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                imview2.setVisibility(View.VISIBLE);

            }
        }, 2000);
    }
}

Upvotes: 0

Views: 106

Answers (3)

Sher Alam
Sher Alam

Reputation: 372

public class exp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addexample);

    //player starting code goes here.
 }

 protected void onPause() {
    //stop the music player here.
}
}

Upvotes: 1

Sreehari
Sreehari

Reputation: 5655

In onResume start your audio

@Override
    protected void onResume() {
        super.onResume();
        //start your audio track here
    }

Stop the same in onPause

@Override
    protected void onPause() {
        //Sop it over here
        super.onPause();
    }

Create the player instances in onCreate

Upvotes: 1

Alvaro Royo
Alvaro Royo

Reputation: 156

Try to stop your audio in onPause:

public class exp extends Activity {
ImageView imview1,imview2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addexample);

    imview2 = (ImageView) findViewById(R.id.imageView2);

    imview2.setVisibility(View.INVISIBLE);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            imview2.setVisibility(View.VISIBLE);

        }
    }, 2000);
}

@Override
public void onPause(){
    super.onPause();
    //Kill your audio
}

}

I think your solution is something like that. Try it and comment

Good look!

Upvotes: 0

Related Questions