Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Playing audio files using FFMPEG lib in Android


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Simply_Vipul   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 July 2012 - 06:43 AM

Hello friends,
I am having some problem w ith FFMPEG library in android naive interface. I am trying to play audio files using ffmpeg all files play nicely but files (like MP3) which have ID3 tags are not playing. I tried all the ways but its not working. I don't know where I am doing wrong.

Here is my C code for decoding audio files :
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
#include <android/log.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
#define LOG_TAG "FFmpegTest"
#define LOG_LEVEL 10
#define LOGI(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__);}
#define LOGE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);}
char *sourceFileName;
char *resultFileName;
AVFormatContext *gFormatCtx;
int audioStreamIndex;
AVCodec *gCodec;
AVCodecContext *gCodecCtx;
FILE *sourceFile, *resultFile;
int out_size, len;
uint8_t *outbuf;
uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
AVPacket avpkt;
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 *pEnv, jobject pObj, jstring source_file_name, jstring result_file_name, jbyteArray array)
    {
    jclass cls = (*pEnv)->GetObjectClass(pEnv, pObj);
    jmethodID play = (*pEnv)->GetMethodID(pEnv, cls, "playSound", "([BI)V"); //At the begining of your main function
    LOGI(10, "start decode.");
    /* get source file's name */
    sourceFileName = (char *) (*pEnv)->GetStringUTFChars(pEnv, source_file_name, NULL);
    avcodec_init();
    av_register_all();
    if (sourceFileName == NULL)
{
LOGE(1, "cannot get the source file name!");
exit(1);
}
    LOGI(10, "source file name is %s", sourceFileName);
    avcodec_init();
    av_register_all();
    /* get format somthing of source file to AVFormatContext */
    int lError;
    if ((lError = av_open_input_file(&gFormatCtx, sourceFileName, NULL, 0, NULL)) != 0)
{
LOGE(1, "Error open source file: %d", lError);
exit(1);
}
    if ((lError = av_find_stream_info(gFormatCtx)) < 0)
{
LOGE(1, "Error find stream information: %d", lError);
exit(1);
}
    LOGI(10, "audio format: %s", gFormatCtx->iformat->name);
    LOGI(10, "audio bitrate: %d", gFormatCtx->bit_rate);
    /* ???get audio stream's id and codec of source file??? */
    audioStreamIndex = av_find_best_stream(gFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &gCodec, 0);
    LOGI(10, "audioStreamIndex %d", audioStreamIndex);
    if (audioStreamIndex == AVERROR_STREAM_NOT_FOUND)
{
LOGE(1, "cannot find a audio stream");
exit(1);
}
    else if (audioStreamIndex == AVERROR_DECODER_NOT_FOUND)
{
LOGE(1, "audio stream found, but no decoder is found!");
exit(1);
}
    LOGI(10, "audio codec: %s", gCodec->name);
    /* get codec somthing of audio stream to AVCodecContext */
    gCodecCtx = gFormatCtx->streams[audioStreamIndex]->codec;
    if (avcodec_open(gCodecCtx, gCodec) < 0)
{
LOGE(1, "cannot open the audio codec!");
exit(1);
}
    /* ???gCodec = avcodec_find_decoder(gCodecCtx->codec_id);
	 if (!gCodec) {
	 LOGE(1, "codec not found");
	 return;
	 }
	 gCodecCtx= avcodec_alloc_context();
	 if (avcodec_open(gCodecCtx, gCodec) < 0) {
	 LOGE(1, "could not open codec");
	 exit(1);
	 }??? */
    outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
    sourceFile = fopen(sourceFileName, "rb");
    if (!sourceFile)
{
LOGE(1, "could not open %s", sourceFileName);
exit(1);
}
    /* get result file's name */
    resultFileName = (char *) (*pEnv)->GetStringUTFChars(pEnv, result_file_name, NULL);
    LOGI(10, "result file name is %s", resultFileName);
    resultFile = fopen(resultFileName, "wb");
    if (!resultFile)
{
LOGE(1, "could not create result file");
av_free(gCodecCtx);
exit(1);
}
    av_init_packet(&avpkt);
    avpkt.data = inbuf;
    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, sourceFile);
    LOGI(10, "avpkt.size: %d", avpkt.size);
    /* decode file */
    while (avpkt.size > 0)
{
out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
len = avcodec_decode_audio3(gCodecCtx, (short *) outbuf, &out_size, &avpkt);
if (len < 0)
	 {
	 LOGE(1, "Error while decoding: %d", len);
	 exit(1);
	 }
if (out_size > 0)
	 {
	 /* if a frame has been decoded, output it */
	 //					  LOGE("Stage 8");
	 jbyte *bytes = (*pEnv)->GetByteArrayElements(pEnv, array, NULL);
	 memcpy(bytes, outbuf, out_size); //
	 (*pEnv)->ReleaseByteArrayElements(pEnv, array, bytes, 0);
	 (*pEnv)->CallVoidMethod(pEnv, pObj, play, array, out_size);
	 //					  LOGE("DECODING ERROR5");
	 }
avpkt.size -= len;
avpkt.data += len;
if (avpkt.size < AUDIO_REFILL_THRESH)
	 {
	 memmove(inbuf, avpkt.data, avpkt.size);
	 avpkt.data = inbuf;
	 len = fread(avpkt.data + avpkt.size, 1, AUDIO_INBUF_SIZE - avpkt.size, sourceFile);
	 if (len > 0)
  avpkt.size += len;
	 }
}
    fclose(resultFile);
    fclose(sourceFile);
    free(outbuf);
    avcodec_close(gCodecCtx);
    av_free(gCodecCtx);
    LOGI(10, "end decode.");
    }

I am passing byte array to a function 'playSound' in my Java class.

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS