Java – how to make Android media player play sound?
•
Java
I don't know why it doesn't work. There are no recording errors in logcat, but I can't hear the sound
public static void DeclararSonido(int numero,Context contexto){ switch(numero){ case 0: mp = MediaPlayer.create(contexto,R.raw.alan); break; } } public static void TocarPiedra( int posicion,Context contexto){ DeclararSonido(posicion,contexto); mp.start(); mp.stop(); mp.release(); } public static void TocarSirena(Context contexto){ MediaPlayer mp2= MediaPlayer.create(contexto,R.raw.doh); mp2.start(); mp2.stop(); mp2.release(); }
If I erase MP2 stop(); And MP2 release(); AND mp. stop(); And MP release(); The application plays a sound, but the file is not published
Solution
Of course you don't want to start right away and then stop right away
The problem is that you are following these steps:
mp.start(); // starts playback mp.stop(); // .. then stops immediately .. mp.release();
You should call start and release it when the sound finishes playing You can use completion event to hook up the listener and release it there:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer player) { player.release(); } })
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码