views:

294

answers:

1

Hi Everyone,

I am currently developing my first Android application by reading Dev Documentation at Android official website. What I am trying to accomplish is to play some ring sounds. A section from my code is:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class PlayRingSounds extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
}

public void PlayRingFile(View view) {       
  switch (view.getId()) {
    case R.id.Button01:
      MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
      mp1.start();
      break;
    case R.id.Button02:
      MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
      mp2.start();
      break;        
  }
}   
}

The problem is when I click the 2nd button while "aaa" (sound file from 1st button) is playing, "bbb" also starts playing at the same time. Is there a way to stop "aaa" before "bbb" plays, or is there a way to stop all media players?

Update 12/30/2009 - New code is:

   case R.id.Button01:
       if (mp2.isPlaying())
       {
           mp2.stop();
           mp2.release();
       }
       mp1.reset();
       mp1.prepare();
       mp1.start();
       break;
   case R.id.Button02:
       if (mp1.isPlaying())
       {
           mp1.stop();
           mp1.release();
       }
       mp2.reset();
       mp2.prepare();
       mp2.start();
       break;

mp1.prepare() and mp2.prepare() give IOException error.

+1  A: 

Please note: This is not the best way to do this, this is very sloppy, this is just an idea

public void PlayRingFile(View view) {       
  switch (view.getId()) {
   case R.id.Button01:
    if (mp2.isPlaying()) {
       mp2.stop();    // stops the object from playing
       mp2.release(); // always a good practice to release the resource when done
     }
    MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
    mp1.start();
    break;
   case R.id.Button02:
    if (mp1.isPlaying()) {
       mp1.stop();    // stops the object from playing
       mp1.release(); // always a good practice to release the resource
     }
    MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
    mp2.start();
    break;        
 }
}

As I said, this isn't the best solution especially if you add more buttons then you would have to check every instance of MediaPlayer and there must be better ways of doing this.

My suggestions are to try to find a way to loop through all MediaPlayer's to see if they are open and if so, release the resource and stop playing or maybe a way to stop all MediaPlayer's from playing in general?

I will continue to look for other ways in the meantime, hope this points you in the right direction.

EDIT (12/30/09):

case R.id.Button01:
   if (mp2.isPlaying())  {
       mp2.stop();
       mp2.release();
   }
   mp1.reset();
   createMPlayer1(); // used to re-initialze the mediaplayer for reuse since resources were released.
   mp1.prepare();
   mp1.start();
   break;
case R.id.Button02:
   if (mp1.isPlaying()) {
       mp1.stop();
       mp1.release();
   }
   mp2.reset();
   createMPlayer2();
   mp2.prepare();
   mp2.start();
   break;

public void createMPlayer1() {
   MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
}

public void createMPlayer2() {
   MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
}

I think the IOException could be called when trying to access the file again after we have renoved the resouce. I added two methods to create the separate raw files since I believe the exception occurs when the resources are released. You can either re-initialized the MediaPlayer's or try to discard releasing the resource.

Anthony Forloney
I have not tried it yet, but I guess isPlaying is the key. And you're right about implementing a loop. Thank you for your reply.
ncakmak
No problem, I hope it all goes well.
Anthony Forloney
I tried your code and it worked fine. But once mp2 is stopped it is not replaying. I modified the code so that I added mp.reset() and mp.prepare(), but this time mp1.prepare() and mp2.prepare() gave Unhandled exception type IOException error. Do you have any idea? Thank you for your time.
ncakmak
can you post your code as an edit in your question, so I can browse as whats happening?
Anthony Forloney
Hi there. I added the new code to the question. It is the prepare function that gives the error in both mediaplayers. Thanks again.
ncakmak
This time setDataSource method is giving an error saying that: "The method setDataSource(Context, Uri) in the type MediaPlayer is not acceptable for the arguments (Context, int)"
ncakmak
I posted my edit too fast, I didn't get a chance to read the actual description of `setDataSource` where its used for playing a file or a stream, whereas your files as a raw resource. I am wondering you might have to create a function to (re)intialize the MediaPlayer's and then prepare them the same way you would normally and then play. If you need me to explain further I can jot down some code when I get home
Anthony Forloney
I am new to Java. I will definitely appreciate if you could write a sample code.
ncakmak
Thank you for your help and time.
ncakmak
out of curiosity, how did everything make out?
Anthony Forloney
I decided to go with the issue, so when 2nd button is clicked the sound from the 1st button still plays. But, I have learned so much from our conversation. Thanks again.
ncakmak