Android ffmpeg 音频的解码和播放
FFmpeg在音视频开发的地位不必多说,它已经是行业的一个品牌标杆。本篇文章探讨使用FFmpeg进行音频解码,然后反射调用android系统自带的AudioTrack和OpenSL ES两种播放方式。
首先谈下FFmpeg解码流程,步骤包括:注册组件、分配FormatContext、打开音频文件、获取输入文件信息、获取音频流索引位置、获取音频解码器、打开解码器、循环读取待解码数据、解码完一帧送去播放器播放。
代码如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
add_definitions(-std=c++11)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
find_library( # Sets the name of the path variable.
android-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
android )
set(distribution_DIR ${CMAKE_SOURCE_DIR}/libs/armeabi)
add_library( avutil
SHARED
IMPORTED )
set_target_properties( avutil
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavutil-54.so )
add_library( swresample
SHARED
IMPORTED )
set_target_properties( swresample
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswresample-1.so)
add_library( avcodec
SHARED
IMPORTED )
set_target_properties( avcodec
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavcodec-56.so)
add_library( avformat
SHARED
IMPORTED )
set_target_properties( avformat
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavformat-56.so)
add_library( swscale
SHARED
IMPORTED )
set_target_properties( swscale
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswscale-3.so)
add_library( postproc
SHARED
IMPORTED )
set_target_properties( postproc
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libpostproc-53.so)
add_library( avfilter
SHARED
IMPORTED )
set_target_properties( avfilter
PROPERTIES IMPORTED_L