English 中文(简体)
Is there a way to stop all MediaPlayers?
原标题:

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.

最佳回答

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.

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签