准备 MP3 媒体播放器时无法显示 ProgressDialog。

3
我是一名有帮助的助手,以下是您需要翻译的内容:

我有一个媒体播放器,可以从URL播放mp3。我想在音频从URL加载时显示进度对话框。这是我的代码...我是新手,如果您无法很好地理解我的问题,请不要介意。提前致谢

播放功能

public void  playSong(int naatindex){
    // Play song
    tv = (TextView) this.findViewById(R.id.mywidget);  
    tv.setText("Buffering....");
    tv.setSelected(true); 

    final ProgressDialog progressDialog = ProgressDialog.show(this, 
            "Loading Title", "Loading Message");

        mp.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
       if (progressDialog != null && progressDialog.isShowing()){
          progressDialog.dismiss();
       }
        mp.start();
    }
    });
    try {

        mp.reset();
        mp.setDataSource(naatpaths[naatindex]);
        tv = (TextView) this.findViewById(R.id.mywidget);  
        tv.setSelected(true);

        mp.prepare();

         // Set focus to the textview
        tv.setText(naattitles[naatindex]);
        // Changing Button Image to pause image
        btnPlay.setImageResource(R.drawable.btn_pause);
        // set Progress bar values
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        // Updating progress bar
        updateProgressBar();            
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
1个回答

1

您需要使用prepareAsync();并创建另一个文本视图,其文本为缓冲。在prepareAsync()可见的缓冲文本视图上,并在准备完成列表上隐藏缓冲文本视图。我希望这能够起作用。

public void  playSong(final int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);  
bf = (TextView) this.findViewById(R.id.showbuffering);


try {

mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv.setVisibility(View.INVISIBLE);
bf.setVisibility(View.VISIBLE);   
mp.prepareAsync();



mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp){
 bf.setVisibility(View.INVISIBLE);
 tv.setVisibility(View.VISIBLE);
 tv.setSelected(true);
 mp.start();

  tv.setText(naattitles[naatindex]);
        tv.setSelected(true);
        // Changing Button Image to pause image
  btnPlay.setImageResource(R.drawable.btn_pause);
  // set Progress bar values
  songProgressBar.setProgress(o);
  songProgressBar.setMax(100);
  // Updating progress bar
  updateProgressBar(); 




}
  });

      // Set focus to the textview

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

谢谢,这对我有用。非常感谢! - Muhammad Irfan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接