PCM 原始字节 [] 到 Android 上的音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11267687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PCM Raw Bytes [] To Audio on Android
提问by ShannonS
I currently have a PCM audio in the form of a byte array. The format is signed 16 bit little endian. I would like to convert this to some playable format on the Android, preferably version 3.2 or higher. Does anyone have suggestions on how this can be done? I have done some research and tried the following below, but none were successful. It would be much appreciated if anyone can suggest a better way or indicate where I have gone wrong.
我目前有一个字节数组形式的 PCM 音频。格式是有符号的 16 位小端。我想将其转换为一些可在 Android 上播放的格式,最好是 3.2 或更高版本。有没有人有关于如何做到这一点的建议?我做了一些研究并尝试了以下方法,但都没有成功。如果有人能提出更好的方法或指出我哪里出错了,我们将不胜感激。
I have tried creating an AudioFormat with the correct audio settings, however Android does not support the javax.sound.sampled library.
我尝试使用正确的音频设置创建 AudioFormat,但是 Android 不支持 javax.sound.sampled 库。
I have also tried writing the PCM data into a wave file, but I am running into trouble getting all of the necessary wav header information.
我还尝试将 PCM 数据写入波形文件,但在获取所有必要的 wav 标头信息时遇到了麻烦。
I have also tried using the AudioTrack class offered by the Android, to play the PCM data. However, I can't seem to hear any sound. Here is my method:
我还尝试使用 Android 提供的 AudioTrack 类来播放 PCM 数据。但是,我似乎听不到任何声音。这是我的方法:
public void playback(){
try{
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, 500000, AudioTrack.MODE_STATIC);
audioTrack.write(audio, 0, 500000);
} catch(Throwable t){
Log.d("Audio","Playback Failed");
}
}
Any suggestions would be appreciated.
任何建议,将不胜感激。
回答by Hyman Satriano
http://developer.android.com/reference/android/media/AudioTrack.html
http://developer.android.com/reference/android/media/AudioTrack.html
This class looks like it has exactly what you need!
这门课看起来正是你需要的!
write(byte[] audioData, int offsetInBytes, int sizeInBytes)
Writes the audio data to the audio hardware for playback.
将音频数据写入音频硬件进行播放。
EDIT
编辑
Change your stream to AudioManager.STREAM_MUSIC
将您的流更改为 AudioManager.STREAM_MUSIC
回答by vitakot
The question is whether you want to play PCM samples you receive from some source directly in the Android app or if you have some external files you need to convert. In the first case go the way as Hyman57 described. It is really easy and you can even add markers to have precise timing.
问题是您是想直接在 Android 应用程序中播放从某个来源接收的 PCM 样本,还是有一些需要转换的外部文件。在第一种情况下,按照 Hyman57 描述的方式进行。这真的很容易,您甚至可以添加标记以获得精确的计时。
int bufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
mAudioTrack.play();
then call write method in a cycle:
然后循环调用write方法:
mAudioTrack.write(buffer, 0, buffer.length);
In the second case just use some free audio editor: http://audacity.sourceforge.net/download/. You can store PCM samples as a .txt file, open it in the editor and save in a format you need...
在第二种情况下,只需使用一些免费的音频编辑器:http: //audacity.sourceforge.net/download/。您可以将 PCM 样本存储为 .txt 文件,在编辑器中打开它并以您需要的格式保存...