Pause music player on a phone call and again resume it after phone call in android

前端 未结 5 784
暗喜
暗喜 2020-12-11 18:25

I am using android.media.MediaPlayer object to play audio files in my app. Everything works fine but when a phone call comes while a song is playing the app doe

相关标签:
5条回答
  • 2020-12-11 19:04

    When your Activity (or service) is interrupted by the phone call place MediaPlayer#pause() into Activity#onPause and code that resumes playing into Activity#onResume I think the problem may be that the MediaPlayer is executed in its own thread and when your Activity is paused it doesn't mean that MediaPlayer is automatically paused as well

    0 讨论(0)
  • 2020-12-11 19:05

    Use a PhoneStateListener to find out when the phone is in use.

    0 讨论(0)
  • 2020-12-11 19:06

    here is the permanent solution. Use audiomanager for getting audio focus. When call comes it calls AUDIOFOCUS_LOSS_TRANSIENT and you can pause the media player in that method. After call it calls AUDIOFOCUS_GAIN method so just call mediaplayer.start method .

    private AudioManager.OnAudioFocusChangeListener  afChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    
                @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                mMediaPlayer.start();}
    
    else if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
    
                    mMediaPlayer.pause();
     }
    
    0 讨论(0)
  • 2020-12-11 19:12

    I had a similar issue. My HTC Desire HD would stop the music when phone call arrived but does not resume after hang up. I found that is caused by the Viber application and when i removed "Viber", everything went back to normal. Now when I receive a call the music pauses and resumes after i finish talking.

    0 讨论(0)
  • 2020-12-11 19:13

    Introduce a PhoneStateListener in your Application like so:

    private PhoneStateListener mPhoneListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber) {
            try {
                switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        pauseStream();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        resumeStream();
                        break;
                    default:
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };
    

    Then you need to set it up in your onCreate() or in your onStartCommand() method in case of a Service:

            radioStream.setAudioStreamType(AudioManager.STREAM_MUSIC);
            radioStream.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer stream) {
                    telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    telephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
                    isMusicPlaying = true;
                    isServiceRunning = true;
                    stream.start();
                }
            });
            try {
                radioStream.prepare();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
                streamErrorHandler();
            } catch (IOException e1) {
                e1.printStackTrace();
                streamErrorHandler();
            }
    

    and you can implement your version of the following methods:

    public static void pauseStream() {
        if (isServiceRunning) {
            if (radioStream.isPlaying()) {
                isMusicPlaying = false;
                radioStream.pause();
            }
        } else {
            Log.i(TAG, "service isn't running");
        }
    }
    
    public static void resumeStream() {
        if (isServiceRunning) {
            if (!radioStream.isPlaying()) {
                isMusicPlaying = true;
                radioStream.start();
            }
        } else {
            Log.i(TAG, "service isn't running");
        }
    }
    

    radioStream is your static MediaPlayer object. I am using this within a service so you can drop out the isServiceRunning but this should work for anyone's case.

    0 讨论(0)
提交回复
热议问题