android中服务播放音乐,android中用Service播放音乐

一般的Android应用程序的音乐可以分为背景音乐和点击各种控件时的音效,前者一般为比较大的音乐文件,后者一般为比较小的音乐。在Android中一般用MediaPlayer类处理比较大的音频文件,用SoundPool类处理比较短促的音频文件。

因此,现设计一音乐播放工具类如下:

MusicPlayer

Java代码 5960297_1.gif

importandroid.content.Context;

importandroid.media.AudioManager;

importandroid.media.MediaPlayer;

importandroid.media.SoundPool;

importcom.ruanko.shengji4Android.R;

importcom.ruanko.shengji4Android.model.SysSetting;

publicclassMusicPlayerimplementsMediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

privateContext context;

privateMediaPlayer bgPlayer;//播放背景音乐的播放器

privateSoundPool actionMusicPlayer;//播放音效的播放器

privateintsource_da,source_givecard,source_start,source_win,source_calllord;//各种音效的source

publicMusicPlayer(Context context) {//初始化

this.context = context;

this.actionMusicPlayer =newSoundPool(10, AudioManager.STREAM_SYSTEM,5);//这里指定声音池的最大音频流数目为10,声音品质为5大家可以自己测试感受下效果

this.source_da = actionMusicPlayer.load(context, R.raw.da,0);

this.source_givecard = actionMusicPlayer.load(context, R.raw.givecard,0);

this.source_start = actionMusicPlayer.load(context, R.raw.start,0);

this.source_win = actionMusicPlayer.load(context, R.raw.win,0);

this.source_calllord = actionMusicPlayer.load(context, R.raw.calllord,0);

}

publicvoidonCompletion(MediaPlayer mp) {//音频文件播放完成时自动调用

}

publicbooleanonError(MediaPlayer mp,intwhat,intextra) {//音频文件播放发生错误时自动调用

returnfalse;

}

publicvoidplayBgSound(intparamInt) {//播放背景音乐,paramInt为音频文件ID

if(!SysSetting.getInstance(context).isBgSound()) {//SysSetting为一个单例类,是我自己定义的,用于保存本应用程序相关设置,isBgSound()为得到是否开启背景音乐设置

return;

}

stopSound(bgPlayer);

try{

MediaPlayer mediaPlayer = MediaPlayer.create(context, paramInt);

bgPlayer = mediaPlayer;

bgPlayer.setOnCompletionListener(this);

bgPlayer.setLooping(true);//设置是否循环,一般背景音乐都设为true

bgPlayer.start();

}catch(IllegalStateException e) {

e.printStackTrace();

}

}

publicMediaPlayer getBackGroundPlayer() {

returnthis.bgPlayer;

}

publicvoidstopBgSound() {//停止背景音乐的播放

if(bgPlayer ==null)

return;

if(bgPlayer.isPlaying())

bgPlayer.stop();

bgPlayer.release();

bgPlayer =null;

}

privatevoidplaySound(intsource) {//如果系统设置中开启了音效,则播放相关的音频文件

if(SysSetting.getInstance(context).isEffectSound()) {

actionMusicPlayer.play(source,1,1,0,0,1);

}

}

publicvoidplayHitCardSound() {

playSound(source_da);

}

publicvoidplayGiveCardSound() {

playSound(source_givecard);

}

publicvoidplayStartSound() {

playSound(source_start);

}

publicvoidplayWinSound() {

playSound(source_win);

}

publicvoidplayCalllordSound() {

playSound(source_calllord);

}

}

import android.content.Context;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.SoundPool;

import com.ruanko.shengji4Android.R;

import com.ruanko.shengji4Android.model.SysSetting;

public class MusicPlayer implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

private Context context;

private MediaPlayer bgPlayer; //播放背景音乐的播放器

private SoundPool actionMusicPlayer; //播放音效的播放器

private int source_da,source_givecard,source_start,source_win,source_calllord; //各种音效的source

public MusicPlayer(Context context) { //初始化

this.context = context;

this.actionMusicPlayer = new SoundPool(10, AudioManager.STREAM_SYSTEM,5); //这里指定声音池的最大音频流数目为10,声音品质为5大家可以自己测试感受下效果

this.source_da = actionMusicPlayer.load(context, R.raw.da, 0);

this.source_givecard = actionMusicPlayer.load(context, R.raw.givecard, 0);

this.source_start = actionMusicPlayer.load(context, R.raw.start, 0);

this.source_win = actionMusicPlayer.load(context, R.raw.win, 0);

this.source_calllord = actionMusicPlayer.load(context, R.raw.calllord, 0);

}

public void onCompletion(MediaPlayer mp) { //音频文件播放完成时自动调用

}

public boolean onError(MediaPlayer mp, int what, int extra) { //音频文件播放发生错误时自动调用

return false;

}

public void playBgSound(int paramInt) { //播放背景音乐,paramInt为音频文件ID

if(!SysSetting.getInstance(context).isBgSound()) { //SysSetting为一个单例类,是我自己定义的,用于保存本应用程序相关设置,isBgSound()为得到是否开启背景音乐设置

return;

}

stopSound(bgPlayer);

try {

MediaPlayer mediaPlayer = MediaPlayer.create(context, paramInt);

bgPlayer = mediaPlayer;

bgPlayer.setOnCompletionListener(this);

bgPlayer.setLooping(true); //设置是否循环,一般背景音乐都设为true

bgPlayer.start();

} catch (IllegalStateException e) {

e.printStackTrace();

}

}

public MediaPlayer getBackGroundPlayer() {

return this.bgPlayer;

}

public void stopBgSound() { //停止背景音乐的播放

if(bgPlayer == null)

return;

if(bgPlayer.isPlaying())

bgPlayer.stop();

bgPlayer.release();

bgPlayer = null;

}

private void playSound(int source) { //如果系统设置中开启了音效,则播放相关的音频文件

if(SysSetting.getInstance(context).isEffectSound()) {

actionMusicPlayer.play(source, 1, 1, 0, 0, 1);

}

}

public void playHitCardSound() {

playSound(source_da);

}

public void playGiveCardSound() {

playSound(source_givecard);

}

public void playStartSound() {

playSound(source_start);

}

public void playWinSound() {

playSound(source_win);

}

public void playCalllordSound() {

playSound(source_calllord);

}

}

在activity中怎样使用这个工具类呢?播放背景音乐可能我们一般都想到的是service,但是service对背景音乐的控制比较难,尤其是,在不同场景,需要不时变换背景音乐的情况下,因此,可以考虑自己定义一个activity,继承Activity,在其中定义一个静态的成员变量

Java代码 5960297_1.gif

publicstaticMusicPlayer musicPlayer;

public static MusicPlayer musicPlayer;

并在这个Activity的onCreate方法中初始化该musicPlayer

Java代码 5960297_1.gif

if(musicPlayer ==null) {

SysSetting.getInstance(getApplicationContext()).load();//加载系统设置,SysSetting为我自己定义的一个类,由于代码较长,就不给出了

musicPlayer =newMusicPlayer(getApplicationContext());

musicPlayer.playBgSound(R.raw.welcome);//播放背景音乐

}

if(musicPlayer == null) {

SysSetting.getInstance(getApplicationContext()).load(); //加载系统设置,SysSetting为我自己定义的一个类,由于代码较长,就不给出了

musicPlayer = new MusicPlayer(getApplicationContext());

musicPlayer.playBgSound(R.raw.welcome); //播放背景音乐

}

然后,再将所有的Activity继承于这个自己定义的Activity即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值