Android media players forever on ICS

I want to play a notification sound, and my problem is that the sound loops forever when it should sound only once.

I tried two ways:

notification.sound = Uri.parse("content://media/internal/audio/media/38"); 

and

 mMediaPlayer = new MediaPlayer(); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); mMediaPlayer.setDataSource(this, Uri.parse("content://media/internal/audio/media/38")); mMediaPlayer.setLooping(false); mMediaPlayer.prepare(); mMediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Log.v(Utils.TAG, "onprepared"); mp.start(); } }); mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.v(Utils.TAG, "end, we should release"); mp.stop(); mp.release(); } }); 

In the second case, I never see the end, β€œthe end we must let go”, the sound is played again and again.

Any idea?

Many thanks

UPDATE:

I tried two devices and:

  • He forever obsessed with the Galaxy Nexus with ICS 4.0.4.
  • It works fine on HTC Hero 2.2.1
+6
source share
2 answers

Try the following:

 try { mp.setDataSource(uri.toString()); mp.setAudioStreamType(AudioManager.STREAM_ALARM); mp.prepare(); mp.start(); mp.setOnSeekCompleteListener(new OnSeekCompleteListener() { public void onSeekComplete(MediaPlayer mp) { mp.stop(); mp.release(); } }); } catch (Exception e) { e.printStackTrace(); } 
+5
source

There are sounds with β€œbuilt-in” loops that will play forever. This is different from setting a MediaPlayer loop. If you start playing sound with a built-in loop, it will never end. This is basically a ringtone (as opposed to alarms or notification tones). You can set RingtoneManager to return only notifications or alarms using myringtonemanager.setType(RingtoneManager.TYPE_ALARM|RingtoneManager.TYPE_NOTIFICATION) which will exclude most sounds, but unfortunately this does not guarantee that all of them will be excluded on any device. : - (

So Tiago Almeida's solution is a good job (and I voted), although it will also truncate all sounds that have only a few (and without endless) loops.

+5
source

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


All Articles