资源准备:
- 在res目录下创建raw文件夹,把音频文件拖进去
视图代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/btn_play" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停"
android:id="@+id/btn_pause" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:id="@+id/btn_stop" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/seek" />
</LinearLayout>
java代码:
package com.example.androidmedia26;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_play;
private Button btn_pause;
private Button btn_stop;
private SeekBar seek;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
if (msg.what == 0) {
int mediaProcess = mediaPlayer.getCurrentPosition();
seek.setProgress(mediaProcess);
handler.sendEmptyMessageDelayed(0, 1000);
}
}
};
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_play = findViewById(R.id.btn_play);
btn_pause = findViewById(R.id.btn_pause);
btn_stop = findViewById(R.id.btn_stop);
seek = findViewById(R.id.seek);
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
seek.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(this, R.raw.guoyuanchao);
seek.setMax(mediaPlayer.getDuration());
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int seekProcess = seekBar.getProgress();
mediaPlayer.seekTo(seekProcess);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
mediaPlayer.start();
handler.sendEmptyMessageDelayed(0, 1000);
}
break;
case R.id.btn_pause:
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.btn_stop:
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
效果: