PCM原始字节[]为音频在Android [英] PCM Raw Bytes [] To Audio on Android

查看:171
本文介绍了PCM原始字节[]为音频在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个字节数组形式的PCM音频。格式符号的16位小端。我想将其转换为对Android的一些播放格式,preferably 3.2或更高版本。有没有人对如何可以做到这一点建议吗?我做了一些调查,并试图下面的下面,但没有成功。这将是pciated如果任何人都可以提出一个更好的方法或指明我已经错了多少AP $ P $。

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");
    }
}

任何建议将是AP preciated。

Any suggestions would be appreciated.

推荐答案

<一个href="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.

修改

更改您的流 AudioManager.STREAM_MUSIC

这篇关于PCM原始字节[]为音频在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆