Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MediaPlayer继续播放_Android_Android Activity_Android Mediaplayer - Fatal编程技术网

Android MediaPlayer继续播放

Android MediaPlayer继续播放,android,android-activity,android-mediaplayer,Android,Android Activity,Android Mediaplayer,在活动中,我正在创建一个MediaPlayer对象并播放它。但在“后退”按钮上,按下此“活动”消失,但MediaPlayer继续播放音乐。我想要这种行为 private void playSound(Context context, Uri alert){ mMediaPlayer = new MediaPlayer(); try{ mMediaPlayer.setDataSource(context, alert); final AudioMa

在活动中,我正在创建一个MediaPlayer对象并播放它。但在“后退”按钮上,按下此“活动”消失,但MediaPlayer继续播放音乐。我想要这种行为

private  void playSound(Context context, Uri alert){
    mMediaPlayer = new MediaPlayer();
    try{
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0){
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    }catch (IOException e){
        Log.i("AlarmReceiver", "No audio files are Found!");
    }
}
此外,在这个活动创建中,我正在通知栏上创建一个通知

final NotificationManager mgr =
        (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.img_backmain,
        "Wake up!",
        System.currentTimeMillis());
Calendar c = Calendar.getInstance();
int intentId = (rowId * 1000)+c.getTime().getDay();
// This pending intent will open after notification click
PendingIntent mNotifyIntent= PendingIntent.getActivity(this, intentId, getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

note.setLatestEventInfo(this, ""+ringingAlarm.get_alarm_time()+" - "+ringingAlarm.get_note()+" "+rowId,
        "Wake up! It's Late Already", mNotifyIntent);

note.flags |= Notification.FLAG_NO_CLEAR;

mgr.notify(rowId, note);
现在,在此通知上再次单击此活动创建。以及创建另一个新的MediaPlayer。所以有两种音乐播放

问题是,我不想要这两个不同的MediaPlayer实例

更新 代码摘要:

    public class AlarmReceiverActivity extends Activity {
        @Override
        public void onCreate(Bundle saveInstanceState){
            super.onCreate(saveInstanceState);
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Log");
            mWakeLock.acquire();

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |
                            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,

                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

            setContentView(R.layout.alarm);

            .....
            .....

            final NotificationManager mgr =
                    (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification note = new Notification(R.drawable.img_backmain,

                "Wake up!",
                System.currentTimeMillis());
        Calendar c = Calendar.getInstance();
        int intentId = (rowId * 1000)+c.getTime().getDay();
        // This pending intent will open after notification click
        PendingIntent mNotifyIntent= PendingIntent.getActivity(this, intentId, getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

        note.setLatestEventInfo(this, ""+ringingAlarm.get_alarm_time()+" - "+ringingAlarm.get_note()+" "+rowId,
                "Wake up! It's Late Already", mNotifyIntent);

        note.flags |= Notification.FLAG_NO_CLEAR;
        //After uncomment this line you will see number of notification arrived
        //note.number=2;
        mgr.notify(rowId, note);

       .....
       .....

        playSound(this, getAlarmUri());

        stopAlarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMediaPlayer.stop();
                finish();

            }
        });
    }

    ...........
    ...........

    private  void playSound(Context context, Uri alert){
        mMediaPlayer = new MediaPlayer();
        try{
            mMediaPlayer.setDataSource(context, alert);
            final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0){
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            }
        }catch (IOException e){
            Log.i("AlarmReceiver", "No audio files are Found!");
        }
    }

    protected  void onStop(){
        super.onStop();
        mWakeLock.release();
    }

}

你读过吗?非常抱歉,我没有把所有的东西都放在前面。我已经更新了我的问题。请检查一下。对不起,我的英语不好。我没有看到任何关于释放或停止一个或多个MediaPlayer的代码。请阅读我之前评论中的链接。这里我用代码更新了我的问题。请复习。