我有什么: 我有一个媒体播放器代码,可以完美地缓存
下面的链接< R.string.testsong_20_sec
> 链接 :: Its a song hosted in google drive
SingleSongBuffering.java
public class SingleSongBuffering extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_song_buffering);
initView();
}
/** This method initialise all the views in project*/
private void initView() {
buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(this);
seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
editTextSongURL.setText(R.string.testsong_20_sec);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
/** Method which updates the SeekBar primary progress by current song playing position*/
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.ButtonTestPlayPause){
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
try {
mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
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.
} catch (Exception e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
buttonPlayPause.setImageResource(R.drawable.button_pause);
}else {
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.SeekBarTestPlay){
/** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
if(mediaPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
mediaPlayer.seekTo(playPositionInMillisecconds);
}
}
return false;
}
@Override
public void onCompletion(MediaPlayer mp) {
/** MediaPlayer onCompletion event handler. OMethod which calls then song playing is complete*/
buttonPlayPause.setImageResource(R.drawable.button_play);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
/** Method which updates the SeekBar secondary progress by current song loading from URL position*/
seekBarProgress.setSecondaryProgress(percent);
}
}
工作原理: 我能够播放歌曲并播放/暂停歌曲
什么不行: 进度条部分不起作用! 1.进度条没有从开头本身在进度线的末尾更新它
我想要的是什么: 进度条部分工作,以便我可以拖动进度,以便从该点播放歌曲
如何解决这个问题?
答案 0 :(得分:0)
进度条部分无效! 1.进度条没有更新它 从一开始就在进度线的末尾
**测试2到3个网址后。
如果分配给MediaPlayer数据源的曲目(网址)未提供曲目信息,缓冲区列表器将不会给出百分比。
您可以测试它:在设置数据源(url)进行流式传输后,一旦mediaplyer开始付费检查,媒体播放器trackDuration(mPlayer.getDuration)是否给出该轨道的正确持续时间?
因为如果轨道持续时间不正确,缓冲水平也将不正确。
所以问题可能来自轨道而不是进度条
在下面的代码中,一个网址带有跟踪详细信息,一旦玩家准备好并开始播放,就会提供跟踪duraiton(在onPrepared(MediaPlayer mp)
中查看)
并且没有提供跟踪信息的网址。
检查代码中给出的注释
希望我能正确地回答你的问题。如果我没有,请告诉我..
private MediaPlayer mPlayer;
private void playMusic() {
try {
mPlayer = new MediaPlayer();
// For testing purpose added below url
/**
* Its not like mp3 or particular coded url will return duration and track info.
* streaming url should also provide details.
*
* mPlayer.setDataSource("http://50.117.115.210/");
*
* Url which is not providing track details and other information,so
* here we are not getting track duration and bcz of that buffering
* update will not be correct.
*
* */
/**
* mPlayer.setDataSource("http://www.samisite.com/sound/cropShadesofGrayMonkees.mp3");
* This is providing correct details of the track which is going to play.
*
*/
mPlayer.setDataSource("http://www.samisite.com/sound/cropShadesofGrayMonkees.mp3");
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Media player will required Track's information to give
// you buffering percentage. So once check after starting
// player are you getting track duration from
// MediaPlayer.getDuration()
Log.e("onBufferingUpdate", "" + percent);
seekBar.setSecondaryProgress(percent);
// *****
// Buffering update will be correct if mp.getDuration() > 0
// double ratio = percent / 100.0;
// int bufferingpercent = (int) (mp.getDuration() * ratio);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private OnSeekBarChangeListener seekbarListner = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
if (mPlayer != null) {
if (progress <= seekBar.getSecondaryProgress()) {
} else {
seekBar.setProgress(progress);
}
}
}
}
};