【问题标题】:mediaplayer.stop causes android app to crashmediaplayer.stop 导致 android 应用程序崩溃
【发布时间】:2022-10-05 01:22:17
【问题描述】:

在我们的 andorid 广播接收器类中,我们试图停止从 mainactivity.class 启动的铃声。我们使用 mediaplayer.stop 来停止铃声,它成功地完成了,但它使应用程序崩溃。我们使用了 .stop ()、.pause ()、.release (),但不幸的是,它们都不起作用。广播接收器的代码如下所示

初始化媒体播放器的 Firebase 类(摘要代码),并在媒体播放器停止时触发按钮接收器类的未决广播意图。

public class Firebase extends FirebaseMessagingService {
            public static Ringtone ringtone;
            public static MediaPlayer mp;

Intent buttonIntent = new Intent(this, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",notification_id);
PendingIntent btsPendingIntent = PendingIntent.getBroadcast(this, requestID, buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action1 = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Dismiss", btsPendingIntent).build();

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setTimeoutAfter(60000)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setAutoCancel(false)
                    .setContentIntent(pendingIntent)
                    .addAction(action1)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setFullScreenIntent(pendingIntent2, true)
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        
                mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
                mp.start();
        }

按钮接收器类(媒体播放器停止的地方) 公共类 ButtonReceiver 扩展 BroadcastReceiver { 公共媒体播放器mp;

    @Override
    public void onReceive(Context context, Intent intent) {

        int notificationId = intent.getIntExtra("notificationId", 0);

    
        mp.stop ();
        mp.seekTo(0);

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);

    }
}

请帮助我们解决这个问题。

【问题讨论】:

    标签: java android android-mediaplayer


    【解决方案1】:

    您的代码没有显示 MediaPlayer 正在初始化,因此一旦您尝试引用 mp 实例,它肯定会崩溃。

    我很确定您误解了MediaPlayer 的工作原理。它不是一项全球服务——您必须自己管理每个实例。根据您的代码,您在不同的地方实例化它两次,并期望它们以某种方式控制同一个实例。您正在声明和创建MediaPlayer,并在FirebaseMessagingService 类中开始播放,但您试图在BroadcastReceiver 类中停止它。但是在那里,您尝试使用一个完全不同的 MediaPlayer 实例,它从未被初始化,这就是它崩溃的原因。

    您需要保留对 mp 对象的全局引用(在单例中或在您的 Application 对象中),并更改其余代码以正确使用它。例如:

     public class App extends Application {
            public static MediaPlayer mp;
        }
    
    public class Firebase extends FirebaseMessagingService {
       ...
    
       if(App.mp == null){
            App.mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
       }
       App.mp.start();
    
       ....
    }
    

    并在您的广播接收器中:

    if(App.mp != null){
            App.mp.stop ();
            App.mp = null
       }
    

    【讨论】:

    • 添加更多代码,请检查
    • 我仍然看不到媒体播放器的初始化位置与您尝试停止它的位置之间有任何联系。这两段代码与您发布的内容完全脱节
    • 添加了更多解释
    猜你喜欢
    • 2014-05-08
    • 2013-12-02
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2018-01-29
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多