在android下实现音乐播放器
音乐播放器步骤:
定义一个接口,接口里面定义自己想实现的方法
定义一个中间类继承服务并去实现该接口
然后返回该中间类的对象,目的是为了能够在MainActivity中获取到该对象,调用到服务中的方法
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="播放" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="暂停" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="继续播放" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
首先定义一个接口
public interface Iservice {
public void callPlayMusic();
public void callPauseMusic();
public void callRePalyMusic();
public void callseetToPositin(int position);
}
其次,用一个类继承服务
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
private Timer timer;
private TimerTask task;
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
@Override
public void onCreate() {
//构造mediaplayer 这个类用于播放音频和视频
mediaPlayer = new MediaPlayer();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
// 播放音乐的方法
public void playMusic() {
System.out.println("playMusic");
//开始播放
try {
mediaPlayer.setDataSource("/mnt/sdcard/xpg.mp3");
//[3]准备开始播放
mediaPlayer.prepare();
mediaPlayer.start();
updateSeekBar();
} catch (Exception e) {
e.printStackTrace();
}
//
}
//更新进度条的逻辑
private void updateSeekBar() {
//获取当前歌曲的总时长
final int duration = mediaPlayer.getDuration();
//获取当前的进度 timer 时时获取当前歌曲播放的进度
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
int currentPosition = mediaPlayer.getCurrentPosition();//获取当前歌曲的进度
//通过使用handler发消息的方式把数据提交到mainActivity
Message msg = Message.obtain();
Bundle bundle = new Bundle(); //就是map
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
//通过handelr发消息
MainActivity.handler.sendMessage(msg);
}
};
timer.schedule(task, 1000, 1000);
//当前歌曲播放完成的时候 取消timer 和 task
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
System.out.println("歌曲播放完成了");
timer.cancel();
task.cancel();
}
});
}
//这个方法实现播放指定歌曲的位置
public void seetToPositin(int position){
mediaPlayer.seekTo(position);
}
// 暂停音乐的方法
public void PauseMusic() {
mediaPlayer.pause();
System.out.println("PauseMusic");
}
// 继续播放音乐的方法
public void RePalyMusic() {
//如果暂停过就是继续上次的位置播放
mediaPlayer.start();
System.out.println("RePalyMusic");
}
//定义一个中间对象
private class MyBinder extends Binder implements Iservice{
@Override
public void callPlayMusic() {
playMusic();
}
@Override
public void callPauseMusic() {
PauseMusic();
}
@Override
public void callRePalyMusic() {
RePalyMusic();
}
@Override
public void callseetToPositin(int position) {
seetToPositin(position);
}
}
}
最后,在MainActivity中实现
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.R.string;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private Iservice iservice;
private Myconn conn;
private static SeekBar seekBar;
public static Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//获取携带的数据
Bundle data = msg.getData();
int duration = data.getInt("duration");
int currentPosition = data.getInt("currentPosition");
//设置进度条条的进度
seekBar.setMax(duration);
seekBar.setProgress(currentPosition);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 调用startService方法保证服务在后台长期运行
Intent intent = new Intent(this, MusicService.class);
startService(intent);
// 使用bindservice 目的获取服务中定义好 的中间人对象
conn = new Myconn();
bindService(intent, conn, BIND_AUTO_CREATE);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
//给控件设置监听
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//完成拖动到哪就播放哪里
iservice.callseetToPositin(seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
}
// 点击按钮播放音乐
public void click1(View v) {
iservice.callPlayMusic();
}
// 点击按钮暂停音乐
public void click2(View v) {
iservice.callPauseMusic();
}
// 点击按钮继续播放
public void click3(View v) {
iservice.callRePalyMusic();
}
@Override
protected void onDestroy() {
// 解绑服务
unbindService(conn);
super.onDestroy();
}
// 定义服务监听类
private class Myconn implements ServiceConnection {
// 当连接服务成功的时候调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取我们定义的中间人对象
iservice = (Iservice) service;
}
// 失去连接
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}