调试|Android平台的声音播放|AudioTrack|JNI|悲催的侧边键

1.AudioTrack的用法

MODE_STREAM用法:

param.audiotrack_buffersize = AudioTrack.getMinBufferSize(freq, chnls, fmt);
AudioTrack audiotrack=new AudioTrack(AudioManager.STREAM_MUSIC,freq,chnls,fmt,param.audiotrack_buffersize,AudioTrack.MODE_STREAM);

getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat)
Returns the minimum buffer size required for the successful creation of an AudioTrack object to be created in the MODE_STREAM mode.

MODE_STATIC用法:

        int iMinBufSize = AudioTrack.getMinBufferSize(16000,  
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);
        
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                16000, //freq
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                100*iMinBufSize,
                AudioTrack.MODE_STATIC);


查看SDK文档,查看源码

2.JNI传递中获得AudioTrack实例


3.音频文件的信息

        int iMinBufSize = AudioTrack.getMinBufferSize(16000,  
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);
        
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                16000, //freq
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                100*iMinBufSize,
                AudioTrack.MODE_STATIC);


4.在Native层编译是提示AudioTrack未声明,注意以下:
(1) #include <media/AudioTrack.h>
(2) android::
AudioTrack* pAudioTrack = (android::AudioTrack*)env->GetIntField(AudioTrack_cls, audio_native); //命名空间


5.我原来是将视频数据读出到一个buffer里面,读入buffer的数据长度为364800。

bufferSizeInBytes

the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode, you can write data into this buffer in smaller chunks than this size. If using the AudioTrack in static mode, this is the maximum size of the sound that will be played for this instance. See getMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioTrack instance in streaming mode. Using values smaller than getMinBufferSize() will result in an initialization failure.

iMinBufSize=9600
bufferSizeInBytes  = 9600*100
文件的长度364800 《  9600*100


6.LOG打印的错误提示:

03-29 18:11:41.276: DEBUG/AudioHardware(887): [A-HAL]AudioStreamOutMarvell::setParameters +++
03-29 18:11:41.276: INFO/AudioHardware(887): AudioStreamOutMarvell::setParameters() streamstarted=3
03-29 18:11:41.276: DEBUG/AudioHardware(887): [A-HAL]AudioStreamOutMarvell::setParameters ---
03-29 18:11:41.284: DEBUG/AudioHardware(887): [A-HAL]AudioStreamOutMarvell::setParameters +++
03-29 18:11:41.284: INFO/AudioHardware(887): AudioStreamOutMarvell::setParameters() streamvolume=768
03-29 18:11:41.292: INFO/AudioHardware(887): AudioStreamOutMarvell::setParameters() stream:3, volume:0
03-29 18:11:41.308: DEBUG/AudioHardware(887): [A-HAL]AudioStreamOutMarvell::setParameters ---
03-29 18:11:41.542: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write +++
03-29 18:11:41.542: INFO/AudioHardware(887): [AVT-AUD] Output throughput: (192000),(192000),(0),(0),(230400)
03-29 18:11:41.550: ERROR/AudioHardware(887): [AVT-AUD]-------write error is Broken pipe---------------
03-29 18:11:41.550: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write ---
03-29 18:11:46.550: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write +++
03-29 18:11:46.550: INFO/AudioHardware(887): [AVT-AUD] Output throughput: (192000),(0),(230400),(192000),(230400)
03-29 18:11:46.558: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write ---
03-29 18:11:51.754: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write +++
03-29 18:11:51.754: INFO/AudioHardware(887): [AVT-AUD] Output throughput: (211200),(230400),(211200),(211200),(211200)
03-29 18:11:51.754: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write ---
03-29 18:11:56.948: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write +++
03-29 18:11:56.956: INFO/AudioHardware(887): [AVT-AUD] Output throughput: (211200),(211200),(192000),(211200),(192000)
03-29 18:11:56.956: INFO/AudioHardware(887): [AVT-AUD] AudioStreamOutMarvell::write ---

OMS采用的是Marvell处理器。


7  Mode_STATIC改为MODE_STREAM试试


    pAudioTrack->start();
    LOGE("[BeginAudioRender] start---3");
    fseek(fp, 0, SEEK_SET);
    while(!feof(fp))
    {
        memset(stream,0, 640);
        
        if(!fread(stream,1,640,fp)) return;
        LOGE("[BeginAudioRender] write---\r\n");
        LOGE("stream[0] = %02x, stream[1] = %02x stream[2] = %02x stream[3] = %02x",stream[0],stream[2],stream[2],stream[3]);
        pAudioTrack->write(stream, 640);
    }


8 最后,经过几番波折,终于解决了。升哥提醒我在播放声音的时候按侧边键,看看当前的媒体声音的音量大笑。。。居然。。居然是0,调高了音量就出声了。

因为Android系统的声音分几种,铃声、媒体。。。等等。要看你播放的声音属于哪一种,然后将对应的音量调高。

你可能感兴趣的:(android,Stream,jni,buffer,平台,output)