import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseArray;
/**
* 点击声音类
*/
public class SoundManager {
private static SoundManager mSoundManager;
private SoundPool mSoundPool;
private AudioManager mAudioManager;
private SparseArray<Integer> mSoundPoolMap;
public static final int maxSounds = 4;
public static SoundManager getInstance(Context context) {
if (mSoundManager == null) {
mSoundManager = new SoundManager(context);
}
return mSoundManager;
}
public SoundManager(Context mContext) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
boolean loaded = true;
}
});
mSoundPoolMap = new SparseArray<Integer>();
mSoundPoolMap.put(0, mSoundPool.load(mContext, R.raw.click, 0));
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.success, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.change, 2));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.click_up, 3));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.click_down, 4));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume,
1, 0, 1f);
}
public static void clear() {
if (mSoundManager != null) {
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}
}
//使用
SoundManager soundManagerr = new SoundManager(getContext());
soundManager.playSound(index);
标签: java
android java 通过MusicService 播放背景音乐
1.在manifests.xml中添加
<service android:name=".utils.MusicService" android:enabled="true"/>
2.新建一个音乐类
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
/**
* MusicService 控制后台音乐播放
* 提供控制方法,在Activity中调用
*/
/*
public class MusicService extends Service implements MediaPlayer.OnErrorListener {
private final IBinder mBinder = new ServiceBinder(); //在Activity 使用Ibinder 对象与 MusicService 进行交互
MediaPlayer mediaPlayer;
private int length = 0;
public MusicService() {
//这里可以执行一些初始化
}
public class ServiceBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.bg_main);
mediaPlayer.setOnErrorListener(this);
if (mediaPlayer != null) {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
}
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int
extra) {
onError(mediaPlayer, what, extra);
return true;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer.start();
return START_STICKY;
}
public void pauseMusic() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
length = mediaPlayer.getCurrentPosition();
}
}
public void resumeMusic() {
if (mediaPlayer.isPlaying() == false) {
mediaPlayer.seekTo(length);
mediaPlayer.start();
}
}
public void stopMusic() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public boolean stopService(Intent name) {
mediaPlayer.stop();
mediaPlayer.release();
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
try {
mediaPlayer.stop();
mediaPlayer.release();
} finally {
mediaPlayer = null;
}
}
}
public boolean onError(MediaPlayer mp, int what, int extra) {
Common.showShort(this,String.valueOf(R.string.music_player_fail));
if (mediaPlayer != null) {
try {
mediaPlayer.stop();
mediaPlayer.release();
} finally {
mediaPlayer = null;
}
}
return false;
}
/*调用说明:
开始、暂停、继续播放、停止 音乐:
step 1:在 activity 的onCreate 中调用 doBindService 来将 service 绑定到 activity
Step 2: 通过Intent 启动service
Intent music = new Intent();
music.setClass(this,MusicService.class);
startService(music);
Step 3: 在 activity中调用 pause, resume 或者 stop 来控制音乐:
mServ.pauseMusic();
mServ.resumeMusic();
mServ.stopMusic();
Step 4: 在Activity 的onDestroy 中调用 doUnbindService
以下内容写在Activity中:
*/
private boolean mIsBound = false;
private MusicService mServ;
private ServiceConnection Scon =new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder
binder) {
mServ = ((MusicService.ServiceBinder)binder).getService();
}
public void onServiceDisconnected(ComponentName name) {
mServ = null;
}
};
void doBindService(){
bindService(new Intent(this,MusicService.class),
Scon,Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService()
{
if(mIsBound)
{
unbindService(Scon);
mIsBound = false;
}
}