FFmpeg Audio Playback Sample

It was very hard to find a proper sample code that would compile with the newest FFmpeg library, and when some compiled they didn’t play correctly. This one however runs great, the planar audio channel playback was added by me, and later support for float playback(double PCM follows the same but wasn’t added due to media files being scarce) so now it plays the majority of MP3s, WMAs, AACs … and any audio format that you can name, I guess. I modified the code that was found here http://stackoverflow.com/questions/9799560/decode-audio-using-libavcodec-and-play-using-libao many thanks to him. This code was adapted from C, so somethings have not been thoroughly converted to C++; it compiles and runs anyway.

EDIT:

Please refer to this updated code

Compile with

g++ -std=c++11 test.cpp `pkg-config --libs --cflags libavcodec libavformat libavutil ao`

For MSVC, use this libao version: https://github.com/manushanga/libao-win32

Advertisements

6 thoughts on “FFmpeg Audio Playback Sample

  1. Your code saved me a lot of headaches, it made it very clear what was going on with avconv and planar audio channels. A few quick changes for gnu c and success.

    Thank you!!

    Reply
  2. The code really helps! I didn’t even know anything about AV_SAMPLE_FMT_(XXX) before. Other code examples just write the buffer without converting them to PCM.

    Reply
  3. must be missing something. I get major distortion during playback of AV_SAMPLE_FMT_FLTP types of media files (AAC, WMA).

    I did notice the version of ao_play I’m using requires type unsigned char instead of char for the samples buffer.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s