android studio在XML布局文件中定义一个MediaPlayer控件和一些控件按钮,如播放、暂停、停止和进度条等等
时间: 2023-07-16 11:16:05 浏览: 144
可以在XML布局文件中使用MediaPlayer控件和一些控件按钮,如播放、暂停、停止和进度条等等,实现音频播放功能,具体步骤如下:
1. 在XML布局文件中添加一个MediaPlayer控件和一些控件按钮。
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:indeterminate="false"
android:max="100" />
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Play"
android:layout_alignParentLeft="true"
android:layout_below="@+id/progressBar"/>
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/progressBar"/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Stop"
android:layout_alignParentRight="true"
android:layout_below="@+id/progressBar"/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:max="100"
android:progress="0"
android:layout_below="@+id/btn_play"
android:progressDrawable="@drawable/progress_drawable"
android:thumb="@drawable/progress_thumb" />
<MediaPlayer
android:id="@+id/media_player"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
```
2. 在Java代码中实例化MediaPlayer控件和控件按钮,并设置监听器。
```
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MediaPlayer mediaPlayer;
private Button btnPlay, btnPause, btnStop;
private SeekBar seekBar;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio);
seekBar = (SeekBar) findViewById(R.id.seekbar);
btnPlay = (Button) findViewById(R.id.btn_play);
btnPause = (Button) findViewById(R.id.btn_pause);
btnStop = (Button) findViewById(R.id.btn_stop);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnStop.setOnClickListener(this);
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
seekBar.setSecondaryProgress(i);
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
mediaPlayer.seekTo(i * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
updateSeekBar();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.btn_pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.btn_stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio);
seekBar.setProgress(0);
updateSeekBar();
}
break;
}
}
private void updateSeekBar() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() / 1000);
handler.postDelayed(runnable, 1000);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
```
3. 实现音频播放功能,即在相应的按钮点击事件中调用MediaPlayer的相应方法,如start()、pause()和stop()等方法。
以上是实现在XML布局文件中定义一个MediaPlayer控件和一些控件按钮,如播放、暂停、停止和进度条等等的步骤。
阅读全文