从通知中输入应用程序后无法停止媒体播放器

时间:2013-04-08 04:44:22

标签: android alarmmanager android-mediaplayer android-notifications

我有一个闹钟应用程序,可在闹钟响起时播放音乐。这是我的第一个Android应用程序。当我在应用程序中时,我可以显示一个按钮并停止音乐。但是,我已启用通知,并希望在用户点击通知时停止播放音乐。我从通知返回应用程序,但我无法停止音乐。除非我按下后退键,否则当我从通知返回应用程序时,我显示的按钮不会显示。如果我点击后退按钮,按钮就会出现。我已经回顾了几个Media Player示例和通知示例,但似乎都没有我的问题。如果我能回到正确的位置,我知道如何停止音乐。通知管理器是否返回了我可以检查的标志?我花了很多时间在这上面,并没有想出一个好的解决方案。此外,我的通知图标非常小。我怎么能放大呢?

这是我的代码:

    public void setAlarmPlaySong() {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override public void onReceive( Context context, Intent _ )
        {   
            stopPlaying();
            // Set up for playing the song selected          
            switch(mPos) {
                case 0:
                    // Play song - 
                    mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.calmyourstorm);
                    mPlayer.start(); 
                break;
                case 1:
                    // Play song - 
                    //mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.gowiththeflow);
                    //mPlayer.start(); 
                break;
                case 2:
                    // Play song - 
                    mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.freeyourmind);
                    mPlayer.start(); 
                break;
                case 3:
                    // Play song - 
                break;  
                case 4:
                    // Play song - 
                break;  
            } // end switch         
            context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
            showStopAlarmButton();
        } // end on Receive
    };  
        //
        //Intent intent = new Intent(getApplicationContext(), SetAlarmActivity.class);
        createNotification();        
        //Create alarm manager
        AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

        this.registerReceiver( receiver, new IntentFilter("STOP_MUSIC") );
        PendingIntent sender = PendingIntent.getBroadcast( this, 0, new Intent("STOP_MUSIC"), 0 );
        //PendingIntent sender = PendingIntent.getBroadcast( this, 0, intent, 0 );

        //set the timer as a RTC Wakeup to alarm manager object
        manager.set( AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), sender );   
}

这是createNotification

public void createNotification() {
    // Create the notification that will display when the app is
    // closed.      

    Context ctx = getApplicationContext();

    // The intent that is triggered if the notification
    // is trigger
    Intent notificationIntent = new Intent(ctx, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx,
            YOUR_PI_REQ_CODE, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //Resources res = ctx.getResources();
    // Build notification
    Notification.Builder builder = new Notification.Builder(ctx);
    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.beasleybackground2)
                //.setTicker(res.getString(R.string.your_ticker))
                //.setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Jazz Alarm")
                .setContentText("Good Morning!");
                //.setContentTitle(res.getString(R.string.your_notif_title))
                //.setContentText(res.getString(R.string.your_notif_text));
    Notification n = builder.build();

    // Hide the notification after its selected
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(YOUR_NOTIF_ID, n);                   

}   // createNotification

这是showStopAlarmButton

        public void showStopAlarmButton() {
    final Button alarmOffButton = (Button) findViewById(R.id.btn_alarmOff);
    alarmOffButton.setVisibility(View.VISIBLE);
    alarmOffButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
            //When button is clicked stop the alarm and hide the button
            //when play is clicked show stop button and hide play button
            alarmOffButton.setVisibility(View.GONE);                
            // Cancel song
            stopPlaying();  
        }
    });     
}  

0 个答案:

没有答案