通过使用FFmpeg的Andr​​oid的解码音频音频、FFmpeg、Andr、oid

2023-09-04 04:31:24 作者:好听的扣扣昵称最新版

我可以使用下面的code没有问题,播放WAV文件。当尝试播放完全相同的介质中MP3格式的我只得到乱码垃圾。我相信,我从根本上误解了如何将AV codec_de code_audio3功能工作。

由于WAV文件中包含PCM数据时,它是德codeD就可以直奔AudioTrack.write功能。必须有一些额外的步骤来获得MP3音乐像这样工作。我不知道我错过了什么,但我一直在拉我的头发了一个星期了。

Java的code

 包com.rohaupt.RRD2;

进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;

进口android.app.Activity;
进口android.media.AudioFormat;
进口android.media.AudioManager;
进口android.media.AudioTrack;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.os.SystemClock;

公共类播放器扩展活动
{
    私人AudioTrack轨道;
    私人的FileOutputStream操作系统;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        createEngine();

        MediaPlayer的熔点为新的MediaPlayer();
        mp.start();

        INT BUFSIZE = AudioTrack.getMinBufferSize(32000,
                                                  AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                                                  AudioFormat.ENCODING_PCM_16BIT);


        跟踪=新AudioTrack(AudioManager.STREAM_MUSIC,
                               32000,
                               AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                               AudioFormat.ENCODING_PCM_16BIT,
                               BUFSIZE,
                               AudioTrack.MODE_STREAM);

        字节[]字节=新字节[BUFSIZE]

        尝试 {
            OS =新的FileOutputStream(/ SD卡/ a.out的,假);
        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        字符串结果=的loadFile(/ SD卡/ a.mp3,字节);

        尝试 {
            os.close();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
    }

    无效playSound(字节[] buf中,诠释大小){
        //android.util.Log.v("ROHAUPT,RAH播放);
        如果(track.getPlayState()!= AudioTrack.PLAYSTATE_PLAYING)
            track.play();
        track.write(BUF,0,大小);

        尝试 {
            os.write(BUF,0,大小);
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
    }


    私人本地无效createEngine();
    私人本地字符串的loadFile(字符串文件,byte []数组);

    / **恢复JNI。所以在初始化* /
    静态{
         的System.loadLibrary(avutil);
         的System.loadLibrary(avcore);
         的System.loadLibrary(AV codeC);
         的System.loadLibrary(avformat);
         的System.loadLibrary(avdevice);
         的System.loadLibrary(swscale);
         的System.loadLibrary(avfilter);
         的System.loadLibrary(FFMPEG);
    }
}
 

C $ C $ç

 的#include< ASSERT.H>
#包括< jni.h>
#包括< string.h中>
#包括<安卓/ log.h>

#包括libav codeC / AV codec.h
的#include了libavformat / avformat.h

#定义DEBUG_TAGROHAUPT

无效Java_com_rohaupt_RRD2_player_createEngine(JNIEnv的* ENV,JCLASS clazz所)
    {
        AV codec_init();

        av_register_all();


    }

    的jstring Java_com_rohaupt_RRD2_player_loadFile(JNIEnv的* ENV,jobject OBJ,的jstring文件,jbyteArray阵列)
    {
        jboolean isCopy;
        INT I;
        INT audioStream = -1;
        INT资源;
        INT德codeD = 0;
        INT out_size;
        AVFormatContext * pFormatCtx;
        AV codecContext *一个codecCtx;
        AV codecContext * C = NULL;
        AV codeC *一个codeC;
        AVPacket包;
        JCLASS CLS =(* ENV) - > GetObjectClass(ENV,OBJ);
        jmethodID玩=(* ENV) - >的GetMethodID(ENV,CLS,playSound,([BI)V); //在你的main函数的开头
        为const char * szfile =(* ENV) - > GetStringUTFChars(环境,文件和放大器; isCopy);
        int16_t * pAudioBuffer =(int16_t *)av_malloc(AV codeC_MAX_AUDIO_FRAME_SIZE * 2 + FF_INPUT_BUFFER_PADDING_SIZE);
        int16_t * outBuffer =(int16_t *)av_malloc(AV codeC_MAX_AUDIO_FRAME_SIZE * 2 + FF_INPUT_BUFFER_PADDING_SIZE);


        __android_log_print(ANDROID_LOG_INFO,DEBUG_TAG,RAH28启动);
        RES = av_open_input_file(安培; pFormatCtx,szfile,NULL,0,NULL);
        如果(水库!= 0)
        {
            __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH开幕输入失败,结果:[%D],RES);
            返回的文件;
        }

        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH获得流信息);
        RES = av_find_stream_info(pFormatCtx);
        如果(水库℃,)
        {
            __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH获得流信息失败,结果:[%D],RES);
            返回的文件;
        }

        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH获得音频流);
        对于(i = 0; I< pFormatCtx-> nb_streams;我++){
          如果(pFormatCtx->流[I]  - > codeC-> codec_type == AVMEDIA_TYPE_AUDIO和放大器;&安培;
             audioStream< 0){
            audioStream =我;
          }
        }


        如果(audioStream ==  -  1)
        {
            __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH找不到音频流);
            返回的文件;
        }
        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频流发现结果:[%D],RES);


        一个codecCtx = pFormatCtx->流[audioStream]  - > codeC;
        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频codeC信息载入);

        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频codeC信息[%D],C $ ccCtx-&GT一$ codec_id);

        一个codeC = AV codec_find_de codeR(A codecCtx-> codec_id);
        如果(!一个codeC){
           __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频codeC不支持);
           返回的文件;
        }
        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频codeC信息找到);


        RES = AV codec_open(A codecCtx,一个codeC);
        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频$ C $毫升装[%D] [%D],一个codecCtx-> sample_fmt,水库);

        // C = AV codec_alloc_context();
        av_init_packet(安培;包);


        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH频道[%D]采样率[%D]样本格式[%D],一个codecCtx->频道,codecCtx-> SAMPLE_RATE,一个codecCtx-> sample_fmt);


        INT X,Y;
        X = 0; Y = 0;
        而(av_read_frame(pFormatCtx,&安培;分组)GT; = 0){
            __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH框架阅读:[%D] [%D],X ++,Y);

            如果(A codecCtx-> codec_type == AVMEDIA_TYPE_AUDIO){
                        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH音频准备好了);
                        INT DATA_SIZE = AV codeC_MAX_AUDIO_FRAME_SIZE * 2 + FF_INPUT_BUFFER_PADDING_SIZE;
                        INT大小= packet.size;
                        Y = 0;
                        德codeD = 0;
                        __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH包大小:[%D],尺寸);
                        而(尺寸大于0){

                                __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH解码:[%D] [%D],X,Y ++);
                                INT LEN = AV codec_de code_audio3(A codecCtx,pAudioBuffer,和放大器; DATA_SIZE,和放大器;包);



                                __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH 1大小[%D] len个[%D] DATA_SIZE [%D] out_size [%D],尺寸,LEN,DATA_SIZE,out_size);
                                jbyte *字节=(* ENV) - > GetByteArrayElements(ENV,阵列,NULL);

                                的memcpy(字节+的codeD,pAudioBuffer,LEN); //


                                __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAGRAH 2);

                                (* ENV) - > ReleaseByteArrayElements(ENV,阵列,字节,0);
                                __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAGRAH 3);

                                (* ENV) - > CallVoidMethod(ENV,OBJ,玩,数组的len);

                                __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAGRAH 4);


                                大小 -  = LEN;
                                德codeD + = LEN;

                       }
                       av_free_packet(安培;包);
            }

     }

          //关闭视频文件
        av_close_input_file(pFormatCtx);

        // __ android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,RAH运行完毕后的结果:[%D],RES);
        (* ENV) - > ReleaseStringUTFChars(环境,文件,szfile);
        返回的文件;
    }
 

要添加一些细节。当调用这个函数WAV文件我碰到下面的日志数据。

  I / ROHAUPT(227):RAH28启动
D / ROHAUPT(227):RAH获得流信息
D / ROHAUPT(227):RAH获得音频流
D / ROHAUPT(227):RAH有个结果发现音频流:[0]
D / ROHAUPT(227):RAH音频codeC信息加载
D / ROHAUPT(227):RAH音频codeC信息[65536]
D / ROHAUPT(227):RAH音频codeC信息发现
D / ROHAUPT(227):RAH音频$ C $毫升装[1] [0]
D / ROHAUPT(227):RAH频道[2]采样率[32000]样本格式[1]
D / ROHAUPT(227):RAH框架阅读:[0] [0]
D / ROHAUPT(227):RAH音频就绪
D / ROHAUPT(227):RAH包大小:[4096]
D / ROHAUPT(227):RAH解码:[1] [0]
D / ROHAUPT(227):RAH 1尺寸[4096] len个[4096] DATA_SIZE [4096] out_size [0]
D / ROHAUPT(227):RAH 2
D / ROHAUPT(227):RAH 3
D / ROHAUPT(227):RAH 4
D / ROHAUPT(227):RAH框架阅读:[1] [1]
D / ROHAUPT(227):RAH音频就绪
...
D / ROHAUPT(227):RAH框架阅读:[924] [1]
D / ROHAUPT(227):RAH音频就绪
D / ROHAUPT(227):RAH包大小:[4096]
D / ROHAUPT(227):RAH解码:[925] [0]
D / ROHAUPT(227):RAH 1尺寸[4096] len个[4096] DATA_SIZE [4096] out_size [0]
D / ROHAUPT(227):RAH 2
D / ROHAUPT(227):RAH 3
D / ROHAUPT(227):RAH 4
D / ROHAUPT(227):RAH框架阅读:[925] [1]
D / ROHAUPT(227):RAH音频就绪
D / ROHAUPT(227):RAH包大小:[3584]
D / ROHAUPT(227):RAH解码:[926] [0]
D / ROHAUPT(227):RAH 1尺寸[3584] len个[3584] DATA_SIZE [3584] out_size [0]
D / ROHAUPT(227):RAH 2
D / ROHAUPT(227):RAH 3
D / ROHAUPT(227):RAH 4
 

当使用MP3文件我得到以下调用

  I / ROHAUPT(280):RAH28启动
D / ROHAUPT(280):RAH获得流信息
D / ROHAUPT(280):RAH获得音频流
D / ROHAUPT(280):RAH有个结果发现音频流:[0]
D / ROHAUPT(280):RAH音频codeC信息加载
D / ROHAUPT(280):RAH音频codeC信息[86017]
D / ROHAUPT(280):RAH音频codeC信息发现
D / ROHAUPT(280):RAH音频codeC加载[1] [0]
D / ROHAUPT(280):RAH频道[2]采样率[32000]样本格式[1]
D / ROHAUPT(280):RAH框架阅读:[0] [0]
D / ROHAUPT(280):RAH音频就绪
D / ROHAUPT(280):RAH包大小:[432]
D / ROHAUPT(280):RAH解码:[1] [0]
D / ROHAUPT(280):RAH 1尺寸[432] len个[432] DATA_SIZE [4608] out_size [0]
D / ROHAUPT(280):2 RAH
...
D / ROHAUPT(280):RAH框架阅读:[822] [1]
D / ROHAUPT(280):RAH音频就绪
D / ROHAUPT(280):RAH包大小:[432]
D / ROHAUPT(280):RAH解码:[823] [0]
D / ROHAUPT(280):RAH 1尺寸[432] len个[432] DATA_SIZE [4608] out_size [0]
D / ROHAUPT(280):2 RAH
D / ROHAUPT(280):3 RAH
D / ROHAUPT(280):4 RAH
D / ROHAUPT(280):RAH框架阅读:[823] [1]
D / ROHAUPT(280):RAH音频就绪
D / ROHAUPT(280):RAH包大小:[432]
D / ROHAUPT(280):RAH解码:[824] [0]
D / ROHAUPT(280):RAH 1尺寸[432] len个[432] DATA_SIZE [4608] out_size [0]
D / ROHAUPT(280):2 RAH
D / ROHAUPT(280):3 RAH
D / ROHAUPT(280):4 RAH
 

解决方案

我可以采取发现在api_example.c文件中的libav codeC的audio_de code_example code,以解决我的问题FFmpeg的源代码并修改它以适合我的需要。下面是code。只是要注意,它并没有动态地选择正确的codeC脱code用,那是后话,我会在其旁有修复以及一些其他物品。在Java code保持不变。

 无效Java_com_rohaupt_RRD2_player_Example(JNIEnv的* ENV,jobject OBJ,的jstring文件,jbyteArray阵列)
    {
        jboolean isfilenameCopy;
        为const char *文件名=(* ENV) - > GetStringUTFChars(环境,文件和放大器; isfilenameCopy);
        AV codeC * codeC;
        AV codecContext * C = NULL;
        INT out_size,LEN;
        FILE *楼* OUTFILE;
        uint8_t有* outbuf中;
        uint8_t有INBUF [AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]。
        AVPacket avpkt;
        JCLASS CLS =(* ENV) - > GetObjectClass(ENV,OBJ);
        jmethodID玩=(* ENV) - >的GetMethodID(ENV,CLS,playSound,([BI)V); //在你的main函数的开头

        av_init_packet(安培; avpkt);

        的printf(音频解码\ N);

        / *找到MPEG音频解codeR * /
        codeC = AV codec_find_de codeR(codeC_ID_MP3);
        如果(!codeC){
            fprintf中(错误,codeC找不到\ N);
            出口(1);
        }

        C = AV codec_alloc_context();

        /* 打开它 */
        如果(AV codec_open(C,codeC)小于0){
            fprintf中(错误,无法打开codeC \ N);
            出口(1);
        }

        outbuf中=的malloc(AV codeC_MAX_AUDIO_FRAME_SIZE);

        F = FOPEN(文件名,RB);
        如果(!F){
            fprintf中(错误,无法打开%s \ N,文件名);
            出口(1);
        }

        / *德code,直到EOF * /
        avpkt.data = INBUF;
        avpkt.size = FREAD(INBUF,1,AUDIO_INBUF_SIZE,F);

        而(avpkt.size大于0){
            out_size = AV codeC_MAX_AUDIO_FRAME_SIZE;
            LEN = AV codec_de code_audio3(C,(短*)outbuf中和放大器; out_size,和放大器; avpkt);
            如果(LEN℃,){
                fprintf中(错误,错误而解码的\ n);
                出口(1);
            }
            如果(out_size大于0){
                / *如果一个框架已经去codeD,它输出* /
                jbyte *字节=(* ENV) - > GetByteArrayElements(ENV,阵列,NULL);
                的memcpy(字节,outbuf中out_size); //
                (* ENV) - > ReleaseByteArrayElements(ENV,阵列,字节,0);
                (* ENV) - > CallVoidMethod(ENV,OBJ,玩,阵列,out_size);

            }
            avpkt.size  -  = LEN;
            avpkt.data + = LEN;
            如果(avpkt.size< AUDIO_REFILL_THRESH){
                / *笔芯输入缓冲器,以避免试图去code
                 *不完整的框架。代替这一点,也可以使用
                 *解析器,或通过使用合适的容器格式
                 *了libavformat。 * /
                memmove与(INBUF,avpkt.data,avpkt.size);
                avpkt.data = INBUF;
                LEN = FREAD(avpkt.data + avpkt.size,1,
                            AUDIO_INBUF_SIZE  -  avpkt.size,F);
                如果(len个大于0)
                    avpkt.size + = LEN;
            }
        }

        fclose函数(F);
        免费(outbuf中);

        AV codec_close(C);
        av_free(C);
    }
 

I can play Wav files using the below code without issues. When trying to play the exact same media in Mp3 format I only get garbled junk. I believe I am fundamentally misunderstanding how the avcodec_decode_audio3 function works.

Since the Wav file contains PCM data when it is decoded it can go straight to the AudioTrack.write function. There must be some additional step to get Mp3s to work like this. I don't know what I'm missing, but I've been pulling my hair out for a week now.

Java Code

package com.rohaupt.RRD2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;

public class player extends Activity
{
    private AudioTrack track;
    private FileOutputStream os;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createEngine();

        MediaPlayer mp = new MediaPlayer();
        mp.start();

        int bufSize = AudioTrack.getMinBufferSize(32000,
                                                  AudioFormat.CHANNEL_CONFIGURATION_STEREO, 
                                                  AudioFormat.ENCODING_PCM_16BIT);


        track = new AudioTrack(AudioManager.STREAM_MUSIC, 
                               32000, 
                               AudioFormat.CHANNEL_CONFIGURATION_STEREO, 
                               AudioFormat.ENCODING_PCM_16BIT, 
                               bufSize,
                               AudioTrack.MODE_STREAM);

        byte[] bytes = new byte[bufSize];

        try {
            os = new FileOutputStream("/sdcard/a.out",false);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String result = loadFile("/sdcard/a.mp3",bytes);

        try {
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void playSound(byte[] buf, int size) {  
        //android.util.Log.v("ROHAUPT", "RAH Playing");
        if(track.getPlayState()!=AudioTrack.PLAYSTATE_PLAYING)
            track.play();
        track.write(buf, 0, size);

        try {
            os.write(buf,0,size);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private native void createEngine();
    private native String loadFile(String file, byte[] array);

    /** Load jni .so on initialization*/ 
    static {
         System.loadLibrary("avutil"); 
         System.loadLibrary("avcore"); 
         System.loadLibrary("avcodec");
         System.loadLibrary("avformat");
         System.loadLibrary("avdevice");
         System.loadLibrary("swscale");
         System.loadLibrary("avfilter");
         System.loadLibrary("ffmpeg");
    }
}

C Code

#include <assert.h>
#include <jni.h>
#include <string.h>
#include <android/log.h>

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"

#define DEBUG_TAG "ROHAUPT"  

void Java_com_rohaupt_RRD2_player_createEngine(JNIEnv* env, jclass clazz)
    {
        avcodec_init();

        av_register_all();


    }

    jstring Java_com_rohaupt_RRD2_player_loadFile(JNIEnv* env, jobject obj,jstring file,jbyteArray array)
    {   
        jboolean            isCopy;  
        int                 i;
        int                 audioStream=-1;
        int                 res;
        int                 decoded = 0;
        int                 out_size;
        AVFormatContext     *pFormatCtx;
        AVCodecContext      *aCodecCtx;
        AVCodecContext      *c= NULL;
        AVCodec             *aCodec;
        AVPacket            packet;
        jclass              cls = (*env)->GetObjectClass(env, obj);
        jmethodID           play = (*env)->GetMethodID(env, cls, "playSound", "([BI)V");//At the begining of your main function
        const char *        szfile = (*env)->GetStringUTFChars(env, file, &isCopy);
        int16_t *           pAudioBuffer = (int16_t *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE);
        int16_t *           outBuffer = (int16_t *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE);


        __android_log_print(ANDROID_LOG_INFO, DEBUG_TAG, "RAH28 Starting");
        res = av_open_input_file(&pFormatCtx, szfile, NULL, 0, NULL);
        if(res!=0)
        {
            __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH opening input failed with result: [%d]", res);
            return file;
        }

        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting stream info");
        res = av_find_stream_info(pFormatCtx);
        if(res<0)
        {
            __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting stream info failed with result: [%d]", res);
            return file;
        }

        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting audio stream");
        for(i=0; i < pFormatCtx->nb_streams; i++) {
          if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
             audioStream < 0) {
            audioStream=i;
          }
        }


        if(audioStream==-1)
        {
            __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH couldn't find audio stream");
            return file;
        }
        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio stream found with result: [%d]", res);


        aCodecCtx=pFormatCtx->streams[audioStream]->codec;
        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info loaded");

        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info [%d]", aCodecCtx->codec_id);

        aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
        if(!aCodec) {
           __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec unsupported");
           return file;
        }
        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info found");


        res = avcodec_open(aCodecCtx, aCodec);
        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec loaded [%d] [%d]",aCodecCtx->sample_fmt,res);

        //c=avcodec_alloc_context();
        av_init_packet(&packet);


        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH channels [%d] sample rate [%d] sample format [%d]",aCodecCtx->channels,aCodecCtx->sample_rate,aCodecCtx->sample_fmt);


        int x,y;
        x=0;y=0;
        while (av_read_frame(pFormatCtx, &packet)>= 0) {
            __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH frame read: [%d] [%d]",x++,y);

            if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
                        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio ready");
                        int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE;
                        int size=packet.size;
                        y=0;
                        decoded = 0;
                        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH packet size: [%d]", size);
                        while(size > 0) {

                                __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH decoding: [%d] [%d]",x,y++);
                                int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer, &data_size, &packet);



                                __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 1 size [%d] len [%d] data_size [%d] out_size [%d]",size,len,data_size,out_size);
                                jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);

                                memcpy(bytes + decoded, pAudioBuffer, len); //


                                __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 2");

                                (*env)->ReleaseByteArrayElements(env, array, bytes, 0);
                                __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 3");

                                (*env)->CallVoidMethod(env, obj, play, array, len);

                                __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 4");


                                size -= len;
                                decoded += len;

                       }
                       av_free_packet(&packet);
            }

     }

          // Close the video file
        av_close_input_file(pFormatCtx);

        //__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH Finished Running result: [%d]", res);
        (*env)->ReleaseStringUTFChars(env, file, szfile);   
        return file;  
    }

To add some detail. When Calling this function with a Wav File I get the following Log Data

I/ROHAUPT (  227): RAH28 Starting
D/ROHAUPT (  227): RAH getting stream info
D/ROHAUPT (  227): RAH getting audio stream
D/ROHAUPT (  227): RAH audio stream found with result: [0]
D/ROHAUPT (  227): RAH audio codec info loaded
D/ROHAUPT (  227): RAH audio codec info [65536]
D/ROHAUPT (  227): RAH audio codec info found
D/ROHAUPT (  227): RAH audio codec loaded [1] [0]
D/ROHAUPT (  227): RAH channels [2] sample rate [32000] sample format [1]
D/ROHAUPT (  227): RAH frame read: [0] [0]
D/ROHAUPT (  227): RAH audio ready
D/ROHAUPT (  227): RAH packet size: [4096]
D/ROHAUPT (  227): RAH decoding: [1] [0]
D/ROHAUPT (  227): RAH 1 size [4096] len [4096] data_size [4096] out_size [0]
D/ROHAUPT (  227): RAH 2
D/ROHAUPT (  227): RAH 3
D/ROHAUPT (  227): RAH 4
D/ROHAUPT (  227): RAH frame read: [1] [1]
D/ROHAUPT (  227): RAH audio ready
...
D/ROHAUPT (  227): RAH frame read: [924] [1]
D/ROHAUPT (  227): RAH audio ready
D/ROHAUPT (  227): RAH packet size: [4096]
D/ROHAUPT (  227): RAH decoding: [925] [0]
D/ROHAUPT (  227): RAH 1 size [4096] len [4096] data_size [4096] out_size [0]
D/ROHAUPT (  227): RAH 2
D/ROHAUPT (  227): RAH 3
D/ROHAUPT (  227): RAH 4
D/ROHAUPT (  227): RAH frame read: [925] [1]
D/ROHAUPT (  227): RAH audio ready
D/ROHAUPT (  227): RAH packet size: [3584]
D/ROHAUPT (  227): RAH decoding: [926] [0]
D/ROHAUPT (  227): RAH 1 size [3584] len [3584] data_size [3584] out_size [0]
D/ROHAUPT (  227): RAH 2
D/ROHAUPT (  227): RAH 3
D/ROHAUPT (  227): RAH 4

When calling with an Mp3 file I get the following

I/ROHAUPT (  280): RAH28 Starting
D/ROHAUPT (  280): RAH getting stream info
D/ROHAUPT (  280): RAH getting audio stream
D/ROHAUPT (  280): RAH audio stream found with result: [0]
D/ROHAUPT (  280): RAH audio codec info loaded
D/ROHAUPT (  280): RAH audio codec info [86017]
D/ROHAUPT (  280): RAH audio codec info found
D/ROHAUPT (  280): RAH audio codec loaded [1] [0]
D/ROHAUPT (  280): RAH channels [2] sample rate [32000] sample format [1]
D/ROHAUPT (  280): RAH frame read: [0] [0]
D/ROHAUPT (  280): RAH audio ready
D/ROHAUPT (  280): RAH packet size: [432]
D/ROHAUPT (  280): RAH decoding: [1] [0]
D/ROHAUPT (  280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT (  280): RAH 2
...
D/ROHAUPT (  280): RAH frame read: [822] [1]
D/ROHAUPT (  280): RAH audio ready
D/ROHAUPT (  280): RAH packet size: [432]
D/ROHAUPT (  280): RAH decoding: [823] [0]
D/ROHAUPT (  280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT (  280): RAH 2
D/ROHAUPT (  280): RAH 3
D/ROHAUPT (  280): RAH 4
D/ROHAUPT (  280): RAH frame read: [823] [1]
D/ROHAUPT (  280): RAH audio ready
D/ROHAUPT (  280): RAH packet size: [432]
D/ROHAUPT (  280): RAH decoding: [824] [0]
D/ROHAUPT (  280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT (  280): RAH 2
D/ROHAUPT (  280): RAH 3
D/ROHAUPT (  280): RAH 4

解决方案

I was able to solve my problem by taking the audio_decode_example code found in the api_example.c file in libavcodec of the ffmpeg source and modify it to suit my needs. Below is the code. Just to note, it doesn't dynamically pick the correct codec to decode with, that is something I'll have to fix next along with a few other items. The java code remains untouched.

void Java_com_rohaupt_RRD2_player_Example(JNIEnv* env, jobject obj,jstring file,jbyteArray array)
    {
        jboolean            isfilenameCopy;
        const char *        filename = (*env)->GetStringUTFChars(env, file, &isfilenameCopy);
        AVCodec *codec;
        AVCodecContext *c= NULL;
        int out_size, len;
        FILE *f, *outfile;
        uint8_t *outbuf;
        uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
        AVPacket avpkt;
        jclass              cls = (*env)->GetObjectClass(env, obj);
        jmethodID           play = (*env)->GetMethodID(env, cls, "playSound", "([BI)V");//At the begining of your main function

        av_init_packet(&avpkt);

        printf("Audio decoding\n");

        /* find the mpeg audio decoder */
        codec = avcodec_find_decoder(CODEC_ID_MP3);
        if (!codec) {
            fprintf(stderr, "codec not found\n");
            exit(1);
        }

        c= avcodec_alloc_context();

        /* open it */
        if (avcodec_open(c, codec) < 0) {
            fprintf(stderr, "could not open codec\n");
            exit(1);
        }

        outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

        f = fopen(filename, "rb");
        if (!f) {
            fprintf(stderr, "could not open %s\n", filename);
            exit(1);
        }

        /* decode until eof */
        avpkt.data = inbuf;
        avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

        while (avpkt.size > 0) {
            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
            len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
            if (len < 0) {
                fprintf(stderr, "Error while decoding\n");
                exit(1);
            }
            if (out_size > 0) {
                /* if a frame has been decoded, output it */
                jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);
                memcpy(bytes, outbuf, out_size); //
                (*env)->ReleaseByteArrayElements(env, array, bytes, 0);
                (*env)->CallVoidMethod(env, obj, play, array, out_size);

            }
            avpkt.size -= len;
            avpkt.data += len;
            if (avpkt.size < AUDIO_REFILL_THRESH) {
                /* Refill the input buffer, to avoid trying to decode
                 * incomplete frames. Instead of this, one could also use
                 * a parser, or use a proper container format through
                 * libavformat. */
                memmove(inbuf, avpkt.data, avpkt.size);
                avpkt.data = inbuf;
                len = fread(avpkt.data + avpkt.size, 1,
                            AUDIO_INBUF_SIZE - avpkt.size, f);
                if (len > 0)
                    avpkt.size += len;
            }
        }

        fclose(f);
        free(outbuf);

        avcodec_close(c);
        av_free(c);
    }