📅  最后修改于: 2023-12-03 15:13:20.467000             🧑  作者: Mango
Android Media Player示例是一个使用Android的MediaPlayer类实现基本音频播放的示例程序。它可以帮助程序员学习如何使用Android的MediaPlayer类来播放音频文件。
Android Media Player示例实现了以下功能:
使用MediaPlayer类实现基本音频播放功能可分为以下几个步骤:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(audioFilePath);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.pause(); // 暂停播放
mediaPlayer.stop(); // 停止播放
mediaPlayer.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(MediaPlayer mp, int current, int total) {
// 更新UI界面显示当前播放进度
}
});
以下是Android Media Player示例的核心代码:
public class MediaPlayerActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private TextView tvCurrentPosition, tvTotalDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
seekBar = findViewById(R.id.seekBar);
tvCurrentPosition = findViewById(R.id.tv_current_position);
tvTotalDuration = findViewById(R.id.tv_total_duration);
// 创建MediaPlayer实例
mediaPlayer = new MediaPlayer();
// 设置音频源
mediaPlayer.setDataSource(getExternalCacheDir() + "/sample.mp3");
// 准备播放
mediaPlayer.prepareAsync();
// 开始播放
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
tvTotalDuration.setText(String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(mediaPlayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds(mediaPlayer.getDuration()) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(mediaPlayer.getDuration()))));
}
});
// 实现控制功能
findViewById(R.id.btn_play).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
}
});
findViewById(R.id.btn_pause).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
}
});
// 实现音频进度回调
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
seekBar.setProgress(0);
}
});
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(MediaPlayerActivity.this, "Error occurred while playing audio.", Toast.LENGTH_SHORT).show();
return false;
}
});
mediaPlayer.setOnProgressListener(new MediaPlayer.OnProgressListener() {
@Override
public void onProgress(MediaPlayer mp, int current, int total) {
seekBar.setProgress(current);
tvCurrentPosition.setText(String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(current),
TimeUnit.MILLISECONDS.toSeconds(current) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(current))));
}
});
// 实现SeekBar的拖动事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
}
Android Media Player示例是一个简单而有用的示例程序,它可以帮助Android开发者学习使用MediaPlayer类来实现基本音频播放功能。通过仔细学习这个示例程序的实现方法,我们可以更好地掌握Android音频播放相关知识,进而在自己的应用中应用它们。