public class AudioPlay extends Activity {
private MediaPlayer mediaPlayer;
private Button playButton;
private int currentPosition;
private SeekBar seekBar;
Boolean mWorking;
private static final String TAG = "MediaActivity";
String path = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_paly);
seekBar = (SeekBar) findViewById(R.id.playSeekBar);
playButton = (Button) findViewById(R.id.playButton);
path = getIntent().getStringExtra("path");
mediaPlayer = new MediaPlayer();
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
clearData();
}
} else {
playMusic(path);
}
}
});
// 监听播放完成
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
clearData();
}
});
// 监听触摸
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
// 触摸的时候,做好标记,
public void onStartTrackingTouch(SeekBar seekBar) {
mWorking = false;
updateSeekBar();
}
// 触摸结束
public void onStopTrackingTouch(SeekBar seekBar) {
if(mediaPlayer.isPlaying()){
mWorking = true;
mediaPlayer.seekTo(seekBar.getProgress());// 通知触摸位置到播放位置
updateSeekBar();
}else {
clearData();
}
}
});
}
/**
* 播放音乐
*
* @param path 播放文件的路径
*/
public void playMusic(String path) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.setLooping(false);// 循环播放
mediaPlayer.prepare();
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
playButton.setText("播放中");
updateSeekBar();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 中途停止,或者播放完毕清空相关数据
*/
public void clearData() {
// 延迟
Handler handler=new Handler();
Runnable runnable=new Runnable() {
@Override
public void run() {
currentPosition=0;
seekBar.setProgress(currentPosition);
}
};
handler.postDelayed(runnable, 300);
mWorking=false;// 停止计时
playButton.setText("开始");
}
/**
* 更新SeekBar
*/
private void updateSeekBar() {
mWorking = true;
final int duration = mediaPlayer.getDuration();
new Thread() {
@Override
public void run() {
while (mWorking) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mediaPlayer == null) return;
int currentPosition = mediaPlayer.getCurrentPosition();
//发送数据给ui
Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putInt("currentPosition", currentPosition);
message.setData(bundle);
handler.sendMessage(message);
}
}
}.start();
}
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
//int duration = data.getInt("duration");
int currentPosition = data.getInt("currentPosition");
seekBar.setProgress(currentPosition);
}
};
@Override
protected void onDestroy() {
mediaPlayer.stop();
clearData();
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
}
// 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="音乐播放器" />
<EditText
android:id="@+id/mediaFileName"
android:layout_width="match_parent"
android:text="播放.mp3"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<LinearLayout
android:id="@+id/linearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<SeekBar
android:id="@+id/playSeekBar"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:maxHeight="2dp"
android:minHeight="2dp"
android:paddingBottom="3dp"
android:paddingLeft="12dp"
android:max="200"
android:paddingRight="12dp"
android:paddingTop="3dp"
android:progressDrawable="@drawable/layer_progress"
android:thumb="@drawable/shape_circle" />
</LinearLayout>
</LinearLayout>
//layer_progress
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="3dp" />
<solid android:color="#ECF0F1" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="3dp" />
<solid android:color="#C6CACE" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="3dp" />
<solid android:color="#16BC5C" />
</shape>
</clip>
</item>
</layer-list>
//shape_circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- solid表示远的填充色 -->
<solid android:color="#16BC5C" />
<!-- stroke则代表远的边框线 -->
<stroke
android:width="1dp"
android:color="#16BC5C" />
<!-- size控制高宽 -->
<size
android:height="20dp"
android:width="20dp" />
</shape>