背景实现功能

本文介绍如何在Android应用中实现背景音乐播放服务。通过创建自定义Service并利用MediaPlayer进行多线程后台播放,同时介绍了如何使用SoundPool播放音效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                             背景实现功能

一、Android有其自带的播放器,我们可以使用隐式Intent来调用它:通过传入一个Action为ACTION_VIEW同时,指定Data为所要播放的Audio的Uri对象,并指定格式信息,则我们就可以调用播放器来播放该Audio了。

二、具体实现:

1、AudioService.java(多线程实现后台音乐播放)

* 多线程实现后台播放背景音乐的service
 */
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class AudioService extends Service implements
		MediaPlayer.OnCompletionListener {
	// 实例化MediaPlayer对象
	MediaPlayer player;
	private final IBinder binder = new AudioBinder();

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	public void onCreate() {
		super.onCreate();
		// 从raw文件夹中获取一个应用自带的mp3文件
		player = MediaPlayer.create(this, R.raw.qq);
		player.setOnCompletionListener(this);
		player.setLooping(true);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		if (!player.isPlaying()) {
			new MusicPlayThread().start();
		}
		else player.isPlaying();
		return START_STICKY;
	}

	
	/**
	 * 当Audio播放完的时候触发该动作
	 */
	public void onCompletion(MediaPlayer mp) {
		stopSelf();// 结束了,则结束Service

	}

	public void onDestroy() {
		super.onDestroy();
		if (player.isPlaying()) {
			player.stop();
		}
		player.release();
	}

	// 为了和Activity交互,我们需要定义一个Binder对象
	public class AudioBinder extends Binder {
		// 返回Service对象
		public AudioService getService() {
			return AudioService.this;
		}
	}

	private class MusicPlayThread extends Thread {
		public void run() {
			if (!player.isPlaying()) {
				player.start();
			}
		}
	}
   
}
2、
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);     
    }     
}

3、当运行时效果显示如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值