Pause Media Player in case of phone call or user open a video or an audio in another program

android media player library
android mediaplayer
android-mediaplayer service example
audio focus android
android media player app
android mediaplayer video example
simple music player android studio
android music player source code tutorial

I want to pause media player in case the user use another program to watch video or the user receive a phone call like any multimedia app (YouTube ,...)

MediaPlayer mediaPlayer = MediaPlayer.create(this,;


You need to override android methods which relate to the lifecycle of an android application

    protected void onStop() {
        mediaPlayer.stop(); //put your stop criteria here

    public void onPause() {
        mediaPlayer.pause(); //put your pause criteria here

    public void onResume() {
        super.onResume();; //put your resume criteria here

you can simply override these methods to do your job.

Using MediaPlayer to play an Audio File in Android, Which class can you use to play a music file? I have created an android application to play text to speech file using media player but if other audio/video starts playing then also my audio plays i.e two audios are played simultaneously. Is there any way to stop first audio before starting another audio/video. Is there any broadcast receiver which will get called on the start of other audio.

MediaPlayer can be implemented as a service. Pause the service when the event happens using the BroadcastReceiver and you can again resume it using that. It would work in this manner.

all the resource when the application is in onPause() state. I've developed a music player app where i need to pause music when user gets a call through his skype app. I've tried AudioFocus and phone state listener concepts but it is not working for skype. Can somebody help me out on this. Thanks in advance.

You have to use AudioFocus ( In this way you will receive an Event when another App is taking "the ownership" of Audio Device to play its content/audio and then another Event will be raised when that App stops to play it and the "ownership" return to your App.

When another app gains audio focus, stop or pause playing, or duck the Beginning with Android 8.0 (API level 26), when you call Use the builder's methods to set the other fields of the request. mediaPlayer = MediaPlayer() and video playback apps, it isn't useful when playing spoken content, such  For example, when a user is listening to music and another application needs to notify the user of something very important, the user might not hear the notification tone due to the loud music. Starting with Android 2.2, the platform offers a way for applications to negotiate their use of the device's audio output. This mechanism is called Audio Focus.

MediaPlayer class can be used to control playback of audio/video files and is thrown if prepare() or prepareAsync() is called in any other state. is updated in calls to isPlaying() , and it can be a number of seconds in the case of streamed content. Calling start() to resume playback for a paused MediaPlayer object, and the  In order to add pause functionality when a call is incoming I created a receiver that sets a variable when the call is coming. The activity reads this variable in onPause(), pauses the music and resets it. When the call is done and the activity is resumed, the music is resumed in onResume(). This works fine as long the activity has the focus.

How to handle edge use-cases like incoming calls, change of audio outputs (e.g. You use the READ_PHONE_STATE permission to access phone state to listen class, which with minimal setup you can use to play audio and video. moved to playing media on another app, so release the media player. The media player is used to view and listen to video and audio. Media playback can be inline (embedded in a page or with a group of other controls) or in a dedicated full-screen view. You can modify the player's button set, change the background of the control bar, and arrange layouts as you see fit.

creates a new object called MediaPlayer. To play a streaming from the network​, this function may take some time, in which case you should use instead the which allows to continue execution of the program even without waiting for the video. Since we need to stop playing the video and continue it, pause() and start()  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.