I have several MediaPlayers in the same activity. It works this way:
MediaPlayer mpOne starts playing, if a variable has a value "yes" MediaPlayer mptwo starts playing, else mpThree starts.
When the audio ends, starts another process identical
This is my code:
MediaPlayer mpOne,mpTwo,mpThree,mpFour,mpFive
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morning);
mpOne= MediaPlayer.create(this, R.raw.one);
mphora.setLooping(false);
mpTwo= MediaPlayer.create(this, R.raw.two);
mphora.setLooping(false);
mpThree= MediaPlayer.create(this, R.raw.three);
mphora.setLooping(false);
mpFour= MediaPlayer.create(this, R.raw.four);
mphora.setLooping(false);
mpFive= MediaPlayer.create(this, R.raw.five);
mphora.setLooping(false);
mpOne.start();
mpOne.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
if (variable.equals("yes"){
mpTwo.start();
} else {
mpThree.start();
}
});
mpTwo.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpThree.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpFour.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
// same proccess
});
}
My problem is when I want to stop all audios.
Currently I have a button that does the following:
stop.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(mpOne.isPlaying()){mpOne.stop();mpOne.release();}
else if(mpTwo.isPlaying()){mpTwo.stop();mpTwo.release();}
else if(mpThree.isPlaying()){mpThree.stop();mpThree.release();}
else if(mpFour.isPlaying()){mpFour.stop();mpFour.release();}
else if(mpFive.isPlaying()){mpFive.stop();mpFive.release();}
}
}
});
The problem is that sometimes it works and sometimes the audio still playing.
I've searched and found similar problems, but not a solution
Can anyone tell me a safe way to stop all playback?
Thank you very much in advance.
regards
You can do only one MediaPlayer instance and at onCompletion setting a different audio source
Related
I am having a listview in my app. Each listview item has button which will play an audio from a url received from web service. But my problem is that if I click play button from the next item then both start playing together. I am having problem in this. I want only one to play at a time. Right now I am creating new Media player object everytime button is clicked, but I also tried creating a single global object but in this case it only plays first time and not after it. What is the possible solution of it.
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Make the mp variable global and remove this:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
because if you take a look at this reference http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram it says:
Once the MediaPlayer object is in the End state, it can no longer be
used and there is no way to bring it back to any other state.
And when you call mp.release(); the media player WILL go to that state.
Then make your onClickListener look something like this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
//... and so on
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
The trick there is to make it back to Idle state so you can set the new data source and start playing again. It's all about the states...
For example, I want to play 3 songs. When the 1st song ends, the 2nd song begins, and when the 2nd song ends, the 3rd song begins, and when the 3rd song ends, the 1st song begins again and so on. Is the mp.setOnCompletionListener used here?
You are right. You can do something simple like this:
public class MainActivity extends Activity {
MediaPlayer mp1, mp2, mp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp1 = MediaPlayer.create(this, R.raw.music_1);
mp2 = MediaPlayer.create(this, R.raw.music_2);
mp3 = MediaPlayer.create(this, R.raw.music_3);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});
}
}
This will play mp1 and onCompletion of it, it will play mp2, and onCompletion of mp2, it will play mp3, and onCompletion of mp3, it will play mp1 again and so on...
First declare the 3 MediaPlayer files:
MediaPlayer song1;
MediaPlayer song2;
MediaPlayer song3;
Then initialize the MediaPlayer objects:
song1 = MediaPlayer.create(this, R.raw.exampleSong1);
song2 = MediaPlayer.create(this, R.raw.exampleSong2);
song3 = MediaPlayer.create(this, R.raw.exampleSong3);
Now start the first Media file with
song1.start();
Now with every instance created you should add to every MediaPlayer object a setOnCompletionListener like this:
song1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song2.start();
}
});
Do the same for the Second and Third MediaPlayer files:
song2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song3.start();
}
});
song3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song1.start();
}
});
First I am using soundpool But with soundpool its difficult to do this task. So I am now using media player. In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish so that user able to play sound again when its finish.
Code-
private LinearLayout layout1;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
});
}
private LinearLayout layout1;
MediaPlayer mp;
boolean flag=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(!flag){
mp.start();
layout1.setEnabled(false);
} else{flag =true ;}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
flag=false;
layout1.setEnabled(true);
}
}); }
EDIT
You can do like this in onClickListener:
boolean flag;
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if (flag == false){
flage=true;
layout1.setEnabled(false);
mp.start();
}
});
The value of flag is dependant on state of sound file.
The callback public void onCompletion(MediaPlayer mp) gives you a reference to the MediaPlayer.
public void onCompletion(MediaPlayer mp) {
flag = false;
layout1.setEnabled(true);
}
i'm newbie in this world of programming, i want reproduce one sound at time, is a application with long sounds of animals.
when i push some button make sound about some animals (gato make sound of a cat)
i fail to play a sound at time, i try with soundpool too, same results, obviously the problem are my low lvl of programming xD, can somebody help me?
public class MainActivity extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void presionGato(View v) {
mp.stop();
mp.create(this, R.raw.gato);
mp.start();
}
public void presionLeon(View v) {
mp.stop();
mp.create(this, R.raw.leon);
mp.start();
}
public void presionPerro(View v) {
mp.stop();
mp = mp.create (this, R.raw.perro);
mp.start();
}
public void presionTigre(View v) {
mp = mp.create (this, R.raw.tigre);
mp.start();
}
public void presionRata(View v) {
mp = mp.create (this, R.raw.rata);
mp.start();
}
public void presionSapo(View v) {
mp = mp.create (this, R.raw.sapo);
mp.start();
}
public void presionRana(View v) {
mp = mp.create (this, R.raw.rana);
mp.start();
}
}
I suggest to you, learn programming and search about it. You didn't listen anything from UI, how can you know which button was clicked?
first, initialize UI element and media player on onCreate;
Button gatoButton = (Button) findViewById(R.id.gato);
Button leonButton = (Button) findViewById(R.id.leon);
mp = new MediaPlayer();
then set onClickListener for each button.
gatoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionGato();
}});
leonButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionLeon();
}});
You can use
if(mp.isPlaying()){
mp.stop();
mp.release();
}
it will stop the music if it is playing then you can start the your new music again. It prevent to play the music at the same time.
guys scenario is that suppose i click a button, the sound plays and within the duration of that track i again click the button and want to play it from the beginning. i tried with the following code, but no success.
code is :
public class SoundtestActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.gun_shot);
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
mp.reset();
mp.start();
}
}));
}
}
Try this code
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp.start();
}
});
Or replace mp.start(); by mp.reset(); in this code
Best way and the easiest way is =
mp.setLooping(true);
And yes it does work, just simply add this line after mp.start() and nothinfg else is required
You just need to comment the following lines and shift your Media player instance in the button's listener method, bingo your done with multiple media players playing parallel to each other
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(this, R.raw.gun_shot); // ADD THIS LINE HERE
if(mp.isPlaying()){
mp.stop(); //ADDED TO STOP FIRST
mp.release(); //ADDED TO RELEASE RESOURCES
}
mp.start();
}
}));