的MediaPlayer和返回通过意向当前活动 [英] MediaPlayer and returning to current activity via Intent

查看:85
本文介绍了的MediaPlayer和返回通过意向当前活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MediPlayer流网络电台,并在当流播放通知中心的通知显示。

如果我离开应用程序(即通过家庭按钮),然后返回到与通知(收音机在后台播放),我得到一个新的活动,在那里我无法再停止音乐应用程序由于新实例MediaPlayer的创建。

有没有办法要么停止所有的MediaPlayer实例或返回到当前(活动)活动?

  @覆盖
保护无效的onPause(){
    super.onPause();}
@覆盖
保护无效onResume(){
    如果(player.isPlaying()){
        buttonPlay.setEnabled(假);
        buttonStopPlay.setEnabled(真);
    }    super.onResume();
}
在通知中心//显示通知
公共无效showStatus(){
    NotificationCompat.Builder mBuilder =
            新NotificationCompat.Builder(本)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(RADIO)
            .setContentText(你听收音机);    意图resultIntent =新意图(这一点,MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);    TaskStackBuilder stackBuilder = TaskStackBuilder.create(本);    stackBuilder.addParentStack(MainActivity.class);    stackBuilder.addNextIntent(resultIntent);
    的PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);    mNotificationManager.notify(123,mBuilder.build());
}


解决方案

你在哪里创建的实例的MediaPlayer

如果您在活动中这样然后再考虑改变你的策略。

去了解正确的方法是:


  1. 创建一个服务的负责实例化,并呼吁的MediaPlayer 的方法。

  2. 根据您的应用程序逻辑(即文按钮被窃听)的活动应该发布一个意图即会指示服务做在的MediaPlayer
  3. 某些操作
  4. 当你走出去,然后再返回到应用程序活动阉这是一个新的实例或一个老 - 将发布意图来的同一个实例服务,从而一的MediaPlayer

有关详细信息,你应该检查出谷歌对使用服务用MediaPlayer的

I'm using MediPlayer to stream internet radio, and have a notification display in the notification center when the stream is playing.

If I leave the app (i.e. via home button) and then return to the app with the notification (radio playing in background), I get a new activity where I am unable to then stop the music due to a new instance of MediaPlayer being created.

Is there any way to either stop all MediaPlayer instances or return to the current (active) activity?

@Override
protected void onPause() {
    super.onPause();

}


@Override 
protected void onResume() {
    if (player.isPlaying()) {
        buttonPlay.setEnabled(false);
        buttonStopPlay.setEnabled(true);
    }

    super.onResume();
}


//Display notification in notification center
public void showStatus () {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("RADIO")
            .setContentText("You're listening to RADIO");

    Intent resultIntent= new Intent(this, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    stackBuilder.addParentStack(MainActivity.class);

    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(123, mBuilder.build());
}

解决方案

Where are you creating the instance of the MediaPlayer?

If you are doing so within the Activity then consider changing your strategy.

The right way to go about is to:

  1. Create a Service that is in charge of instantiating and calling the methods on the MediaPlayer.
  2. According to your application logic (i.e. wen a button is tapped) the Activity should post an Intent that will instruct the Service to do some operation over the MediaPlayer.
  3. When you go out and back again to the app the 'Activity' wether it's a new instance or an old one - will post the Intents to the same instance of Service and thus to the same instance of a MediaPlayer.

For more details, you should check out Google's documentation on Using a Service with MediaPlayer.

这篇关于的MediaPlayer和返回通过意向当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆