Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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
Java MediaPlayer并通过Intent返回当前活动_Java_Android - Fatal编程技术网

Java MediaPlayer并通过Intent返回当前活动

Java MediaPlayer并通过Intent返回当前活动,java,android,Java,Android,我正在使用MediPlayer播放互联网广播流,播放流时,通知中心会显示通知 如果我离开应用程序(即通过“主页”按钮),然后返回应用程序并发出通知(在后台播放广播),我将获得一个新的活动,在该活动中,由于正在创建新的MediaPlayer实例,我无法停止音乐 是否有任何方法停止所有MediaPlayer实例或返回到当前(活动)活动 @Override protected void onPause() { super.onPause(); } @Override protected

我正在使用MediPlayer播放互联网广播流,播放流时,通知中心会显示通知

如果我离开应用程序(即通过“主页”按钮),然后返回应用程序并发出通知(在后台播放广播),我将获得一个新的活动,在该活动中,由于正在创建新的MediaPlayer实例,我无法停止音乐

是否有任何方法停止所有MediaPlayer实例或返回到当前(活动)活动

@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());
}

您在哪里创建
MediaPlayer的实例

如果你在活动< /代码>中这样做,那么考虑改变你的策略。 正确的方法是:

  • 创建一个
    服务
    ,该服务负责实例化和调用
    MediaPlayer上的方法
  • 根据您的应用程序逻辑(即点击一个按钮),
    活动
    应发布一个
    意图
    ,该意图将指示
    服务
    MediaPlayer
    上执行一些操作
  • 当您外出并再次返回应用程序时,“活动”无论是新实例还是旧实例,都会将
    意图
    发布到
    服务
    的同一实例,从而发布到
    MediaPlayer
    的同一实例

  • 有关更多详细信息,请查看上的谷歌文档。

    非常感谢您的快速回复。这看起来会奏效。再次感谢