Android one MediaPlayer instance - singleton

自闭症网瘾萝莉.ら 提交于 2019-12-13 13:15:03

问题


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.

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

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

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?

Thanks!


回答1:


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

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.

EDIT: 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();



回答2:


You can do this :


public class MainActivity extends Activity {



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


}

public void playSound(View view){
    MyMediaPlayer.getInstance().create(getApplicationContext(), R.raw.sound).start();

}
public void stopSound(View view){
    MyMediaPlayer.getInstance().stop();

}

}


来源:https://stackoverflow.com/questions/28380525/android-one-mediaplayer-instance-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!