I called it in the SendNotification method method.
try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); PlaySound(this,notification); } catch (Exception e) { e.printStackTrace(); }
The definition of PlaySound is as follows:
private void PlaySound(Context context,Uri alert){ try{ mPlayer=new MediaPlayer(); mPlayer.setDataSource(context,alert); final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE); if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0); { mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.prepare(); mPlayer.setLooping(true); mPlayer.start(); } }catch(IOException e) { Log.i("AlaramReciever", "no audio file"); } }
To stop the sound when listening to the notification, I changed the MediaPlayer object to an open static object, as shown below:
if(GCMNotificationIntentService.mPlayer != null) { try{ GCMNotificationIntentService.mPlayer.stop(); GCMNotificationIntentService.mPlayer.release(); }finally { GCMNotificationIntentService.mPlayer = null; } }
Desired functionality achieved, but I keep getting the next RunTimeException
The android.media.MediaPlayer $ EventHandler handler, which sends a message to the handler in a dead thread, when it immediately calls onDestroy. Is there any way to prevent this?
source share