Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:28:03+00:00 2026-06-07T21:28:03+00:00

I have integrated ffmpeg lib in my project and I can also get the

  • 0

I have integrated ffmpeg lib in my project and I can also get the information of media files. But now i have to play mp3 files using AudioTrack class in android using ffmpeg lib.

For this I have to pass byte buffer to AudioTrack but I dont know how to get byte buffer from ffmpeg and use it with AudioTrack. I also want to play file instantly without delay.

Here is my audio track code in java :

AudioTrack track;
bufferSize = AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT)
track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, mode);

//Play audio clip
track.play();

while(stream_is_over){
//Copy the decoded raw buffer from native code to "buffer" .....
............
track.write(buffer, 0, readBytes);
}

Can anyone please give me the working code to play mp3 files with audio track. I have searched a lot but haven’t find any correct answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:28:05+00:00Added an answer on June 7, 2026 at 9:28 pm

    I managed this by creating buffer of audio files and then playing it with AudioTrack class on the fly.
    Now i’m trying to pause/stop the audio file cause stopping or pausing AudioTrack is not working.

    Here is my code to pass byte buffer to my java class :

    #include <assert.h>
    #include <jni.h>
    #include <string.h>
    #include <android/log.h>
    
    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    
    #define LOG_TAG "mylib"
    #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
    #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
    
    #define AUDIO_INBUF_SIZE 20480
    #define AUDIO_REFILL_THRESH 4096
    
    void Java_ru_dzakhov_ffmpeg_test_MainActivity_createEngine(JNIEnv* env,
            jclass clazz) {
        avcodec_init();
    
        av_register_all();
    
    }
    
    jstring Java_ru_dzakhov_ffmpeg_test_MainActivity_loadFile(JNIEnv* env,
            jobject obj, jstring file, jbyteArray array) {
        {
            jboolean isfilenameCopy;
            const char * filename = (*env)->GetStringUTFChars(env, file,
                    &isfilenameCopy);
            int audioStreamIndex;
            AVCodec *codec;
            AVCodecContext *c = NULL;
            AVFormatContext * pFormatCtx;
            AVCodecContext * aCodecCtx;
            int out_size, len, audioStream = -1, i, err;
            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
    
            LOGE("source file name is %s", filename);
    
            avcodec_init();
    
            av_register_all();
    
            LOGE("Stage 1");
            /* get format somthing of source file to AVFormatContext */
            int lError;
            if ((lError = av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL))
                    != 0) {
                LOGE("Error open source file: %d", lError);
                exit(1);
            }
            if ((lError = av_find_stream_info(pFormatCtx)) < 0) {
                LOGE("Error find stream information: %d", lError);
                exit(1);
            }
            LOGE("Stage 1.5");
            LOGE("audio format: %s", pFormatCtx->iformat->name);
            LOGE("audio bitrate: %d", pFormatCtx->bit_rate);
    
            audioStreamIndex = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO,
                    -1, -1, &codec, 0);
    
            LOGE("audio codec: %s", codec->name);
    
            /* get codec somthing of audio stream to AVCodecContext */
            aCodecCtx = pFormatCtx->streams[audioStreamIndex]->codec;
            if (avcodec_open(aCodecCtx, codec) < 0) {
                LOGE("cannot open the audio codec!");
                exit(1);
            }
    
            printf("Audio decoding\n");
            LOGE("Stage 1.7");
            LOGE("S");
            codec = avcodec_find_decoder(aCodecCtx->codec_id);
            LOGE("Stage 1.8");
            if (!codec) {
                LOGE("codec not found\n");
                exit(1);
            }
            LOGE("Stage 2");
    //              c= avcodec_alloc_context();
            LOGE("Stage 3");
            /* open it */
            if (avcodec_open(aCodecCtx, codec) < 0) {
                LOGE("could upper");
                fprintf(stderr, "could not open codec\n");
                LOGE("could not open codec");
    
            }
            LOGE("Stage 4");
            outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
    
            f = fopen(filename, "rb");
            if (!f) {
                fprintf(stderr, "could not open %s\n", filename);
                LOGE("could not open");
                exit(1);
            }
    
            /* decode until eof */
            avpkt.data = inbuf;
            avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
            LOGE("Stage 5");
    
            while (avpkt.size > 0) {
    //                  LOGE("Stage 6");
                out_size = (AVCODEC_MAX_AUDIO_FRAME_SIZE / 3) * 2;
                len = avcodec_decode_audio3(aCodecCtx, (int16_t *) outbuf,
                        &out_size, &avpkt);
                LOGE("data_size %d len %d", out_size, len);
                if (len < 0) {
                    fprintf(stderr, "Error while decoding\n");
                    LOGE("DECODING ERROR");
                    LOGE("DECODING ERROR %d", len);
                    exit(1);
                }
    //                  LOGE("Stage 7");
                if (out_size > 0) {
                    /* if a frame has been decoded, output it */
    //                      LOGE("Stage 8");
                    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);
    //                      LOGE("DECODING ERROR5");
                }
                LOGE("Stage 9");
                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;
                }
            }
            LOGE("Stage 12");
            fclose(f);
            free(outbuf);
    
            avcodec_close(c);
            av_free(c);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my project,I have integrated Spring Security with CAS server authentication. Now my project
Hi I have integrated Facebook with asp.net web site through open id But now
I have integrated Facebook Like into my website.Now i need to get/list the count
I have integrated in Hudson a maven 3 project calling following sequence: clean tomcat:redeploy
I have integrated Facebook into my Android App. When a user now posts a
I have integrated sIFR3 and it shows perfectly in my home page but not
i have integrated the PayPal in to my Android App. But as i click
I have integrated Google mobile analytics into my iphone app, but for some reason
I have integrated FlexPMD into my pom.xml file but I need to change the
I have integrated Twitter and Facebook in my iPhone App successfully. Also Facebook and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.