前言
基于Android NDK开发之旅34--FFmpeg音频解码这篇文章,我们已经学会音频解码基本过程。
音频播放有两种方法:FFmpeg+AudioTrack(Android自带的播放工具) 、FFmpeg+OpenSL ES。
这篇文章就通过FFmpeg+AudioTrack方式对音频解码的文件进行播放操作。
1.Java中 jni声明
package com.haocai.ffmpegtest.util;
import android.view.Surface;
public class VideoPlayer {
//音频播放
public native void audioPlayer(String input);
static{
System.loadLibrary("avutil-54");
System.loadLibrary("swresample-1");
System.loadLibrary("avcodec-56");
System.loadLibrary("avformat-56");
System.loadLibrary("swscale-3");
System.loadLibrary("postproc-53");
System.loadLibrary("avfilter-5");
System.loadLibrary("avdevice-56");
System.loadLibrary("myffmpeg");
}
}
2.定义播放??锳udioTrack
package com.haocai.ffmpegtest.util;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;
/**
* Created by Xionghu on 2017/12/12.
* Desc:
*/
public class AudioUtil {
//Todo 生成JNI签名后删除 该声明
//Todo 该声明 来源于 audioTrack.write()
public int write(byte[] audioData, int offsetInBytes, int sizeInBytes) {
return 0;
}
/**
* 创建一个AudioTrack对象,用于播放
* @return
*/
public AudioTrack createAudioTrack(int sampleRateInHz, int nb_channels){
Log.i("AudioUtil", "nb_channels:"+nb_channels);
Log.i("AudioUtil", "sampleRateInHz:"+sampleRateInHz);
//固定格式的音频码流
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
//声道个数 影响声道的布局
int channelConfig;
if(nb_channels == 1){
channelConfig = android.media.AudioFormat.CHANNEL_OUT_MONO;
}else if(nb_channels == 2){
channelConfig = android.media.AudioFormat.CHANNEL_OUT_STEREO;
}else{
channelConfig = android.media.AudioFormat.CHANNEL_OUT_STEREO;
}
int bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleRateInHz, channelConfig,
audioFormat,
bufferSizeInBytes, AudioTrack.MODE_STREAM);
//audioTrack.write()
return audioTrack;
}
}
3.获取播放模块AudioTrack相关方法的签名
3.1Build ->Rebuild Project 得到编译文件classes
3.2通过javap得到方法的签名
Windows 命令模式下
D:\>CD D:\AndroidProject\FFmpegTest\app\build\intermediates\classes\debug
D:\AndroidProject\FFmpegTest\app\build\intermediates\classes\debug> javap -s -p com.haocai.ffmpegtest.util.AudioUtil
Compiled from "AudioUtil.java"
public class com.haocai.ffmpegtest.util.AudioUtil {
public com.haocai.ffmpegtest.util.AudioUtil();
descriptor: ()V
public int write(byte[], int, int);
descriptor: ([BII)I
public android.media.AudioTrack createAudioTrack(int, int);
descriptor: (II)Landroid/media/AudioTrack;
}
4.编写音频解码播放功能
ffmpeg_voicer.c
#include <com_haocai_ffmpegtest_util_VideoPlayer.h>
#include <android/log.h>
#include <android/native_window_jni.h>
#include <android/native_window.h>
#include <unistd.h>
#include <stdlib.h>
//解码
#include "include/libavcodec/avcodec.h"
//封装格式处理
#include "include/libavformat/avformat.h"
//像素处理
#include "include/libswscale/swscale.h"
//重采样
#include "include/libswresample/swresample.h"
#define LOG_TAG "ffmpegandroidplayer"
#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,FORMAT,##__VA_ARGS__);
#define LOGD(FORMAT,...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,FORMAT, ##__VA_ARGS__)
//音频解码 采样率 新版版可达48000 * 4
#define MAX_AUDIO_FRME_SIZE 2 * 44100
//音频播放
JNIEXPORT void JNICALL Java_com_haocai_ffmpegtest_util_VideoPlayer_audioPlayer
(JNIEnv *env, jobject jobj, jstring input_jstr) {
const char* input_cstr = (*env)->GetStringUTFChars(env, input_jstr, NULL);
LOGI("%s", "init");
//注册组件
av_register_all();
AVFormatContext *pFormatCtx = avformat_alloc_context();
//打开音频文件
if (avformat_open_input(&pFormatCtx, input_cstr, NULL, NULL) != 0) {
LOGI("%s", "无法打开音频文件");
return;
}
//获取输入文件信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGI("%s", "无法获取输入文件信息");
return;
}
//获取音频流索引位置
int i = 0, audio_stream_idx = -1;
for (; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_idx = i;
break;
}
}
if (audio_stream_idx == -1)
{
LOGI("%s", "找不到音频流");
return;
}
//获取解码器
AVCodecContext *pCodeCtx = pFormatCtx->streams[audio_stream_idx]->codec;
AVCodec *codec = avcodec_find_decoder(pCodeCtx->codec_id);
if (codec == NULL) {
LOGI("%s", "无法获取加码器");
return;
}
//打开解码器
if (avcodec_open2(pCodeCtx, codec, NULL) < 0) {
LOGI("%s", "无法打开解码器");
return;
}
//压缩数据
AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
//解压缩数据
AVFrame *frame = av_frame_alloc();
//frame->16bit 44100 PCM 统一音频采样格式与采样率
SwrContext *swrCtx = swr_alloc();
//输入采样率格式
enum AVSampleFormat in_sample_fmt = pCodeCtx->sample_fmt;
//输出采样率格式16bit PCM
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
//输入采样率
int in_sample_rate = pCodeCtx->sample_rate;
//输出采样率
int out_sample_rate = 44100;
//获取输入的声道布局
//根据声道个数获取默认的声道布局(2个声道,默认立体声)
//av_get_default_channel_layout(pCodeCtx->channels);
uint64_t in_ch_layout = pCodeCtx->channel_layout;
//输出的声道布局
uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
swr_alloc_set_opts(swrCtx, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, NULL);
swr_init(swrCtx);
//获取输入输出的声道个数
int out_channel_nb = av_get_channel_layout_nb_channels(out_ch_layout);
LOGI("out_count:%d", out_channel_nb);
//jclass player_class = (*env)->GetObjectClass(env,jobj);
jclass cls = (*env)->FindClass(env, "com/haocai/ffmpegtest/util/AudioUtil");
//jmethodID
jmethodID constructor_mid= (*env)->GetMethodID(env, cls,"<init>","()V");
//实例化一个AudioUtil对象(可以在constructor_mid后加参)
jobject audioutil_obj = (*env)->NewObject(env, cls, constructor_mid); //类似于AudioUtil audioutil =new AudioUtil();
//AudioTrack对象
jmethodID create_audio_track_mid =(*env)->GetMethodID(env,cls,"createAudioTrack","(II)Landroid/media/AudioTrack;");
jobject audio_track = (*env)->CallObjectMethod(env,audioutil_obj,create_audio_track_mid,out_sample_rate,out_channel_nb);
//调用AudioTrack.play方法
jclass audio_track_class = (*env)->GetObjectClass(env,audio_track);
jmethodID audio_track_play_mid = (*env)->GetMethodID(env,audio_track_class,"play","()V");
(*env)->CallVoidMethod(env,audio_track,audio_track_play_mid);
//AudioTrack.write
jmethodID audio_track_write_mid = (*env)->GetMethodID(env,audio_track_class,"write","([BII)I");
//16bit 44100 PCM 数据
uint8_t *out_buffer = (uint8_t *)av_malloc(MAX_AUDIO_FRME_SIZE);
int got_frame = 0, framecount = 0, ret;
//6.一帧一帧读取压缩的音频数据AVPacket
while (av_read_frame(pFormatCtx, packet) >= 0) {
//解码音频类型的Packet, packet 有可能是音频数据流 有可能是视频数据流
if (packet->stream_index == audio_stream_idx) {
//解码
ret = avcodec_decode_audio4(pCodeCtx, frame, &got_frame, packet);
if (ret < 0) {
LOGI("%s", "解码完成");
break;
}
//非0,正在解码
if (got_frame > 0) {
LOGI("解码:%d", framecount++);
swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRME_SIZE, (const uint8_t **)frame->data, frame->nb_samples);
//获取sample的size
int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel_nb, frame->nb_samples, out_sample_fmt, 1);
//out_buffer 缓冲区数据,转换成byte数组
jbyteArray audio_sample_array = (*env)->NewByteArray(env,out_buffer_size);
jbyte* sample_byte = (*env)->GetByteArrayElements(env,audio_sample_array,NULL);
//将out_buffer的数据复制到sample_byte
memcpy(sample_byte,out_buffer,out_buffer_size);
//同步数据 同时释放sample_byte
(*env)->ReleaseByteArrayElements(env,audio_sample_array,sample_byte,0);
//AudioTrack.write PCM数据
(*env)->CallIntMethod(env,audio_track,audio_track_write_mid,audio_sample_array,0,out_buffer_size);
//释放局部引用 否则报错JNI ERROR (app bug): local reference table overflow (max=512)
(*env)->DeleteLocalRef(env,audio_sample_array);
usleep(1000 * 16); //16ms
}
}
av_free_packet(packet);
}
av_frame_free(&frame);
av_free(out_buffer);
swr_free(&swrCtx);
avcodec_close(pCodeCtx);
avformat_close_input(&pFormatCtx);
(*env)->ReleaseStringUTFChars(env, input_jstr, input_cstr);
}
5.调用音频播放功能
/**
* 音频解码
*/
public void audioPlayer(){
/**
* 1.播放视频文件中的音频
*/
// String input = new File(Environment.getExternalStorageDirectory(),"告白气球.avi").getAbsolutePath();
/**
* 2.播放音频文件中的音频
*/
String input = new File(Environment.getExternalStorageDirectory(),"说散就散.mp3").getAbsolutePath();
VideoPlayer player = new VideoPlayer();
player.audioPlayer(input);
Log.d("Main","正在播放");
}
}
6.调用音频播放功能
Log输出
I/ffmpegandroidplayer: init
I/ffmpegandroidplayer: out_count:2
I/AudioUtil: nb_channels:2
I/AudioUtil: sampleRateInHz:44100
I/ffmpegandroidplayer: 解码:0
I/ffmpegandroidplayer: 解码:1
I/ffmpegandroidplayer: 解码:2
I/ffmpegandroidplayer: 解码:3
I/ffmpegandroidplayer: 解码:4
太多省略......