Android - find if sound is currently playing and stop it

I am using an instance of MediaPlayer to stream audio files from an Internet location. The audio player is in separate mode. The user can select from the list of audio files and go to this operation to play sound.

Now the user can return to the previous action (with a list) and select a different audio file. In this case, I want to stop any other sound that is being played and start playing the new audio that has been selected.

Is there any way I can find out if an audio file is playing without holding the MediaPlayer object?

Thanks.

Edit

I learned how to find out if a sound is playing. We can do this by using the AudioManager object and calling isAudioPlaying (). This will return true if any sound is being played.

Now another question: how to stop sound playback now? I do not have an instance of the MediaPlayer object that was created to start the audio (since the user has already left the action once and returned with a new activity object and, therefore, a new instance of MediaPlayer)

+7
android
source share
5 answers

You will need to call stop() on the MediaPlayer instance. To make this work in your application, you will need:

  • Call stop() within the audio playback activity (in onDestroy() ), e.g.
  • Create a Service to play and communicate with audio from both.

Using Service will allow your code to continue working outside the Activity lifecycle and is the only way to save the MediaPlayer object as you need in this case.

Alternatively, you can create your own subclass of Application and save MediaPlayer there, but using Service is considered best practice.

+10
source share

There is a convenient code for sleeping here until the sound is played:

  AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); while(manager.isMusicActive()) { Log.d("music", "music is active"); try { Thread.sleep(500); } catch (InterruptedException e) { Log.e("music", "interrupted waiting for music to stop"); } Log.d("music", "done playing music"); } 
+2
source share

@lostintransit "Would it be better to use a service, a static variable, or a singleton class? What would be the best design option?"

I think service is what you want. The built-in media player and Pandora application use the service to ensure that music is not tied to the activity life cycle.

If I understand why you are using singleton or statics, I donโ€™t think it will do what you want. Singleton / static will only be used as part of a single process on Linux. If you start your activity, close it, and then start it again, they will work in different processes.

+2
source share

Try it...

 if (mp1 != null && mp1.isPlaying()) { mp1.stop(); } 

Where mp1 is MediaPlayer

+1
source share

I found a way to check if the audio stream is currently busy (AudioManager.STREAM_RING, AudioManager.STREAM_NOTIFICATION, etc.):

 /** * Unhide android api: check is stream is active now (AudioManager.STREAM_RING, AudioManager.STREAM_NOTIFICATION...), * uses reflection * @param audioStream * @return */ public static boolean isStreamActive(int audioStream) { Class<?> audioSystemClazz = null; Boolean res = false; try { audioSystemClazz = Class.forName("android.media.AudioSystem"); if (null != audioSystemClazz) { // isStreamActive Method method = audioSystemClazz.getDeclaredMethod("isStreamActive", new Class<?>[] { int.class, int.class }); if (null != method) { res = (Boolean) method.invoke(null, audioStream, 0); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return res; } 
+1
source share

All Articles