Android-如何只允许一次播放MediaPlayer的一个实例? [英] Android - How do I only allow one instance of MediaPlayer to play at a time?

查看:69
本文介绍了Android-如何只允许一次播放MediaPlayer的一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ListView项作为按钮来创建一个简单的Sound-board Android应用.(顺便说一句,我是新手程序员)

I'm trying to create a simple Sound-board Android app, using ListView items as buttons. (Btw, I'm a novice programmer)

这个想法是我按下一个按钮,然后播放一个特定的声音文件.如果我在播放声音时按任何按钮,则应先停止播放该声音,然后再开始播放新声音.

The idea is that I press a button, and a specific sound file plays. If I press any button while a sound is playing, it should first stop that sound and then start to play the new one.

当前播放声音时不会停止任何当前播放的声音,因此,如果我向按钮发送垃圾邮件,我会同时播放多个声音(如果我一次按下太多,则应用程序强制关闭).

Currently the sounds play without stopping any currently playing sounds, so that if I spam the buttons I get multiple sounds playing at the same time (and if I press too many at once, the app force closes).

我尝试使用以下几种变体:

I have tried using a few variations of:

if (mp.isPlaying()) {
  mp.stop();
}

但是根据我在其他一些资料中所读到的内容,我正在创建MediaPlayer类的多个实例,即使它们具有相同的名称,stop()方法也会尝试在mp的最新实例上停止(在某些情况下,甚至还没有创建).

But according to what I read on a few other sources, I am creating multiple instances of the MediaPlayer class, and even though they have the same name the stop() method tries to stop on the latest instance of mp (in some cases it isn't even created yet).

我猜我对MediaPlayer类的一般实现是错误的,但这是我能想到的最好的方法.

I'm guessing my general implementation of the MediaPlayer class is wrong, but it's the best I could figure out to do.

无论如何,这是相关的代码块:

Anyways, here's the relevant block of code:

public class soundTest extends Activity {
  private ListView lv1;
  private String lv_arr[]={"test 1","test 2","test 3","test 4","test 5"};

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv1=(ListView)findViewById(R.id.ListView01);
    lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, lv_arr));

    lv1.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        if (lv1.getItemAtPosition(position)=="test 1") {
          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound1);
          mp.start();
          mp.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
              mp.release();
            }
          });
        }

        if (lv1.getItemAtPosition(position)=="test 2") {
          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound2);
          mp.start();
          mp.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
              mp.release();
            }
          });
        }

        //And the rest of the sounds 3,4,5.

      }
    });
  }
}

感谢您的任何帮助.

编辑(3月22日):

我发现下面的代码应该可以工作:

I've found the following piece of code that should work:

mp.setDataSource(context, Uri.parse("android.resource://" + Config.PACKAGE + "/" + resId));

但是,我无法弄清楚"Config.PACKAGE"部分是如何工作的.我只是收到一条错误消息:"PACKAGE无法解析,或者不是字段".

But, I can't figure out how the "Config.PACKAGE" part works. I just get an error "PACKAGE cannot be resolved, or is not a field".

我尝试用软件包名称替换"PACKAGE",同样的错误.我也尝试过:

I tried replacing "PACKAGE" with the package name, same error. I also tried:

try {
  mp.setDataSource(getApplicationContext(),Uri.parse("android.resource://com.mptest/" + R.raw.test2));
} catch (IOException e) {
  e.printStackTrace();
}

但是我无法确切地代替"//com.mptest/".

But I can't work what exactly to put in place of "//com.mptest/".

推荐答案

全局变量 MediaPlayer 需要设置为 private static .这让我抓了好几次.

The global variable MediaPlayer needs to be set private static. This has caught me several times.

这篇关于Android-如何只允许一次播放MediaPlayer的一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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