媒体播放器准备时的进度条
我试图弄清楚如何在我的媒体播放器准备流文件时显示“正在加载。请稍候...”的进度条。现在发生的事情是在歌曲准备好后显示。我该如何解决这个问题?
mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
/*dubstep stream*/
try {
dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
dubstepMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dubstepMediaPlayer.start();
if(dubstepMediaPlayer.isPlaying()){
mediaPlayerLoadingBar.dismiss();
}`
编辑: 这是我现在的代码:
`switch(pSelection){ 案例1:
new AsyncTask<Void, Void, Void>(){
@Override
protected void onPreExecute(){
mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
try {
dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
//mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
return null;
}
protected void onPostExecute(Void result){
//mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true)
dubstepMediaPlayer.prepareAsync();
dubstepMediaPlayer.start();
mediaPlayerLoadingBar.dismiss();
}
}.execute();`
I am trying to figure out how to have a progress bar that says "Loading. Please Wait..." while my media player prepares a streaming file. What occurs now is that it displays after the song is prepared. how can i fix this?
mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
/*dubstep stream*/
try {
dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
dubstepMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dubstepMediaPlayer.start();
if(dubstepMediaPlayer.isPlaying()){
mediaPlayerLoadingBar.dismiss();
}`
EDIT:
This is the code I have now:
`switch(pSelection){
case 1:
new AsyncTask<Void, Void, Void>(){
@Override
protected void onPreExecute(){
mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
try {
dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
//mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
return null;
}
protected void onPostExecute(Void result){
//mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true)
dubstepMediaPlayer.prepareAsync();
dubstepMediaPlayer.start();
mediaPlayerLoadingBar.dismiss();
}
}.execute();`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果有人仍然面临这个问题,这里是下面的代码
If someone Still facing this problem here is the code below
问题在于您没有在这里异步执行任何操作,而您应该这样做。您应该使用 AsyncTask 来完成您的工作。
看一下“4 个步骤”,详细信息如下:
编辑:
您可以创建一个匿名内部类来执行您的命令,这可能类似于您创建 onClick 处理程序的方式。在你的 onClick 中做这样的事情:
不要忘记在这里关注你的泛型!
The issue lies in that you are not doing anything asynchronously here, and you should be. You should use an AsyncTask to do your work.
Take a look at 'the 4 steps', as detailed here:
EDIT:
You can create an anonymous inner class to do your bidding, which may be similar to how you are creating your onClick handler. In your onClick do something like this:
Don't forget to keep an eye on your generics here!
这是活动课。这里我只是指路。
这是媒体播放器类。
这是 main.xml
Here is the activity class.Here i am showing the way only.
Here is mediaplayerclass.
And here is the main.xml
当您在底层
mediaplayer
对象上说prepare
时,它在内部确实做了一些准备工作,例如 - 设置文件提取器、设置音频解码器来解码编码的音频文件并设置音频接收器来播放从解码器解码的原始音频数据。现在这一切都需要时间,它不是瞬间的。因此,在您的原始代码中,您检查媒体播放器是否正在播放,然后将其关闭,但问题是此时媒体播放器尚未播放音频,因此您的关闭永远不会被调用,因此它总是可见的。
您需要做的是实现侦听器
MediaPlayer.OnPreparedListener
,并在应用程序中调用方法onPrepared
时调用解雇mediaPlayerLoadingBar.dismiss(); 在该方法中。
When you say
prepare
on the underlyingmediaplayer
object, internally it really does some preparation like - setting up the extractor for the file, setting up the audio decoder to decode the encoded audio file and setting up the audio sink to play the raw audio data that was decoded from the decoder. Now all this will take time, it is not instantaneous.So in your original code, you check if the mediaplayer
isPlaying
and then dismiss it but the problem is at that point of time the mediaplayer is not playing the audio yet and thus your dismiss is never called so it always visible.What you need to do is implement the listener
MediaPlayer.OnPreparedListener
and when the methodonPrepared
is called in your application call the dismissmediaPlayerLoadingBar.dismiss();
in that method.这是我的解决方案:
prepareAsync 函数用于准备音频,它是一个非阻塞操作(它不会阻塞应用程序的主线程)。
然后我使用回调 setOnPreparedListener 在
prepareAsync返回,音频准备好
Here is my solution :
The prepareAsync function used to prepare the audio and it is a non blocking operation (it is not blocking the main thread of the app).
Then I used the callback setOnPreparedListener to get notified when the
prepareAsync return, and the audio is ready
终于发现:
伙计们非常小鬼点:
设置 url 后,
我添加了prepare_sync() 并添加了处理程序,因为它因大文件而崩溃
此代码也会添加进度条。
要点
Progressbar
mp.start-done
这是我的做法,顺便说一句,我听说还有替代的 Exoplayer 也可以看看!
Finally found out:
Guys very Imp point:
After setting url
I have added prepare_sync() and added handler cause it was crashing for big files
This code will add progressbar too.
Points:
Progressbar
mp.start-done
This is my way of doing btw I have heard there is alternative Exoplayer have a look at that too !