#1.创建服务MusicPlayerService.java,基于此类改成IMusicPlayerService.aidl。
public class MusicPlayerService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
/**
* 根据位置打开一个音频并且播放
*
* @param position
*/
public void openAudio(int position) {
/**
* 开始播放音频
*/
public void start() {
}
/**
* 暂停
*/
public void pause() {
}
/**
* 得到歌曲的名称
*/
public String getAudioName() {
return "";
}
/**
* 得到歌曲演唱者的名字
*/
public String getArtistName() {
return "";
}
/**
* 得到歌曲的当前播放进度
*/
public int getCurrentPosition() {
return 0;
}
/**
* 得到歌曲的当前总进度
*/
public int getDuration() {
return 0;
}
/**
* 播放下一首歌曲
*/
public void next() {
}
/**
* 播放上一首歌曲
*/
public void pre() {
}
/**
* 得到播放模式
*/
public int getPlayMode() {
return 0;
}
/**
* 设置播放模式
*/
public void setPlayMode(int mode) {
}
}
#2.创建AIDL文件IMusicPlayerService.aidl,去掉方法体。
// IMusicPlayerService.aidl
package com.example.player;
// Declare any non-default types here with import statements
interface IMusicPlayerService {
/**
* 根据位置打开一个音频并且播放
* @param position
*/
void openAudio(int position);
/**
* 开始播放音频
*/
void start();
/**
* 暂停
*/
void pause();
/**
* 停止
*/
void stop();
/**
* 得到歌曲的名称
*/
String getAudioName();
/**
* 得到歌曲演唱者的名字
*/
String getArtistName();
/**
* 得到歌曲的当前播放进度
*/
int getCurrentPosition();
/**
* 得到歌曲的当前总进度
*/
int getDuration();
/**
* 播放下一首歌曲
*/
void next();
/**
* 播放上一首歌曲
*/
void pre();
/**
* 得到播放模式
*/
int getPlayMode();
/**
* 设置播放模式
*/
void setPlayMode(int mode);
}
#3.点击rebuild,在build目录下会生成一个名称为IMusicPlayerService.java的文件
#3.在服务MusicPlayerService.java中添加:
IMusicPlayerService.Stubstub = new IMusicPlayerService.Stub() {
MusicPlayerServiceservice = MusicPlayerService.this;
@Override
public void stop() throws RemoteException {
service.stop();
}
@Override
public void start() throws RemoteException {
service.start();
}
@Override
public void setPlayMode(int mode) throws RemoteException {
service.setPlayMode(mode);
}
@Override
public void pre() throws RemoteException {
service.pre();
}
@Override
public void pause() throws RemoteException {
service.pause();
}
@Override
public void openAudio(int position) throws RemoteException {
service.openAudio(position);
}
@Override
public void next() throws RemoteException {
service.next();
}
@Override
public int getPlayMode() throws RemoteException {
// TODO Auto-generated method stub
return service.getPlayMode();
}
@Override
public int getDuration() throws RemoteException {
// TODO Auto-generated method stub
return service.getDuration();
}
@Override
public int getCurrentPosition() throws RemoteException {
// TODO Auto-generated method stub
return service.getCurrentPosition();
}
@Override
public String getAudioName() throws RemoteException {
// TODO Auto-generated method stub
return service.getAudioName();
}
@Override
public String getArtistName() throws RemoteException {
// TODO Auto-generated method stub
return service.getArtistName();
}
};
一定要在onBind方法中返回stub对象
@Override
public IBinder onBind(Intent intent) {
return stub;
}