要想实现播放,步骤为1播放音乐会使用服务来完成。我们把播放音乐所会使用的几个方法定义成接口所以我们启动服务时就必须使用bindservice来启动。应为我们需要返回IBinder对象这样我们就可以在activity里调用服务里的方法。我们这里使用seekbar来显示进度。以下是代码实现。
activity代码
public class MainActivity extends Activity {
static Handler handle = new Handler() {
public void handleMessage(android.os.Message msg) {
Bundle bundle = msg.getData();
int duration = bundle.getInt("duration");
int currentPostition = bundle.getInt("currentPosition");
System.out.println(currentPostition);
sb.setMax(duration);
sb.setProgress(currentPostition);
};
};
MyserviceConn conn;
MusicInterface mi;
Intent intent;
private static SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
sb = (SeekBar) findViewById(R.id.seekBar1);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int progress = seekBar.getProgress();
// 改变播放进度
mi.seekTo(progress);
System.out.println("sbsbsb");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// 根据拖动的进度改变音乐播放进度
}
});
intent = new Intent(this, MusicService.class);
startService(intent);
conn = new MyserviceConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void plays(View v) {
mi.play();
}
public void stops(View v) {
mi.pouse();
}
public void continues(View v) {
mi.continueplay();
}
public void exit(View v) {
unbindService(conn);
stopService(intent);
}
class MyserviceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mi = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}
service代码
public class MusicService extends Service {
MediaPlayer player;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new PlayMusic();
}
class PlayMusic extends Binder implements MusicInterface {
@Override
public void play() {
MusicService.this.play();
}
@Override
public void seekTo(int progress) {
MusicService.this.seekTo(progress);
}
@Override
public void pouse() {
// TODO Auto-generated method stub
MusicService.this.pause();
}
@Override
public void continueplay() {
// TODO Auto-generated method stub
MusicService.this.continuePlay();
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
player = new MediaPlayer();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 停止播放
player.stop();
// 释放占用的资源,此时player对象已经废掉了
player.release();
player = null;
if (timer != null) {
timer.cancel();
timer = null;
}
}
// 播放音乐
public void play() {
// 重置
player.reset();
try {
// 加载多媒体文件
player.setDataSource("http://192.168.1.104:8080/Meiko.mp3");
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
// 准备完毕时,此方法调用
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
addTimer();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 继续播放
public void continuePlay() {
player.start();
}
// 暂停播放
public void pause() {
player.pause();
}
public void seekTo(int progress) {
player.seekTo(progress);
}
public void addTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 获取歌曲总时长
int duration = player.getDuration();
// 获取歌曲当前播放进度
int currentPosition = player.getCurrentPosition();
Message msg = MainActivity.handle.obtainMessage();
// 把进度封装至消息对象中
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
MainActivity.handle.sendMessage(msg);
}
// 开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
}, 5, 500);
}
}
接口
public interface MusicInterface {
void play();
void pause();
void continuePlay();
void seekTo(int progress);
}
}
代码实现后效果
该博客介绍了一个在Android中使用服务(Service)和Binder实现音乐播放的功能。通过创建`MusicService`,利用`MediaPlayer`播放音乐,并通过`MusicInterface`接口与Activity交互,实现播放、暂停、继续播放和调整播放进度。同时,使用`SeekBar`展示音乐播放进度。


被折叠的 条评论
为什么被折叠?



