您的位置:首页 > 其它

简单记录,非常简单的一个提示音播放类(允许多个提示音播放)

2013-05-15 11:14 288 查看
import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

/**
* @author maclay
*
*         mediaplayer用来播放提示音,需要提供音频文件id
*
*         2013-5-13
*/
public class AlertTone {
private MediaPlayer mPlayer = null;
static AlertTone smAlertTone;

public static AlertTone getInstance() {
if (smAlertTone == null) {
smAlertTone = new AlertTone();
}
return smAlertTone;
}

public void play(Context context, int id) {
if (mPlayer == null) {
mPlayer = MediaPlayer.create(context, id);
}
if (mPlayer.isPlaying()) {
System.out.println(11111);
return;
}
try {
mPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mPlayer.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mPlayer=null;
smAlertTone=null;
}
});
}
}
//使用soundpool



public class WorksheetSoundPool {
private SoundPool mSoundPool;
public static WorksheetSoundPool smPool;
private int mID;

public static WorksheetSoundPool getInstance() {
if (smPool == null) {
smPool = new WorksheetSoundPool();
}
return smPool;
}

public void play() {
if (mSoundPool == null) {
mSoundPool = new SoundPool(3, 1, 0);
mID = mSoundPool.load(PttApplication.getAppContext(), R.raw.msg, 1);
mSoundPool.setLoop(mID, 0);
}
mSoundPool.play(mID, 10, 10, 0, 0, 1f);
}

public void close() {
if (mSoundPool != null) {
try {
mSoundPool.release();
} catch (Exception e) {
}
mSoundPool = null;
}
smPool = null;
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: