通过搜索栏更改流式音频的位置后,如何确定音频是否已准备好播放?
How to find out if audio is ready to play after changing position of streaming audio by seekbar?
我使用 MediaPlayer
从 url 流式传输音频。一切正常。但是当用户更改 seekbar
以更改播放音频的位置时,短时间需要很长时间来准备音频到 play.after 用户更改搜索栏位置,我显示一个 ProgressBar。我想要什么像听众一样找出播放器正在播放并隐藏 ProgressBar
我的代码是:
public class TestActivity extends AppCompatActivity {
private ImageButton podcastBtn;
private SeekBar podcastSeek;
private TextView podcastFullTime;
private TextView podcastCurrentTime;
private ProgressBar podcastPrg;
private MediaPlayer mediaPlayer;
private boolean podcastInitialized;
private String myUrl = "http://www.archive.org/download/AmericanStories/The_Boarded_Window_-_By_Ambrose_Bierce.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_detail);
findViews();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@SuppressLint("DefaultLocale")
@Override
public void onPrepared(final MediaPlayer mp) {
int audioLength = mp.getDuration();
podcastSeek.setMax(audioLength);
podcastFullTime.setText(String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(audioLength),
TimeUnit.MILLISECONDS.toSeconds(audioLength) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(audioLength))
));
new Thread(new Runnable() {
@Override
public void run() {
while (mp.getCurrentPosition() < mp.getDuration()) {
final int millis = mp.getCurrentPosition();
runOnUiThread(new Runnable() {
@Override
public void run() {
podcastSeek.setProgress(mp.getCurrentPosition());
podcastCurrentTime.setText(String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
));
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
podcastBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
if (!podcastInitialized) {
new AudioPlayerAsync().execute(myUrl);
} else {
mediaPlayer.start();
podcastBtn.setImageResource(R.drawable.pause_icon);
}
} else {
mediaPlayer.pause();
podcastBtn.setImageResource(R.drawable.play_icon);
}
}
});
podcastSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser && podcastInitialized) {
mediaPlayer.seekTo(progress);
podcastPrg.setVisibility(View.VISIBLE);
podcastBtn.setVisibility(View.INVISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
class AudioPlayerAsync extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
podcastPrg.setVisibility(View.VISIBLE);
podcastBtn.setVisibility(View.INVISIBLE);
}
@Override
protected Void doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
podcastInitialized = false;
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
podcastPrg.setVisibility(View.GONE);
podcastBtn.setVisibility(View.VISIBLE);
podcastBtn.setImageResource(R.drawable.pause_icon);
mediaPlayer.start();
podcastInitialized = true;
}
}
@Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
@Override
protected void onResume() {
super.onResume();
if (podcastInitialized) {
mediaPlayer.start();
}
}
@Override
public void onBackPressed() {
mediaPlayer.stop();
finish();
super.onBackPressed();
}
}
我找到了sulotion:
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
float currentPlayingPercent = (100 * mp.getCurrentPosition() / (float)mp.getDuration());
Log.i("tag", "buffer percent: " + percent);
Log.i("tag", "media player percent: " + currentPlayingPercent);
if (podcastPrg.getVisibility() == View.VISIBLE && percent > currentPlayingPercent) {
podcastPrg.setVisibility(View.GONE);
podcastBtn.setVisibility(View.VISIBLE);
}
}
});
我使用 MediaPlayer
从 url 流式传输音频。一切正常。但是当用户更改 seekbar
以更改播放音频的位置时,短时间需要很长时间来准备音频到 play.after 用户更改搜索栏位置,我显示一个 ProgressBar。我想要什么像听众一样找出播放器正在播放并隐藏 ProgressBar
我的代码是:
public class TestActivity extends AppCompatActivity {
private ImageButton podcastBtn;
private SeekBar podcastSeek;
private TextView podcastFullTime;
private TextView podcastCurrentTime;
private ProgressBar podcastPrg;
private MediaPlayer mediaPlayer;
private boolean podcastInitialized;
private String myUrl = "http://www.archive.org/download/AmericanStories/The_Boarded_Window_-_By_Ambrose_Bierce.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_detail);
findViews();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@SuppressLint("DefaultLocale")
@Override
public void onPrepared(final MediaPlayer mp) {
int audioLength = mp.getDuration();
podcastSeek.setMax(audioLength);
podcastFullTime.setText(String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(audioLength),
TimeUnit.MILLISECONDS.toSeconds(audioLength) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(audioLength))
));
new Thread(new Runnable() {
@Override
public void run() {
while (mp.getCurrentPosition() < mp.getDuration()) {
final int millis = mp.getCurrentPosition();
runOnUiThread(new Runnable() {
@Override
public void run() {
podcastSeek.setProgress(mp.getCurrentPosition());
podcastCurrentTime.setText(String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
));
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
podcastBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
if (!podcastInitialized) {
new AudioPlayerAsync().execute(myUrl);
} else {
mediaPlayer.start();
podcastBtn.setImageResource(R.drawable.pause_icon);
}
} else {
mediaPlayer.pause();
podcastBtn.setImageResource(R.drawable.play_icon);
}
}
});
podcastSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser && podcastInitialized) {
mediaPlayer.seekTo(progress);
podcastPrg.setVisibility(View.VISIBLE);
podcastBtn.setVisibility(View.INVISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
class AudioPlayerAsync extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
podcastPrg.setVisibility(View.VISIBLE);
podcastBtn.setVisibility(View.INVISIBLE);
}
@Override
protected Void doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
podcastInitialized = false;
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
podcastPrg.setVisibility(View.GONE);
podcastBtn.setVisibility(View.VISIBLE);
podcastBtn.setImageResource(R.drawable.pause_icon);
mediaPlayer.start();
podcastInitialized = true;
}
}
@Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
@Override
protected void onResume() {
super.onResume();
if (podcastInitialized) {
mediaPlayer.start();
}
}
@Override
public void onBackPressed() {
mediaPlayer.stop();
finish();
super.onBackPressed();
}
}
我找到了sulotion:
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
float currentPlayingPercent = (100 * mp.getCurrentPosition() / (float)mp.getDuration());
Log.i("tag", "buffer percent: " + percent);
Log.i("tag", "media player percent: " + currentPlayingPercent);
if (podcastPrg.getVisibility() == View.VISIBLE && percent > currentPlayingPercent) {
podcastPrg.setVisibility(View.GONE);
podcastBtn.setVisibility(View.VISIBLE);
}
}
});