[英]Android - MediaPlayer streaming displaying a Progress Dialog before playing
在我的應用程序中,我將從URL播放音樂。 因此,由於是通過流傳輸,因此仍需要花費一些時間來播放音頻。 我想在等待MediaPlayer
播放/啟動時實現一個ProgressDialog。
這是我播放音頻的方法:public void playMedia(String 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)
還有其他方法可以實現嗎? 像我應該把它放在哪里? 提前致謝。
您在此處使用的上下文對象pDialog = new ProgressDialog(context);
如果已引用getApplicationContext(),則將無法正常工作。
而是嘗試將其替換為“活動上下文”,
pDialog = new ProgressDialog(ActivityName.this);
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) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.