2013-08-27 45 views
0

在我的應用程序中,我將從URL播放音樂。所以它還需要一段時間才能播放音頻,因爲它是通過流媒體播放的。 我想在等待MediaPlayer播放/啓動時實現ProgressDialog。Android - MediaPlayer流播放之前播放進度對話框

這裏是我的播放音頻方法: 公共無效playMedia(字符串songIndex){

  ProgressDialog pDialog; 
      pDialog = new ProgressDialog(context); 
      pDialog.setMessage("Loading. . . "); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

      Uri songUri = Uri.parse(songIndex); 
        try { 
         mp.setDataSource(context, songUri); 
         mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
         mp.prepare(); 

        } catch (IllegalArgumentException e) { 
         e.printStackTrace(); 
        } catch (IllegalStateException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        }finally{ 
         pDialog.dismiss(); 
        } 

        if(mp!=null) 
         mp.start(); 
     } 

這是我怎樣努力實現它。 但它沒有奏效。 我有例外情況是這樣的:

 08-27 19:48:13.896: E/InputEventReceiver(8798): Exception dispatching input event. 
     08-27 19:48:13.896: E/MessageQueue-JNI(8798): Exception in MessageQueue callback: handleReceiveCallback 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewRootImpl.setView(ViewRootImpl.java:599) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.app.Dialog.show(Dialog.java:285) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at com.app.adapters.MyRowAdapter.playMedia(MyRowAdapter.java:126) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at com.app.adapters.MyRowAdapter$2$4.onTouch(MyRowAdapter.java:522) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.View.dispatchTouchEvent(View.java:7185) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280) 
     08-27 19:48:13.906: E/MessageQueue-JNI(8798): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023) 

是否有實現這個別的辦法嗎?我應該在哪裏放這個? 在此先感謝。

回答

0

如果您引用了getApplicationContext(),則此處使用的上下文對象pDialog = new ProgressDialog(context);將不起作用。

而是嘗試,活動上下文這樣的替換它,

pDialog = new ProgressDialog(ActivityName.this); 
+0

但這並不是一項活動中發生了.. 這是在適配器.. – sftdev

+0

那麼你可以傳遞一個上下文對象從活動.. –

+0

我試過了,但它確實顯示progressdialog – sftdev

0
Use Async Task: 

    private class MediaPlayer extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
        Uri songUri = Uri.parse(songIndex); 
         try { 
          mp.setDataSource(context, songUri); 
          mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
          mp.prepare(); 

         } catch (IllegalArgumentException e) { 
          e.printStackTrace(); 
         } catch (IllegalStateException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
return mp; 
      }   

      @Override 
      protected void onPostExecute(String result) {    
      } 

      @Override 
      protected void onPreExecute() { 
    ProgressDialog pDialog; 
       pDialog = new ProgressDialog(context); 
       pDialog.setMessage("Uploading file. . . "); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      @Override 
      protected void onProgressUpdate(Void... values) { 
      } 
     } 
+0

ooops ..我的壞.. 它實際上並沒有上傳。該消息是假設只是加載 – sftdev

+0

它只會顯示對話框,直到你沒有得到下載,或者你可以設置一些時間在哪個對話框將被看到 –

+0

yah yah。但我擔心的是在停止媒體播放器時使用它。 這是發生在單獨的線程權 – sftdev