背景音乐
来源:互联网 发布:上古卷轴5诺德捏脸数据 编辑:程序博客网 时间:2024/05/17 05:13
我们使用android自带的播放器功能做一个背景音乐,主要我们可以使用隐式Intent来调用它:通过传入一个Action为ACTION_VIEW同时,指定Data为所要播放的Audio的Uri对象,并指定格式信息,调用播放器来播放该Audio。
集体的实现
1、界面
2、AudioService.java(多线程实现后台音乐播放)
</pre><pre name="code" class="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(); } } } }
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); } }
0 0
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- *********背景音乐*******
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- 背景音乐
- // 插入排序 源码
- leetcode-237-Delete Node in a Linked List
- ie7 slideup,mouseleave...的bug的解决
- cocos2d-x游戏实例(5)-A星算法(1)
- C++获取一个目录下的所有文件
- 背景音乐
- cocos2d-x游戏实例(6)-A星算法(2)
- hdoj 1269 迷宫城堡 【有向图SCC 入门题目】
- KVC 与 KVO 理解
- cocos2d-x游戏实例(7)-A星算法(3)
- hdu 5050
- static关键词
- cocos2d-x游戏实例(8)-A星算法(4)
- 一些常识