笔记如下
播放音乐的代码
player = new MediaPlayer();
//设置播放的资源
player.setDataSource(path);
player.prepare();//准备播放
player.start();//开始播放
进度条跟随音乐播放滑动
skb_progress.setMax(player.getDuration());//获取音乐文件的时长
//实现进度条
//获得当前的进度
//更新UI
//获得当前的进度
//更新UI
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(player.toString());
while (player != null) {
if (!tracking) {
//获得当前的进度
int currentPosition = player.getCurrentPosition();
skb_progress.setProgress(currentPosition);//更新UI
}
}
}
}).start();
音乐暂停的方法
//暂停
public void pause(View v){
if (player != null && player.isPlaying()){
System.out.println(player.isPlaying());
player.pause();
((Button)v).setText("继续");
}else if(player != null){
player.start();
performProgress();
((Button)v).setText("暂停");
}
}
源码:
package com.chen.player_asynchronous;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private EditText et_path;
private SeekBar skb_progress;
private MediaPlayer player;
private boolean tracking = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
skb_progress = (SeekBar) findViewById(R.id.skb_progerss);
requestPermission();
skb_progress.setOnSeekBarChangeListener(this);
}
private void requestPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授权时
//进行授权
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}else{
//调用打电话的方法
Toast.makeText(this, "权限获取了...", Toast.LENGTH_SHORT).show();
}
}
//播放
public void play(View v){
tracking = false;
String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)){
return;
}
//播放音乐
if (player == null){
player = new MediaPlayer();
}
//重置播放器
player.reset();
try {
//可能会报错的异常
player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
System.out.println("what :" + what);
return false;
}
});
//设置播放的资源
player.setDataSource(path);
player.prepare();//准备播放
player.start();//开始播放
//进度条的方法
performProgress();
} catch (Exception e) {
e.printStackTrace();
}
}
private void performProgress() {
skb_progress.setMax(player.getDuration());//获取音乐文件的时长
//实现进度条
//获得当前的进度
//更新UI
//获得当前的进度
//更新UI
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(player.toString());
while (player != null) {
if (!tracking) {
//获得当前的进度
int currentPosition = player.getCurrentPosition();
skb_progress.setProgress(currentPosition);//更新UI
}
}
}
}).start();
}
//暂停
public void pause(View v){
if (player != null && player.isPlaying()){
System.out.println(player.isPlaying());
player.pause();
((Button)v).setText("继续");
}else if(player != null){
player.start();
performProgress();
((Button)v).setText("暂停");
}
}
//停止
public void stop(View v){
tracking = true;
if (player != null){
player.stop();
player.release();//释放资源
player = null;
}
}
//seekbar的移动监听事件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//开始拖动
tracking = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//结束拖动
tracking = false;
//播放到对应的位置
if (player != null){
player.seekTo(seekBar.getProgress());
}
}
}