Android Media Player不会停止(直播网址)

时间:2015-08-18 17:02:28

标签: java android android-mediaplayer

我有播放声音的服务。

我接到电话时试图阻止声音,但我不能。

我希望在接到电话时停止播放声音,并在播放结束时重播。

有人知道我做错了吗?

这是我的服务代码:

public class MyMediaPlayer extends Service implements OnInfoListener, OnPreparedListener, OnErrorListener, AudioManager.OnAudioFocusChangeListener {

    public static MediaPlayer mMediaPlayer;
    public static String START_PLAY = "START_PLAY";

    IBinder mBinder = new LocalBinder();
    MusicServicePhoneStateListener mPhoneListener;

    public static final String BROADCAST_ACTION = AC.PACKAGE_NAME+".ACTION_HEADSET_PLUG";
    Intent intent;


    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class LocalBinder extends Binder {
        /*public MyMediaPlayer getServerInstance() {
            return MyMediaPlayer.this;
        }*/
    }


    @Override
    public void onCreate()
    {
        super.onCreate();
        mPhoneListener = new MusicServicePhoneStateListener();
        ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        intent = new Intent(BROADCAST_ACTION);
    }//onCreate


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getBooleanExtra(START_PLAY, false)) {
            initializePlayer();
        }
        return START_STICKY;
    }


    public void initializePlayer() {

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mMediaPlayer.setDataSource("rtsp://example.com/file.stream");
        }
        catch (Exception e) {
            Log.e("MyMediaPlayer", "Error");
        }
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnInfoListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.prepareAsync();

    }



    @Override
    public void onPrepared(MediaPlayer player) {
        startMediaPlayer();
    }

    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                // resume playback
                if (mMediaPlayer == null) initializePlayer();
                else if (!mMediaPlayer.isPlaying()) mMediaPlayer.start();
                mMediaPlayer.setVolume(1.0f, 1.0f);
                break;

            case AudioManager.AUDIOFOCUS_LOSS:
                // Lost focus for an unbounded amount of time: stop playback and release media player
                stopMediaPlayer();
                break;

            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                // Lost focus for a short time, but we have to stop
                // playback. We don't release the media player because playback
                // is likely to resume
                pauseMediaPlayer();
                break;

            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                // Lost focus for a short time, but it's ok to keep playing
                // at an attenuated level
                if (mMediaPlayer.isPlaying()) mMediaPlayer.setVolume(0.1f, 0.1f);
                break;
        }

    }


    @Override
    public void onDestroy(){
        stopMediaPlayer();
        ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);
    }


    @Override
    public boolean onError(MediaPlayer player, int what, int extra) {

        if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED || what == MediaPlayer.MEDIA_ERROR_UNKNOWN || extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT || extra == MediaPlayer.MEDIA_ERROR_IO) {
            Log.e("MediaPlayer","Error");
        }

        stopMediaPlayer();
        return true;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return false;
    }

    public void startMediaPlayer() {
        mMediaPlayer.start();
    }

    public void pauseMediaPlayer() {
        if(mMediaPlayer.isPlaying()) mMediaPlayer.pause();
    }

    public void stopMediaPlayer() {
        if(mMediaPlayer.isPlaying()) mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
        Log.d("MediaPlayer","stop()");
    }


    private class MusicServicePhoneStateListener extends PhoneStateListener {

        private boolean mResumeAfterCall = false;

        @Override
        public void onCallStateChanged(int state, String incoming_number) {
            switch (state) {
                case TelephonyManager.CALL_STATE_OFFHOOK:
                case TelephonyManager.CALL_STATE_RINGING:
                    mResumeAfterCall = mMediaPlayer.isPlaying();
                    stopMediaPlayer();
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if (mMediaPlayer == null && mResumeAfterCall) initializePlayer();
                    break;
                default:
                    break;
            }
        }
    }

}

我不明白为什么pause()和stop()不起作用:(

我已经把stopMediaPlayer(); on TelephonyManager.CALL_STATE_OFFHOOK || TelephonyManager.CALL_STATE_RINGING和initializePlayer();在TelephonyManager.CALL_STATE_IDLE这项工作,但我不知道为什么不使用stop()或pause()。

1 个答案:

答案 0 :(得分:0)

你可以尝试一些黑客攻击。

64州解释:

  

已停止'州。在那种状态下,你需要打电话   在开始播放之前再次准备()。