Search code examples
androidaudiomedia-playermessage

Getting error when using mp.stop() in android


I am encountering this error whenever i do a mp.stop in android, basically an sms with body RING:(password) to play the audio and STOP:(password) to stop the audio. The play is working fine but not the stop.

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

Logcat error:

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.

Solution

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

    You are not allowed to call pause() right after initializing a media player. You can call it only at certain states. These states are defined by a state diagram illustrated in below figure.

    EDIT: You said you want it to stop, so things have changed.

    • Move the initialization inside the RING clause and call start().
    • Call stop() directly inside STOP clause and call reset() right after.
    • Ensure that a RING is always followed by a STOP and a STOP is followed by a RING message. Otherwise, you will call the MediaPlayer functions in wrong states and get the same errors.

    enter image description here