Android How do I allow only one instance of MediaPlayer to be shown?

I am trying to create a simple Soundboard Android application using ListView items as buttons. (By the way, I'm a beginner programmer)

The idea is that I press a button and a specific sound file is played. If I press any button while playing a sound, it must first stop that sound and then start playing a new one.

At the moment, sounds are played without stopping the sounds currently sounding, so if I spam the buttons, I get several sounds at the same time (and if I click too much at once, the application’s power closes).

I tried using several options:

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

But in accordance with what I read in several other sources, I create several instances of the MediaPlayer class, and although they have the same name, the stop () method tries to stop the last mp instance (in some cases not yet created).

I assume my general implementation of the MediaPlayer class is incorrect, but this is the best I could do.

In any case, here is the corresponding code block:

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

Any help would be appreciated, thanks.

Edit (March 22):

I found the following code snippet that should work:

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

But I can’t understand how "Config.PACKAGE" works. I just get the error "PACKAGE could not be resolved or is not a field."

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

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

But I can’t work, what exactly to put instead of "//com.mptest/".

+4
source share
5 answers

The global variable MediaPlayer must be set to private private static . It caught me several times.

+3
source

Do not use global. Use the singleton pattern. This is what it is needed for, so you can have exactly one instance without using a global variable.

There are many good Java code examples for this template, starting with http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html , and you won’t be mistaken.

+2
source

Consider storing MediaPlayer as a global variable instead of multiple instances.

+1
source

You can find a good link in VideoView Source , as it processes one MediaPlayer object, and you can change the content during playback.

0
source

Use the service, it makes it easy to stop the current sound and play another without simultaneous playback.

-2
source

Source: https://habr.com/ru/post/1344438/


All Articles