PCM 原始字节 [] 到 Android 上的音频 [英] PCM Raw Bytes [] To Audio on Android
问题描述
我目前有一个字节数组形式的 PCM 音频.格式是有符号的 16 位小端.我想将其转换为一些可在 Android 上播放的格式,最好是 3.2 或更高版本.有没有人有关于如何做到这一点的建议?我做了一些研究并尝试了以下方法,但都没有成功.如果有人能提出更好的方法或指出我哪里出错了,我们将不胜感激.
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.
我尝试使用正确的音频设置创建 AudioFormat,但是 Android 不支持 javax.sound.sampled 库.
I have tried creating an AudioFormat with the correct audio settings, however Android does not support the javax.sound.sampled library.
我还尝试将 PCM 数据写入波形文件,但在获取所有必要的 wav 标头信息时遇到了麻烦.
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.
我也尝试过使用 Android 提供的 AudioTrack 类来播放 PCM 数据.但是,我似乎听不到任何声音.这是我的方法:
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:
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");
}
}
任何建议将不胜感激.
推荐答案
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.
编辑
将您的流更改为 AudioManager.STREAM_MUSIC
这篇关于PCM 原始字节 [] 到 Android 上的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!