MediaPlayer继续播放

时间:2014-10-19 08:27:49

标签: android android-activity android-mediaplayer

Activity内部创建MediaPlayer对象并播放它。但是在后退按钮上按下这个' Activity'走了,但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!");
    }
}

此外,在此Activity创建时,我在通知托盘上创建Notification

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);

现在点击此通知,再次点击此Activity。并创建另一个新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();
    }

}

0 个答案:

没有答案