• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Playing audio files using FFMPEG lib in Android

0 posts in this topic

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 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;
AVPacket avpkt;
void Java_ru_dzakhov_ffmpeg_test_MainActivity_createEngine(JNIEnv* env, jclass clazz)
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);
if (sourceFileName == NULL)
LOGE(1, "cannot get the source file name!");
LOGI(10, "source file name is %s", sourceFileName);
/* 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);
if ((lError = av_find_stream_info(gFormatCtx)) < 0)
LOGE(1, "Error find stream information: %d", lError);
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");
else if (audioStreamIndex == AVERROR_DECODER_NOT_FOUND)
LOGE(1, "audio stream found, but no decoder is found!");
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!");
/* ???gCodec = avcodec_find_decoder(gCodecCtx->codec_id);
if (!gCodec) {
LOGE(1, "codec not found");
gCodecCtx= avcodec_alloc_context();
if (avcodec_open(gCodecCtx, gCodec) < 0) {
LOGE(1, "could not open codec");
}??? */
sourceFile = fopen(sourceFileName, "rb");
if (!sourceFile)
LOGE(1, "could not open %s", sourceFileName);
/* 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");
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)
len = avcodec_decode_audio3(gCodecCtx, (short *) outbuf, &out_size, &avpkt);
if (len < 0)
LOGE(1, "Error while decoding: %d", len);
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);
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;
LOGI(10, "end decode.");

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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0