如何在android中模拟弹奏一个钢琴音符?
我将在 android pad 中模拟钢琴键盘。当使用按键时,我想播放一些声音,我知道JetPlayer可以播放midi文件,但是在这种情况下,我没有mid文件,只有一个音符,怎么办?
I will simulate a piano key-board in android pad. When use press one key, I would like play some sound, I know JetPlayer can play midi file, but in this case, I have no mid file, just one single note, how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MIDI 和使用 MP3 是完全不同的事情。您可以将 MIDI 编程为听起来像钢琴,但您必须做大量工作才能获得良好的结果,并且您需要注意结果将取决于手机的声音硬件。 Android 对 MIDI 的支持也很差,您需要大量代码来完成管道工作。我建议你坚持使用 MP3。 100x20k 文件实际上并没有那么大。您可以通过一些额外的压缩来使它们更小。对于单音符之类的东西,您确实不需要高质量的音频。在您的手机上尝试一下 16kbps、32kbps 和 64kpbs。
如果您使用 MP3,您可能需要考虑使用 SoundPool 类。查看此处的教程
您可以为每个声音创建一个带有索引的 HashMap,然后您可以使用该索引来播放每个单独的音符。例如,索引可以是键号。要播放音符,您只需使用相关索引调用 SoundPool 的 playSound 方法即可。
我假设您的钢琴键数量有限,因此加载多个声音文件(您的 MP3)的开销是可以接受的。如果数量很大,您可能需要考虑仅加载用户可见的按键的声音文件,以控制内存使用情况。
MIDI and using MP3s are completely different things. You could program MIDI to sound like a piano but you will have to do a lot of work to get a good result and you need to be aware that the result will depend on the sound hardware of the phone. Android support for MIDI is also very poor and you'll need a lot of code to do the plumbing work. I recommend that you stick with MP3s. 100x20k files is not really that big. You could possibly make them smaller with some additional compression. For things like single notes, you really don't need high quality audio. Try it out on your phone with 16kbps, 32kbps and 64kpbs.
If you go with MP3, You might want to consider using the SoundPool classes. Take a look at the tutorial here
This enables you to create a HashMap with an index for each sound which you can then use to play each individual note. The index could be the key number for example. To play a note, you would simply call the playSound method of the SoundPool with the relevant index.
I'm assuming that you have a limited number of piano keys and therefore, the overhead of loading multiple sound files (your MP3s) is acceptable. If you have a large number, you might need to think about only loading the sound files for keys which are visible to the user in order to keep your memory usage under control.
您不能专门在手机上“弹奏”钢琴。您必须有一个用于该特定音符的声音字节。
You can't specifically "play" a piano on the phone.. You have to have a sound byte, that is used for that particular note.