2012-07-22 19 views
0

每當我在android中執行一個mp.stop時,我遇到了這個錯誤,基本上是一個短信與身體RING:(密碼)播放音頻和STOP:(密碼)停止音頻。這個劇本運行良好,但不是停止。在android中使用mp.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的錯誤:

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. 
+0

我們不能幫你與你的代碼的一部分。它似乎缺乏聽衆! – 2012-07-22 00:21:40

+0

剛剛使用代碼 – dythe 2012-07-22 00:25:30

+0

@dythe編輯如果我的回答可以幫助你,而不是忘了接受它並投票給我。謝謝:) – SALMAN 2012-07-22 00:32:59

回答

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

你不允許初始化媒體播放器後,有權要求暫停()。你只能在某些州撥打它。這些狀態由下圖所示的狀態圖來定義。

編輯:你說你想讓它停下來,所以事情已經改變。

  • 在RING子句中移動初始化並調用start()。
  • 直接在STOP子句內調用stop()並立即調用reset()。
  • 確保RING後面總是跟隨STOP,而STOP後面跟RING消息。否則,您將以錯誤的狀態調用MediaPlayer函數並獲取相同的錯誤。

enter image description here

+0

不工作仍然需要準備好的監聽器,我真的不明白如何準備好的監聽器工作。 – dythe 2012-07-22 01:57:52

+0

在if子句之外,您應該只進行一次初始化。你不需要準備好的監聽器。 – Erol 2012-07-22 02:09:12

+0

是的,我改變了。在帖子中更新了代碼和Logcat,不知道爲什麼它仍然不起作用 – dythe 2012-07-22 02:14:37

0

你不能啓動媒體播放器,直到它準備好條件,你不能暫停的媒體播放器,如果它不是玩:)

所以,你必須使你的修訂代碼或者你可能想看看一個很好的教程

Dealing with the Android Media Player

這一定會幫助你。

謝謝:)

相關問題