有没有办法停止所有MediaPlayers? [英] Is there a way to stop all MediaPlayers?

查看:157
本文介绍了有没有办法停止所有MediaPlayers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过在Android的官方网站上阅读文档开发开发我的第一个Android应用程序。我试图做到的是玩一些环声音。从我的code段是:

 进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.View;公共类PlayRingSounds延伸活动{@覆盖
公共无效的onCreate(捆绑savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.main);
}公共无效PlayRingFile(查看视图){
  开关(view.getId()){
    案例R.id.Button01:
      MediaPlayer的MP1 = MediaPlayer.create(this.getApplicationContext(),R.raw.aaa);
      mp1.start();
      打破;
    案例R.id.Button02:
      MediaPlayer的MP2 = MediaPlayer.create(this.getApplicationContext(),R.raw.bbb);
      mp2.start();
      打破;
  }
}
}

问题是,当我点击第二个按钮,AAA(从第一个按钮声音文件)正在播放时,BBB也开始在同一时间播放。有没有办法BBB之前停止AAA剧,还是有办法阻止所有的媒体播放器?

更新12/30/2009 - 新的code是:

 情况下R.id.Button01:
       如果(mp2.isPlaying())
       {
           mp2.stop();
           mp2.release();
       }
       mp1.reset();
       。MP1 prepare();
       mp1.start();
       打破;
   案例R.id.Button02:
       如果(mp1.isPlaying())
       {
           mp1.stop();
           mp1.release();
       }
       mp2.reset();
       。MP2 prepare();
       mp2.start();
       打破;

MP1。prepare()和MP2。prepare()给IOException异常错误。


解决方案

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

 公共无效PlayRingFile(查看视图){
  开关(view.getId()){
   案例R.id.Button01:
    如果(mp2.isPlaying()){
       mp2.stop(); //停止正在播放的对象
       mp2.release(); //经常做的时候一个很好的做法释放资源
     }
    MediaPlayer的MP1 = MediaPlayer.create(this.getApplicationContext(),R.raw.aaa);
    mp1.start();
    打破;
   案例R.id.Button02:
    如果(mp1.isPlaying()){
       mp1.stop(); //停止正在播放的对象
       mp1.release(); //总是一种好习惯,以释放资源
     }
    MediaPlayer的MP2 = MediaPlayer.create(this.getApplicationContext(),R.raw.bbb);
    mp2.start();
    打破;
 }
}

正如我所说的,这是不是最好的解决方案,特别是如果你添加更多的按钮,那么你就必须检查的MediaPlayer 的每个实例,并且必须有更好的方法这样做。

我的建议是尝试通过所有的MediaPlayer 的找到一种方式来循环,看看他们是开放的,如果是这样,释放资源并停止播放或者一种从一般的播放停止所有的MediaPlayer 的?

我将继续寻找在此期间其他方式,希望这点你在正确的方向。

编辑(12/30/09):

 情况下R.id.Button01:
   如果(mp2.isPlaying()){
       mp2.stop();
       mp2.release();
   }
   mp1.reset();
   createMPlayer1(); //用于重新initialze的媒体播放器重复使用,因为资源被释放。
   。MP1 prepare();
   mp1.start();
   打破;
案例R.id.Button02:
   如果(mp1.isPlaying()){
       mp1.stop();
       mp1.release();
   }
   mp2.reset();
   createMPlayer2();
   。MP2 prepare();
   mp2.start();
   打破;公共无效createMPlayer1(){
   MediaPlayer的MP1 = MediaPlayer.create(this.getApplicationContext(),R.raw.aaa);
}公共无效createMPlayer2(){
   MediaPlayer的MP2 = MediaPlayer.create(this.getApplicationContext(),R.raw.bbb);
}

我觉得IOException异常可以尝试再次访问该文件,我们renoved的种源之后,当被调用。我添加了两个方法来创建单独的原始文件,因为我相信资源被释放时,会出现异常。您可以重新初始化 MediaPlayer的's或尝试放弃释放资源。

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.

这篇关于有没有办法停止所有MediaPlayers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆