本文整理汇总了Java中android.media.SoundPool类的典型用法代码示例。如果您正苦于以下问题:Java SoundPool类的具体用法?Java SoundPool怎么用?Java SoundPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SoundPool类属于android.media包,在下文中一共展示了SoundPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initializeSoundPool
import android.media.SoundPool; //导入依赖的package包/类
private void initializeSoundPool()
{
if(sp!=null) {
sp.release();
sp=null;
}
try {
sp=new SoundPool.Builder()
.setMaxStreams(1)
.build();
soundId1=sp.load(context.getAssets().openFd("beats/default_bar.wav"), 1);
soundId2=sp.load(context.getAssets().openFd("beats/default_beat.wav"), 1);
} catch(IOException ioExc) {
if(sp!=null) {
sp.release();
sp=null;
}
}
}
示例2: loadSounds
import android.media.SoundPool; //导入依赖的package包/类
protected void loadSounds()
{
if (null != soundPool) {
return;
}
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(clickers.length + 1)
.setAudioAttributes(audioAttributes)
.build();
for (int i = 0; i < clickers.length; ++i) {
clickers[i].clickSound = soundPool.load(this, clickers[i].soundId, 1);
}
failSound = soundPool.load(this, R.raw.fail, 1);
successSound = soundPool.load(this, R.raw.success, 1);
}
示例3: SoundEngine
import android.media.SoundPool; //导入依赖的package包/类
public SoundEngine(Context context) {
this.context = context;
sounds = new SoundPool(5, AudioManager.STREAM_RING, 0);
soundsMap = new HashMap<Integer, Integer>();
soundsMap.put(EATFOOD, sounds.load(context, R.raw.pacmon_waka_waka, 1));
soundsMap.put(EATGHOST, sounds.load(context, R.raw.pacmon_eating_ghost, 1));
soundsMap.put(DIE, sounds.load(context, R.raw.pacmon_dies, 1));
soundsMap.put(READY, sounds.load(context, R.raw.pacmon_opening_song,1));
soundsMap.put(GAMEOVER, sounds.load(context, R.raw.pacmon_opening_song, 1));
//soundsMap.put(EATCHERRY, sounds.load(context, R.raw.pacmon_eating_cherry, 1));
AudioManager mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// the music that is played at the beginning
// music = MediaPlayer.create(context, R.raw.gameplaymusic);
// music.setLooping(true);
}
示例4: playSound
import android.media.SoundPool; //导入依赖的package包/类
public void playSound(@RawRes final int resId) {
if (this.mSoundSourceMap != null) {
if (this.mSoundSourceMap.containsKey(Integer.valueOf(resId))) {
play(((Integer) this.mSoundSourceMap.get(Integer.valueOf(resId))).intValue());
return;
}
this.mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (status == 0) {
MQSoundPoolManager.this.mSoundSourceMap.put(Integer.valueOf(resId),
Integer.valueOf(sampleId));
MQSoundPoolManager.this.play(sampleId);
}
}
});
this.mSoundPool.load(this.mContext.getApplicationContext(), resId, 1);
}
}
示例5: Sound
import android.media.SoundPool; //导入依赖的package包/类
public Sound(ComponentContainer container) {
super(container.$form());
thisComponent = this;
soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
soundMap = new HashMap<String, Integer>();
vibe = (Vibrator) form.getSystemService(Context.VIBRATOR_SERVICE);
sourcePath = "";
loadComplete = true; //nothing to wait for until we attempt to load
form.registerForOnResume(this);
form.registerForOnStop(this);
form.registerForOnDestroy(this);
// Make volume buttons control media, not ringer.
form.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Default property values
MinimumInterval(500);
if (waitForLoadToComplete) {
new OnLoadHelper().setOnloadCompleteListener(soundPool);
}
}
示例6: registerWithPlayer
import android.media.SoundPool; //导入依赖的package包/类
public void registerWithPlayer(SoundPool player)
{
try
{
if(fd != null)
{
soundId = player.load(fd, 1);
fd.close();
}
else
{
soundId = player.load(res, 1);
}
}
catch (IOException e)
{
Log.e("error loading audio files", e);
}
}
示例7: loadSound
import android.media.SoundPool; //导入依赖的package包/类
public void loadSound(){
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
try {
//Create objects of the 2 required classes
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor;
//create our three fx in memory ready for use
descriptor = assetManager.openFd("sample1.ogg");
sample1 = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("sample4.ogg");
sample4 = soundPool.load(descriptor, 0);
} catch (IOException e) {
//catch exceptions here
}
}
示例8: FeedbackController
import android.media.SoundPool; //导入依赖的package包/类
/**
* Constructs and initializes a new feedback controller.
*/
public FeedbackController(Context context) {
mContext = context;
mResources = context.getResources();
mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
mSoundPool = new SoundPool(NUMBER_OF_CHANNELS, DEFAULT_STREAM, 1);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (status == 0) {
synchronized (mPostLoadPlayables) {
if (mPostLoadPlayables.contains(sampleId)) {
soundPool.play(
sampleId, DEFAULT_VOLUME, DEFAULT_VOLUME, 1, 0, DEFAULT_RATE);
mPostLoadPlayables.remove(Integer.valueOf(sampleId));
}
}
}
}
});
mHandler = new Handler();
mResourceIdToSoundMap.clear();
mResourceIdToVibrationPatternMap.clear();
MidiUtils.purgeMidiTempFiles(context);
}
示例9: SoundPoolPlayer
import android.media.SoundPool; //导入依赖的package包/类
private SoundPoolPlayer() {
soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, SRC_QUALITY);
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (soundPool != null) {
for (Integer soundId : soundResIdMap.values()) {
soundPool.unload(soundId);
}
soundResIdMap.clear();
}
handler.postDelayed(this, RELEASE_RESOURCES_INTERVAL_MS);
}
}, RELEASE_RESOURCES_INTERVAL_MS);
}
示例10: soundPlay
import android.media.SoundPool; //导入依赖的package包/类
/**
* 播放一个铃声
*
* @param context
* @param resId 音乐资源ID
*/
public static void soundPlay(Context context, int resId) {
if (playSound) {
playCount = 0;
replease();
AudioManager am = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);// 实例化
int audioMaxVolum = am.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);// 音效最大值
am.setStreamVolume(AudioManager.STREAM_SYSTEM, audioMaxVolum,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
float audioCurrentVolum = am
.getStreamVolume(AudioManager.STREAM_SYSTEM);
volumnRatio = audioCurrentVolum / audioMaxVolum;
soundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
soundid = soundPool.load(context, resId, 1);
soundPool.setOnLoadCompleteListener(new LoadCompleteListener());
}
}
示例11: getGesturePadFromArray
import android.media.SoundPool; //导入依赖的package包/类
private GesturePad getGesturePadFromArray(String soundPaths[],
SoundPool soundPool,
View buttonView,
int color, int colorDef,
Activity activity) {
Sound sounds[] = new Sound[5];
for (int i = 0; i < 5; i++) {
if (i < soundPaths.length) {
// sounds exists
sounds[i] = new Sound(soundPool, soundPaths[i], mmr);
} else {
// no sound gesture
sounds[i] = new Sound(soundPool, null, mmr);
}
}
return new GesturePad(sounds, buttonView, color, colorDef, activity);
}
示例12: PushUpSoundManager
import android.media.SoundPool; //导入依赖的package包/类
public PushUpSoundManager(final Activity activity) {
AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = actualVolume / maxVolume;
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createSoundPool();
} else {
createDepricatedSoundPool();
}
sounds.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
popSound = sounds.load(activity, R.raw.pop, 1);
tenSound = sounds.load(activity, R.raw.ten, 1);
tadaSound = sounds.load(activity, R.raw.tada, 1);
counter = 0;
}
示例13: buildSoundPool
import android.media.SoundPool; //导入依赖的package包/类
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private SoundPool buildSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
return new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.setAudioAttributes(audioAttributes)
.build();
}
return new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
}
示例14: playSequence
import android.media.SoundPool; //导入依赖的package包/类
public void playSequence(final boolean loop) {
int midiValue = 0;
int velocity = 0;
int instrument = 0;
String fileName = "dyn_" + midiValue + ".mid";
// MidiFile.writeNoteFile(midiValue, velocity, fileName, MainActivity.config.tempo.getTempoEvent());
// MidiFile.writeSingleNoteFile(midiValue,instrument, velocity, fileName, MainActivity.config.tempo.getTempoEvent());
lastSequenceId = sequenceSoundPool.load(FileManager.getInstance().INTERNAL_PATH + "sequence.mid", 1);
sequenceSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
sequenceSoundPool.play(lastSequenceId, 1, 1, 0, (loop ? -1 : 0), 1);
}
});
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(FileManager.getInstance().EXTERNAL_PATH + "sequence.mid");
mediaPlayer.setLooping(loop);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
HLog.e("Media Player Failure");
e.printStackTrace();
}
}
示例15: AndroidAudio
import android.media.SoundPool; //导入依赖的package包/类
public AndroidAudio(final AndroidPlatform plat) {
this.plat = plat;
this.pool = new SoundPool(plat.activity.maxSimultaneousSounds(),
AudioManager.STREAM_MUSIC, 0);
this.pool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
PooledSound sound = loadingSounds.remove(soundId);
if (sound == null) {
plat.log().warn("Got load complete for unknown sound [id=" + soundId + "]");
} else if (status == 0) {
sound.succeed(soundId);
} else {
sound.fail(new Exception("Sound load failed [errcode=" + status + "]"));
}
}
});
}