简单记录,非常简单的一个提示音播放类(允许多个提示音播放)
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; } }
相关文章推荐
- 《解决问题1000.1》又是I2C驱动问题,产品应用上一个问题,非常简单,也比较典型,记录一下
- 记录搭建redis用作缓存并且成功运行了一个简单例子的经历
- SLF4J - 一个允许你统一日志记录API的抽象层
- 第一次使用linux并编写了一个dump mysql的程序,记录下简单的步骤
- 一个非常简单的将半角转换为中文的函数
- 一个非常简单的将半角转换为中文的函数
- 记录一个简单的问题
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例2
- 字符串处理的一个简单Function 仅作记录
- 一个非常简单的crakeme
- 和一个非常厉害的大牛聊天记录
- 一个非常简单的抽奖游戏
- 一个非常简单的着色器示例代码
- 一个非常简单的顶点片元着色器
- 使用Java开发一个非常简单的Web Service例子
- Android:一个非常非常简单的天气app
- 【java学习记录】3.采用面向对象的方法设计一个能对复数(Complex)进行加、减、乘法的简单运算的程序
- 一个简单的播放进度条
- 【问题记录】uwsgi部署并启动俩个几乎一样的python flask web app,发现有一个app响应时间非常长
- 记录最近用到的一个简单的正则表达式