Android 在应用程序中加载和播放声音的最快方式
我正在开发一个项目,其中我必须在一个活动中加载 6 种不同的声音,并在单击按钮时播放所有声音。声音文件不是很大,但问题是它们可能会更多。所以我的问题是这是在单个活动中加载声音文件的最快方法。出于测试目的,我使用 res/raw 文件夹来存储声音文件,并尝试使用两种不同的方法来播放文件,但结果并不令我满意。这是两种不同类型的代码:
方法1:
Button first = (Button) findViewById(R.id.one);
Button second = (Button) findViewById(R.id.two);
first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumtwo);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
second.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumone);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
方法2:
private SoundPool spool;
private int soundID,soundID2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.drumone, 1);
soundID2 = spool.load(this, R.raw.drumtwo, 1);
Button three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Sound();
}
});
}
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
但是问题是这两种方法都很慢..我需要找到一种更快的方式来播放声音..但现在几乎有一个单击按钮播放声音后的第二秒。
你能给我一些如何更快地播放声音的建议吗?我应该在应用程序启动时将它们加载到缓存中,还是将它们保存在数据库或其他地方(事实上,我不认为数据库是一个好的选择,但是我想听听一些建议)。或者也许从资产文件夹中加载它们,但我认为它仍然会很慢。
那么有什么想法或建议吗? 提前致谢!
I'm working on a project in which I have to load 6 different sounds in one activity and play all sound on button click.The sound file are not so big,but the problem is that maybe they will be more.So my question is which is the fastest way to load sound files in a single activity.For test purposes I used res/raw folder to store the sound files and tried with two different method to play the files,but the result did not satisfied me.Here is the two different types of code :
Method 1:
Button first = (Button) findViewById(R.id.one);
Button second = (Button) findViewById(R.id.two);
first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumtwo);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
second.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumone);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
Method 2:
private SoundPool spool;
private int soundID,soundID2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.drumone, 1);
soundID2 = spool.load(this, R.raw.drumtwo, 1);
Button three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Sound();
}
});
}
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
But the problem is that the both methods are slow..I need to find a way a faster way to play the sounds..but now there is like almost a second after I click the button to play the sound.
Can you give me some advice how to play the sounda faster.Should I load them into a cache when the application starts or save them in database or somewhere else (I don't think database is a good option as a matter of fact,but I want to hear some suggestions).Or maybe load them from assets folder,but I think it still going to be slow.
So any ideas or suggestions?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
onCreate
中创建媒体播放器,然后按钮即可让它们播放。我想说,这将是最简单的解决方案。或者,根据它对您的重要性以及您想要做的工作量,您可以考虑使用 JetPlayer:
这里是媒体的 Android 开发页面:
http://developer.android.com/guide/topics/media/index.html
JetPlayer 类的页面:
http://developer.android.com/reference/android/media/JetPlayer.html
以及创建 JET 文件的页面:
http://developer.android.com/guide/topics/media/jet /jetcreator_manual.html
如果你实现了这个,它可能会工作得最好,但肯定是最费力的。
JetPlayer 基本上可以让您拥有一个音频文件(我认为这是一个 MIDI 文件),其中包含多个音轨,您可以根据需要静音和取消静音。我对此没有个人经验,只是阅读了一些文档,但它似乎对于任何具有多种声音的情况都非常有用。
编辑:此外,值得一提的是此邮件列表和如果链接发生变化
You could create the media players in your
onCreate
and then the buttons will just make them play. That would be the easiest solution, I would say.Alternatively, depending on how important it is to you and how much work you want to do, you could consider using JetPlayer:
here's android development page for media:
http://developer.android.com/guide/topics/media/index.html
The page on the JetPlayer class:
http://developer.android.com/reference/android/media/JetPlayer.html
and the page on creating the JET files:
http://developer.android.com/guide/topics/media/jet/jetcreator_manual.html
If you implemented this it would likely work best but would definitely be the most work.
JetPlayer basically lets you have one audio file (i think it's a MIDI file) with multiple tracks that you mute and unmute as you please. I have no personal experience with it and have just read the docs some, but it seems like it would be very useful for any situation with more than one sound.
Edit: Also, it's worth mentioning this mailing list and in case the link ever changes this google search in case anyone is interested in android audio topics.
如果你想要更少的延迟,你必须使用 c++ 中的 OpenSL,From jelly bean 是更快的声音播放器
If you want to have the less delay you must use OpenSL in c++, From jelly bean is the faster sound player
SoundPool 工作正常,您可以查看 Hexiano 项目的示例。但是,您必须将声音预加载到 SoundPool 中,因为在使用它们之前需要花费大量时间来解码文件并将它们存储在内存中。但是一旦加载声音,按键和声音输出之间就没有明显的延迟。
SoundPool is working fine, you can see an example with the Hexiano project. However, you must preload the sounds into SoundPool, because it takes a lot of time to decode the file and store them in-memory before being able to use them. But once the sounds are loaded, there's no noticeable delay between key press and sound output.