3

我目前正在通过阅读 Android 官方网站上的 Dev Documentation 来开发我的第一个 Android 应用程序。我想要完成的是播放一些铃声。我的代码中的一部分是:

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;        
  }
}   
}

问题是当我在播放“aaa”(来自第一个按钮的声音文件)时单击第二个按钮时,“bbb”也同时开始播放。有没有办法在“bbb”播放之前停止“aaa”,或者有没有办法停止所有媒体播放器?

2009 年 12 月 30 日更新 - 新代码是:

   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() 和 mp2.prepare() 给出 IOException 错误。

4

1 回答 1

1

请注意:这不是最好的方法,这很草率,这只是一个想法

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.

于 2009-12-28T15:40:35.293 回答