以短剧正确的方法听起来Android的? [英] The correct way to play short sounds Android?
问题描述
我创建将在屏幕上的多个图像的应用程序,这些图像将是按钮,当挖掘将发挥一短声。我研究这一点,只能找到我用来播放声音,这似乎并不敏感,在目前所有的方法。我想这声音迅速戏剧和响应快速的许多水龙头。我不能确定这是否是可能的,即使在Android中。
I am creating an application that will have multiple images on the screen, these images will be buttons and when tapped will play a short sound. I researched this and could only find the current method I am using to play sounds, which does not seem responsive at all. I want that the sound plays quickly and is responsive to many rapid taps. I was unsure if this was even possible in Android.
在code,我用打我的声音是这样的:
The code I am using to play my sound is this:
Button sound1;
MediaPlayer firstSound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
firstSound = MediaPlayer.create(SoundActivity.this, R.raw.click);
sound1 = (Button) findViewById(R.id.Sound1);
beaver.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
firstSound.start();
}
});
}
我的滞后问题,也可能是也许到我使用的媒体播放器对象的方法是什么?我觉得这应该是我的手机上顺畅,就如何发挥的声音在Android的任何建议是AP preciated。
My lag issue could also be perhaps down to the way I am using the media player object? I do feel that this should be smoother on my phone, any suggestions on how to play sounds in Android is appreciated.
感谢。
由于发现声音泳池,我已经编辑我的code利用这一点,它完美的作品,新的code是这样的:
Since discovering Sound Pool, I have edited my code to use this and it works perfectly, the new code looks like this:
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
int soundID = 1;
Button sound1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(soundID, soundPool.load(this, R.raw.click, 1));
sound1 = (Button) findViewById(R.id.bBeaver);
sound1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float leftVolume = curVolume/maxVolume;
float rightVolume = curVolume/maxVolume;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(soundID, leftVolume, rightVolume, priority, no_loop, normal_playback_rate);
}
});
}
感谢
推荐答案
您可以使用的Soundpool
。它适合你想要做完美什么。
You can use SoundPool
. It fits what you want to do perfectly.
你只需要一种方法来存储对应于每个图像(或按钮),声音效果的ID。
You'll just need a way to store the sound effect IDs corresponding to each image (or button).
也许扩展按钮保存相关联的声音效果ID。而使用通用的Soundpool发挥关联时,触摸按钮的ID的声音效果。
Perhaps extend Button to store associated sound effect ID. And use a common SoundPool to play the sound effect associated to the id when the button is touched.
这篇关于以短剧正确的方法听起来Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!