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

来源:互联网 发布:java支付接口开发 编辑:程序博客网 时间:2024/05/16 12:29
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() {@Overridepublic 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;}}



原创粉丝点击