[FFmpeg-user] How to play MP3 songs using FFMPEG

kalla dalayya dalu547 at gmail.com
Mon Oct 28 10:53:17 CET 2013


Hi,

   Iam able to play .*wav* files using FFMPEG but unable to play
*mp3*songs using this FFMPEG.So suggest me how to play mp3 songs using
FFMPEG
with your code or through link.
***please find attachments below music.java and music.c *

Thanks &Regards
KD Naidu.

Hyderabad
-------------- next part --------------
package com.music;

import java.io.FileOutputStream;

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

public class MainActivity extends Activity 
{

	private AudioTrack track;
    private FileOutputStream os;
    byte[] bytes;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        createEngine();
        
        int bufSize = 8 * AudioTrack.getMinBufferSize(44100,
				AudioFormat.CHANNEL_OUT_STEREO,
				AudioFormat.ENCODING_PCM_16BIT);
       
        track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
				AudioFormat.CHANNEL_OUT_STEREO,
				AudioFormat.ENCODING_PCM_16BIT, bufSize,
				AudioTrack.MODE_STREAM);

        
        bytes = new byte[bufSize];
        final String wavefile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/pani_da.wav";
       
        try
        {
        	new Thread(new Runnable() 
        	{
				@Override
				public void run() 
				{
					playwav(wavefile,bytes);
					playmp3(mp3file,bytes);
				}
			}).start();
        	
        }
        catch (Exception e) 
        {
        	e.getStackTrace();
		}
    }
         //calling from music.c
    void playSound(final byte[] buf, final int size) 
    {  
    	           track.write(buf, 0, size);
			       track.play();
    }

    private native void createEngine();
    private native void playwav(String filewave, byte[] array);
	static 
	{
		
		System.loadLibrary("ffmpeg");
		
	}
}
-------------- next part --------------
#include <jni.h>

#include <assert.h>
#include <string.h>
#include <android/log.h>
#include <errno.h>
#include <math.h>

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

#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096




JNIEXPORT void JNICALL Java_com_music_MainActivity_createEngine(JNIEnv *env, jclass clazz)
{
	__android_log_print(6, "music.c", "create engine called");
	avcodec_init();
	av_register_all();

}

JNIEXPORT void JNICALL  Java_com_music_MainActivity_playwav(JNIEnv* env, jobject obj,jstring file,jbyteArray array)
{
       jboolean            isCopy;
        int                 audioStream=-1;
        int                 res;
        int                 decoded = 0;
        AVFormatContext     *pFormatCtx;
        AVCodecContext      *aCodecCtx;
        AVCodec             *aCodec;
        AVPacket            packet;

        jclass              cls = (*env)->GetObjectClass(env, obj);
        jmethodID           play = (*env)->GetMethodID(env, cls, "playSound", "([BI)V");

        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);


        res = av_open_input_file(&pFormatCtx, szfile, NULL, 0, NULL);
        res = av_find_stream_info(pFormatCtx);
        int i;
        for(i=0; i < pFormatCtx->nb_streams; i++)
        {
          if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
             audioStream < 0)
          {
            audioStream=i;
          }
        }

        aCodecCtx=pFormatCtx->streams[audioStream]->codec;
        aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
        res = avcodec_open(aCodecCtx, aCodec);
        av_init_packet(&packet);

        int x,y;
        x=0;y=0;
        while (av_read_frame(pFormatCtx, &packet)>= 0)
        {

            if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO)
            {
                        int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE;
                        int size=packet.size;
                        y=0;
                        decoded = 0;
                        while(size > 0)
                        {

                                int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer, &data_size, &packet);
                                jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);

                                memcpy(bytes + decoded, pAudioBuffer, len);
                                (*env)->ReleaseByteArrayElements(env, array, bytes, 0);
                                (*env)->CallVoidMethod(env, obj, play, array, len);

                                size -= len;
                                decoded += len;

                       }
                       av_free_packet(&packet);
            }

     }

        av_close_input_file(pFormatCtx);

        (*env)->ReleaseStringUTFChars(env, file, szfile);

 }


More information about the ffmpeg-user mailing list