Android一个MediaPlayer实例-Singleton [英] Android one MediaPlayer instance - singleton

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

问题描述

我将要创建一个简单的android应用程序,以在单击按钮时播放声音,但是我难以理解单例设计模式,这对本应用程序非常有帮助.我想要达到的目的是要有多个活动,并且仅共享一个MediaPlayer实例,以便当用户按下按钮声音时播放,并且如果他在相同或不同活动上按下相同或另一个按钮,声音将停止.

I am about to create simple android app to play sound on a button click but I struggle with understanding singleton design pattern which would be very helpful in this application. What I try to achieve is to have number of activities and share only one MediaPlayer instance among them so that wehen user presses the button sound plays and if he will press the same or another button on same or different activity the sound will stop.

这是我的代码,但是在两次按下按钮之后,创建了另一个MediaPlayer实例,您可以在此处同时播放相同的声音

Here is my code but after pressing the button twice another instance of MediaPlayer is created and you can here the same sound being played at the same time

public class MyMediaPlayer {
MediaPlayer mp;
private static volatile MyMediaPlayer instance = null;
private MyMediaPlayer() { }

public static MyMediaPlayer getInstance() {
    if (instance == null) {
        synchronized (MyMediaPlayer.class) {
            if (instance == null) {
                instance = new MyMediaPlayer();
            }
        }
    }

    return instance;
}
}

和MainActivity.java:

and MainActivity.java:

public class MainActivity extends Activity {

private MyMediaPlayer player = getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void playSound(View view){
    player.mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
    player.mp.start();
}
}

由于我经验不是很丰富,除了上述代码的提示之外,您还可以向我解释如何访问单例字段.我不确定我的方法是否正确.如果我有单例课程,并且想使用此MediaPlayer实例,该怎么做?

As I am not very experienced could you in addition to tips on above code explain me how can I access a field of a singleton. I am not sure if my approach is correct. If I have singleton class and I want to use this MediaPlayer instance how do I do that?

谢谢!

推荐答案

添加null检查要在playSound按钮上创建的mp对象,单击:

Add null check for mp object which you are creating on playSound Button click :

public void playSound(View view){
    if(player.mp==null)
      player.mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
      player.mp.start();
}

因为您已经为MyMediaPlayer类创建了单例类,这样可以在player可用时避免创建新对象.但是mp每次都会初始化.

because you have created singleton class for MyMediaPlayer class which avoid creating new object when player is already available. but mp is initialized every time.

要使用单个MediaPlayer播放多种声音,请按以下步骤操作:

For playing multiple sound using single MediaPlayer do it as:

if(player.mp ==null)
  player.mp = new MediaPlayer();
else
  player.mp.reset();
String fileName="android.resource://"+getPackageName()+
                                               "/"+ R.raw.sound;
player.mp.setDataSource(getApplicationContext(),Uri.parse(fileName));
player.mp.prepare();
player.mp.start();

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

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