0

每当我在 android 中执行 mp.stop 时,我都会遇到此错误,基本上是带有 body RING:(password) 来播放音频和 STOP:(password) 来停止音频的短信。该剧运行良好,但没有停止。

  MediaPlayer mp = MediaPlayer.create(context, R.raw.braincandy);

    if (correctSHA1 != null) {    


        if (intent.getAction().equals(SMS_RECEIVED)) {

          Bundle bundle = intent.getExtras();   
          if (bundle != null) {

          Object pdus[] = (Object[]) bundle.get("pdus");
          SmsMessage smsMessage[] = new SmsMessage[pdus.length];   

          for(int i = 0; i < pdus.length; i++) {   
              smsMessage[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            if (smsMessage[i].getMessageBody().contains("RING:")) {    
                String[] tokens = smsMessage[i].getMessageBody().split(":"); 
                Log.v(TAG, "RING SMS Message keyword detected");

                if (tokens.length >= 2) {    

                    Log.v(TAG, "Token 1: " + tokens[0]);
                    Log.v(TAG, "Token 2: " + tokens[1]);

                    String SHA1hash = PhoneFinder.getSHA1(tokens[1]);   

                    Log.v(TAG, "SHA1 Hash:" + SHA1hash);
                    Log.v(TAG, "Correct SHA1:" + correctSHA1);

                    if (SHA1hash.equals(correctSHA1)) {    
                        Log.v(TAG, "SHA1 hash matched!");
                        this.abortBroadcast(); //Abort Broadcast for SMS Message

                        String to = smsMessage[i].getOriginatingAddress (); 
                        SmsManager sm = SmsManager.getDefault(); 

                        sm.sendTextMessage (to, null, "success!", null, null);
                        NotificationManager nm = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE); 
                        Notification notification = new Notification(android.R.drawable.ic_dialog_info, "Alert", System.currentTimeMillis());
                        PendingIntent contentIntent = PendingIntent.getActivity (context, 0, new Intent (context, PhoneFinder.class), 0);
                        notification.setLatestEventInfo (context, "GPS address", "You have been detected", contentIntent);
                        nm.notify (R.string.service_start, notification);

                        //context.startService(new Intent(context, MyService.class));
                        mp.start();

                        }    

                    }    
                } 
                else if (smsMessage[i].getMessageBody().contains("STOP:")) {
                    String[] tokens = smsMessage[i].getMessageBody().split(":"); 
                    Log.v(TAG, "STOP SMS Message keyword detected");

                    if (tokens.length >= 2) {    

                        Log.v(TAG, "STOP Token 1: " + tokens[0]);
                        Log.v(TAG, "STOP Token 2: " + tokens[1]);

                        String SHA1hash = PhoneFinder.getSHA1(tokens[1]);   

                        Log.v(TAG, "STOP SHA1 Hash:" + SHA1hash);
                        Log.v(TAG, "STOP Correct SHA1:" + correctSHA1);

                        if (SHA1hash.equals(correctSHA1)) {    
                            Log.v(TAG, "STOP SHA1 hash matched!");
                            this.abortBroadcast(); //Abort Broadcast for SMS Message


                        //  context.stopService(new Intent(context, MyService.class));
                            if (mp.isPlaying())
                                mp.pause();
                            }    
                }

日志错误:

07-22 10:11:30.082: V/Logging Activity(7207): New Broadcast Receiver
07-22 10:11:30.082: V/Logging Activity(7207): Password: passwd
07-22 10:11:30.087: V/MediaPlayer-JNI(7207): native_setup
07-22 10:11:30.087: V/MediaPlayer(7207): constructor
07-22 10:11:30.092: V/MediaPlayer(7207): setListener
07-22 10:11:30.092: V/MediaPlayer-JNI(7207): setDataSourceFD: fd 57
07-22 10:11:30.092: V/MediaPlayer(7207): setDataSource(57, 652, 2625504)
07-22 10:11:30.107: V/MediaPlayer(7207): setVideoSurfaceTexture
07-22 10:11:30.107: V/MediaPlayer(7207): prepare
07-22 10:11:30.112: V/MediaPlayer(7207): message received msg=5, ext1=0, ext2=0
07-22 10:11:30.112: V/MediaPlayer(7207): New video size 0 x 0
07-22 10:11:30.112: V/MediaPlayer(7207): callback application
07-22 10:11:30.117: V/MediaPlayer(7207): back from callback
07-22 10:11:30.117: V/MediaPlayer(7207): message received msg=1, ext1=0, ext2=0
07-22 10:11:30.117: V/MediaPlayer(7207): prepared
07-22 10:11:30.117: V/MediaPlayer(7207): signal application thread
07-22 10:11:30.117: V/MediaPlayer(7207): callback application
07-22 10:11:30.117: V/MediaPlayer(7207): back from callback
07-22 10:11:30.117: V/MediaPlayer(7207): prepare complete - status=0
07-22 10:11:30.122: D/Gsm/SmsMessage(7207): SMS SC timestamp: 1342923088000
07-22 10:11:30.132: V/Logging Activity(7207): RING SMS Message keyword detected
07-22 10:11:30.132: V/Logging Activity(7207): Token 1: RING
07-22 10:11:30.132: V/Logging Activity(7207): Token 2: abc123
07-22 10:11:30.137: V/Logging Activity(7207): SHA1 Hash:6367c48dd193d56ea7b0baad25b19455e529f5ee
07-22 10:11:30.137: V/Logging Activity(7207): Correct SHA1:6367c48dd193d56ea7b0baad25b19455e529f5ee
07-22 10:11:30.137: V/Logging Activity(7207): SHA1 hash matched!
07-22 10:11:30.167: V/MediaPlayer-JNI(7207): start
07-22 10:11:30.167: V/MediaPlayer(7207): start
07-22 10:11:30.202: E/MediaPlayer(7207): mOnVideoSizeChangedListener is null. Failed to send MEDIA_SET_VIDEO_SIZE message.
07-22 10:11:30.202: I/MediaPlayer(7207): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
07-22 10:11:30.202: E/MediaPlayer(7207): mOnPreparedListener is null. Failed to send MEDIA_PREPARED message.
07-22 10:11:37.592: V/Logging Activity(7207): New Broadcast Receiver
07-22 10:11:37.592: V/Logging Activity(7207): Password: passwd
07-22 10:11:37.592: V/MediaPlayer-JNI(7207): native_setup
07-22 10:11:37.592: V/MediaPlayer(7207): constructor
07-22 10:11:37.592: V/MediaPlayer(7207): setListener
07-22 10:11:37.592: V/MediaPlayer-JNI(7207): setDataSourceFD: fd 57
07-22 10:11:37.592: V/MediaPlayer(7207): setDataSource(57, 652, 2625504)
07-22 10:11:37.612: V/MediaPlayer(7207): setVideoSurfaceTexture
07-22 10:11:37.612: V/MediaPlayer(7207): prepare
07-22 10:11:37.617: V/MediaPlayer(7207): message received msg=5, ext1=0, ext2=0
07-22 10:11:37.617: V/MediaPlayer(7207): New video size 0 x 0
07-22 10:11:37.617: V/MediaPlayer(7207): callback application
07-22 10:11:37.617: V/MediaPlayer(7207): back from callback
07-22 10:11:37.617: V/MediaPlayer(7207): message received msg=1, ext1=0, ext2=0
07-22 10:11:37.617: V/MediaPlayer(7207): prepared
07-22 10:11:37.617: V/MediaPlayer(7207): signal application thread
07-22 10:11:37.617: V/MediaPlayer(7207): callback application
07-22 10:11:37.617: V/MediaPlayer(7207): back from callback
07-22 10:11:37.617: V/MediaPlayer(7207): prepare complete - status=0
07-22 10:11:37.617: D/Gsm/SmsMessage(7207): SMS SC timestamp: 1342923096000
07-22 10:11:37.617: V/Logging Activity(7207): STOP SMS Message keyword detected
07-22 10:11:37.617: V/Logging Activity(7207): STOP Token 1: STOP
07-22 10:11:37.617: V/Logging Activity(7207): STOP Token 2: abc123
07-22 10:11:37.617: V/Logging Activity(7207): STOP SHA1 Hash:6367c48dd193d56ea7b0baad25b19455e529f5ee
07-22 10:11:37.617: V/Logging Activity(7207): STOP Correct SHA1:6367c48dd193d56ea7b0baad25b19455e529f5ee
07-22 10:11:37.617: V/Logging Activity(7207): STOP SHA1 hash matched!
07-22 10:11:37.617: V/MediaPlayer(7207): isPlaying: 0
07-22 10:11:37.617: V/MediaPlayer-JNI(7207): isPlaying: 0
07-22 10:11:37.617: E/MediaPlayer(7207): mOnVideoSizeChangedListener is null. Failed to send MEDIA_SET_VIDEO_SIZE message.
07-22 10:11:37.617: I/MediaPlayer(7207): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
07-22 10:11:37.617: E/MediaPlayer(7207): mOnPreparedListener is null. Failed to send MEDIA_PREPARED message.
4

2 回答 2

0

在准备好之前,您无法启动媒体播放器,如果没有播放,您将无法暂停媒体播放器:)

所以你必须对你的代码进行修改,或者你可能想看看一个非常好的教程

处理 Android 媒体播放器

这肯定会帮助你。

谢谢 :)

于 2012-07-22T00:31:58.373 回答
0
MediaPlayer mp = MediaPlayer.create(context, R.raw.braincandy);
mp.pause();

您不能在初始化媒体播放器后立即调用 pause()。您只能在某些州调用它。这些状态由下图所示的状态图定义。

编辑:你说你希望它停止,所以事情已经改变了。

  • 在 RING 子句中移动初始化并调用 start()。
  • 直接在 STOP 子句中调用 stop() 并在之后调用 reset()。
  • 确保 RING 后面总是跟着一个 STOP,而 STOP 后面跟着一个 RING 消息。否则,您将在错误状态下调用 MediaPlayer 函数并得到相同的错误。

在此处输入图像描述

于 2012-07-22T01:32:11.430 回答