设为首页 加入收藏

TOP

Android 实现多个Audio文件的顺序播放(一)
2014-11-24 07:23:55 来源: 作者: 【 】 浏览:2
Tags:Android 实现 多个 Audio 文件 顺序 播放

代码如下:


package com.yous365.android.util;


import org.xml.sax.SAXException;


import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.util.Log;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;


import javax.xml.parsers.ParserConfigurationException;


public class SoundManager {


private SoundPool mSoundPool;


private HashMap mSoundPoolMap;


private AudioManager mAudioManager;


private Context mContext;


private Handler mHandler = new Handler();


private Vector mKillSoundQueue = new Vector();


private VoicePhoneNumberUtil util;


private long delay = 1000;


private long seperateTime = 700;


private float rate = 1.0f;


private String locale;


static private SoundManager _instance;


/**
* Requests the instance of the Sound Manager and creates it if it does not
* exist.
*
* @return Returns the single instance of the SoundManager
*/
static synchronized public SoundManager getInstance() {
if (_instance == null)
_instance = new SoundManager();
return _instance;
}


/**
*
*/
private SoundManager() {
util = VoicePhoneNumberUtil.getInstance();
}


/**
* Initialises the storage for the sounds
*
* @param theContext The Application context
*/


public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1,
AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap();
mAudioManager = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
}


/**
* Add a new Sound to the SoundPool
*
* @param key - The Sound Index for Retrieva l
* @param SoundID - The Android ID for the Sound asset.
*/


public void addSound(String key, int SoundID) {
mSoundPoolMap.put(key,
mSoundPool.load(mContext, SoundID, 1));
}
/**
*
* @param key the key we need to get the sound later
* @param afd the fie store in the asset
*/
public void addSound(String key, AssetFileDescriptor afd) {
mSoundPoolMap.put(key, mSoundPool.load(
afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength(), 1));
}

/**
* play the sound loaded to the SoundPool by the key we set
* @param key the key in the map
*/
public void playSound(String key) {


int streamVolume = mAudioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume
/ mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);


int soundId = mSoundPool.play(
mSoundPoolMap.get(key), streamVolume,
streamVolume, 1, 0, rate);
mKillSoundQueue.add(soundId);
// schedule the current sound to stop after set milliseconds
mHandler.postDelayed(new Runnable() {
public void run() {
if (!mKillSoundQueue.isEmpty()) {
mSoundPool.stop(mKillSoundQueue
.firstElement());
}
}
}, delay);


}

/**
*
* @param key the key in t

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ubuntu 64 bit 编译 Android 下一篇Java分批读取文件

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)