[英]Android - MediaPlayer streaming displaying a Progress Dialog before playing
In my application, I will playing music from URL. 在我的应用程序中,我将从URL播放音乐。 So it would still take some time to play the audio since it is through streaming. 因此,由于是通过流传输,因此仍需要花费一些时间来播放音频。 I wanted to implement a ProgressDialog while waiting for the MediaPlayer
to play/start. 我想在等待MediaPlayer
播放/启动时实现一个ProgressDialog。
Here is my method for playing the audio: public void playMedia(String songIndex) { 这是我播放音频的方法: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();
}
This is how I tried to implement it. 这就是我尝试实现它的方式。 But it did not work. 但这没有用。 And i have Exceptions like this: 而且我有这样的例外:
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)
Are there other ways to implement this? 还有其他方法可以实现吗? Like where should i put this one? 像我应该把它放在哪里? Thanks in advance. 提前致谢。
The context object you have used here, pDialog = new ProgressDialog(context);
您在此处使用的上下文对象pDialog = new ProgressDialog(context);
will not work if you have referred to getApplicationContext(). 如果已引用getApplicationContext(),则将无法正常工作。
Instead try replacing it with, Activity Context like this, 而是尝试将其替换为“活动上下文”,
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.