您的位置:首页 > 其它

使用绑定式服务播放音乐

2016-01-25 09:35 435 查看
播放网络资源的方式。

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.net.Uri;

import android.os.Binder;

import android.os.IBinder;

import java.io.IOException;

/**

* Created by chen on 2016/1/20.

*/

public class MusicService extends Service {

private MediaPlayer player; //声明一个播放器

private boolean isStop = false; //判断 当前是否有音乐在播放

private Uri uri; // 音乐的播放地址

@Override

public IBinder onBind(Intent intent) {

// 使用绑定式服务,将音乐的地址发送到服务中。

MyBind bind = new MyBind();

String music = intent.getStringExtra("music");

uri = Uri.parse(music);

preparePlayer();

return bind;

}

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

if (player == null) {

player = new MediaPlayer();

// 播放完成时的监听事件

player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

mp.release();

}

});

player.setOnErrorListener(new MediaPlayer.OnErrorListener() {

// 播放失败的监听事件

@Override

public boolean onError(MediaPlayer mp, int what, int extra) {

// TODO Auto-generated method stub

mp.reset();

return false;

}

});

}

}

// 准备播放

private void preparePlayer() {

player.reset();

try {

player.setDataSource(this,uri);

player.prepare();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

// 返回服务本身。用来在Activity中使用

public class MyBind extends Binder {

public MusicService getInstance() {

return MusicService.this;

}

}

// 要进行播放的音乐的总时长

public int getDuration(){

return player.getDuration();

}

// 当前音乐播放的位置

public int getCurrent(){

if (player!=null) {

return player.getCurrentPosition();

}

return 0;

}

public void play() {

if (isStop) {

preparePlayer();

player.start();

isStop = false;

} else if (player != null && !player.isPlaying()) {

player.start();

}

}

public void pause() {

if (player != null && player.isPlaying()) {

player.pause();

}

}

public void stop() {

if (player != null) {

player.stop();

isStop = true;

player.release();

}

}

// 取消绑定

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

if (player != null) {

player.release();

player = null;

}

return super.onUnbind(intent);

}

}

完整的服务端代码如上,接下来是在Activity中的代码

// 显示意图,转至服务类

intent = new Intent(MainActivity.this, MusicService.class);

// 传值 ,将音乐播放的地址发送给服务端

intent.putExtra("music", music);

// 服务连接类

conn = new MyServiceConn();

// 绑定服务

bindService(intent, conn, Context.BIND_AUTO_CREATE);

public class MyServiceConn implements ServiceConnection {

// 服务连接的时候调用的方法

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// 相通过Ibinder对象获取Service对象

MusicService.MyBind service2=(MusicService.MyBind) service;

myService=service2.getInstance();

// 服务绑定成功 就可以直接进行音乐的播放操作

myService.play();

}

@Override

public void onServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: