0

我嘗試了各種方法,但沒有解決我的問題。希望這裏有人能幫助我。 簡單的要求是我想顯示一個微調框,直到顯示媒體播放器窗口。這聽起來很容易,但事實並非如此。直到媒體播放器準備好才能顯示微調

我打電話給我的mediaplayer類「聽」單擊alertDialog。它是:

alertBox.setCancelable(false) 
    .setNegativeButton("Listen", new DialogInterface.OnClickListener(){ 
    public void onClick(DialogInterface dialog,int id){ 
dialog.dismiss(); 
emp = new EasyMediaPlayer(mp3PopupLayout,buttonPlayPause,seekBarProgress,tv_mp3,downloadURL); 
emp.startPlayingMP3();   
} 
}).show(); 

而startPlaying函數調用chooseToStart(true),它反過來運行媒體播放器。此類是:

public class EasyMediaPlayer implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ 
EasyMediaPlayer(View mp3PopupLayout,ImageView buttonPlayPause,SeekBar seekBarProgress,TextView tv_mp3, String MP3URL){ 
     this.mp3PopupLayout = mp3PopupLayout; 
     this.buttonPlayPause = buttonPlayPause; 
     this.seekBarProgress = seekBarProgress; 
     this.tv_mp3 = tv_mp3; 
     this.MP3URL = MP3URL; 
     seekBarProgress.setOnTouchListener(this); 
     buttonPlayPause.setOnClickListener(this); 
    } 

    public void startPlayingMP3(){ 
     mediaFileLengthInMilliseconds = 1; 
     tv_mp3.setText(""); 
     buttonPlayPause.setImageResource(R.drawable.button_play); 
     buttonPlayPause.setVisibility(View.GONE); 
     seekBarProgress.setProgress(0); 
     seekBarProgress.setSecondaryProgress(0); 
     mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setOnBufferingUpdateListener(this); 
     mediaPlayer.setOnCompletionListener(this); 
     mediaPlayer.setOnErrorListener(new OnErrorListener() { 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       tv_mp3.setText("Error in playing file !!"); 
       return true; 
      } 
     }); 
     mp3DownloadWindow = new PopupWindow(mp3PopupLayout, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, true); 
     mp3DownloadWindow.showAtLocation(mp3PopupLayout, Gravity.BOTTOM, 30, 0); 
     chooseToStart(true); 
    } 

    public void chooseToStart(boolean startFlag){ 
     if(startFlag){ 
      try { 
       mediaPlayer.setDataSource(this.MP3URL); 
       mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
       mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL 
      } catch (Exception e) { 
       tv_mp3.setText(e.toString() + "\nClose it"); 
      } 

      if(!mediaPlayer.isPlaying()){ 
       spinner.cancel(); 
       mediaPlayer.start(); 
       buttonPlayPause.setImageResource(R.drawable.button_pause); 
      }else { 
       mediaPlayer.pause(); 
       buttonPlayPause.setImageResource(R.drawable.button_play); 
      } 
      primarySeekBarProgressUpdater(); 
     } 
    } 
//other stuff 
} 

我需要知道我在哪裏以及如何使用微調器,以便在媒體播放器開始播放之前顯示它。

任何幫助將不勝感激。謝謝

回答

0

使用prepareAsync()setOnPreparedListener()而不是prepare()

mediaPlayer.setDataSource(this.MP3URL); 
mediaPlayer.setOnPreparedListener(new OnPreparedListener() { 
    @Override 
    public void onPrepared(MediaPlayer mp) { 
     // Hide whatever you need, mediaPlayer is prepared 
     mp.start(); 
    } 
}); 
// Show whatever you need, mediaPlayer is preparing 
mediaPlayer.prepareAsync(); 
+1

謝謝。我用prepareAsync()完成了它; –