您的位置:首页 > 移动开发 > Android开发

Android实现音乐后台播放

2015-07-04 12:08 846 查看
Service是一个生命周期长且没有用户界面的程序,当程序在各个activity中切换的时候,我们可以利用service来实现背景音乐的播放,即使当程序退出到后台的时候,音乐依然在播放。

实现代码如下:

(1)src/.../MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
private Button mBtnPrevious; // 上一首
private Button mBtnPlay; // 播放
private Button mBtnNext; // 下一首
private Button mBtnPause; // 暂停
private ComponentName component; // 用于启动服务

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到布局中的控件
findView();
// 绑定控件事件
setListener();
}

// 得到布局中的控件
private void findView() {

component = new ComponentName(this, MusicService.class);
mBtnPrevious = (Button) findViewById(R.id.previous);
mBtnPlay = (Button) findViewById(R.id.play);
mBtnNext = (Button) findViewById(R.id.next);
mBtnPause = (Button) findViewById(R.id.pause);
}

// 绑定控件事件
private void setListener() {
mBtnPrevious.setOnClickListener(this);
mBtnPlay.setOnClickListener(this);
mBtnNext.setOnClickListener(this);
mBtnPause.setOnClickListener(this);
}

// 按钮点击事件响应
public void onClick(View v) {
// 如果点击前一首歌,就在intent中传递前一首歌参数
if (v == mBtnPrevious) {
Intent mIntent = new Intent(MusicService.PREVIOUS_ACTION);
mIntent.setComponent(component);
startService(mIntent);
// 如果点击前播放歌曲,就在intent中传递播放当前歌参数
} else if (v == mBtnPlay) {
Intent mIntent = new Intent(MusicService.PLAY_ACTION);
mIntent.setComponent(component);
startService(mIntent);
// 如果点击前一首歌,就在intent中传递下一首歌参数
} else if (v == mBtnNext) {
Intent mIntent = new Intent(MusicService.NEXT_ACTION);
mIntent.setComponent(component);
startService(mIntent);
// 如果点击前一首歌,就在intent中传递暂停首歌参数
} else {
Intent mIntent = new Intent(MusicService.PAUSE_ACTION);
mIntent.setComponent(component);
startService(mIntent);
}
}
}
(2) src/.../MusicService.java

public class MusicService extends Service {
// 定义需要显示的音乐的字段
String[] mCursorCols = new String[] {
"audio._id AS _id", // index must match IDCOLIDX below
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DURATION };
private MediaPlayer mMediaPlayer; // 声明播放器
private Cursor mCursor; // 声明游标
private int mPlayPosition = 0; // 当前播放的歌曲

// 注册意图
public static final String PLAY_ACTION = "com.wyl.music.PLAY_ACTION";
public static final String PAUSE_ACTION = "com.wyl.music.PAUSE_ACTION";
public static final String NEXT_ACTION = "com.wyl.music.NEXT_ACTION";
public static final String PREVIOUS_ACTION = "com.wyl.music.PREVIOUS_ACTION";

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
mMediaPlayer = new MediaPlayer();
// 通过一个URI可以获取所有音频文件
Uri MUSIC_URL = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// 这里我过滤了一下,因为我机里有些音频文件是游戏音频,很短
// 我这里作了处理,默认大于10秒的可以看作是系统音乐
mCursor = getContentResolver().query(MUSIC_URL, mCursorCols,
"duration > 10000", null, null);
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// 根据不同的action,做不同的相应
String action = intent.getAction();
//播放
if (action.equals(PLAY_ACTION)) {
play();
//暂停
} else if (action.equals(PAUSE_ACTION)) {
pause();
//下一首
} else if (action.equals(NEXT_ACTION)) {
next();
//前一首
} else if (action.equals(PREVIOUS_ACTION)) {
previous();
}
}

// 播放音乐
public void play() {
//初始化音乐播放器
inite();
}

// 暂停时,结束服务
public void pause() {
//暂停音乐播放
stopSelf();
}

// 上一首
public void previous() {
//得到前一首的歌曲
if (mPlayPosition == 0) {
mPlayPosition = mCursor.getCount() - 1;
} else {
mPlayPosition--;
}
//开始播放
inite();
}

// 下一首
public void next() {
//得到后一首歌曲
if (mPlayPosition == mCursor.getCount() - 1) {
mPlayPosition = 0;
} else {
mPlayPosition++;
}
//开始播放
inite();
}

// 初始化播放器
public void inite() {
//充值MediaPlayer
mMediaPlayer.reset();
// 获取歌曲位置
String dataSource = getDateByPosition(mCursor, mPlayPosition);
// 歌曲信息
String info = getInfoByPosition(mCursor, mPlayPosition);
// 用Toast显示歌曲信息
Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT)
.show();
try {
// 播放器绑定资源
mMediaPlayer.setDataSource(dataSource);
// 播放器准备
mMediaPlayer.prepare();
// 播放
mMediaPlayer.start();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}

// 根据位置来获取歌曲位置
public String getDateByPosition(Cursor c, int position) {
c.moveToPosition(position);
int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);
String data = c.getString(dataColumn);
return data;
}

// 获取当前播放歌曲演唱者及歌名
public String getInfoByPosition(Cursor c, int position) {
c.moveToPosition(position);
int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST);
String info = c.getString(artistColumn) + " "
+ c.getString(titleColumn);
return info;

}

// 服务结束时要释放MediaPlayer
public void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: