編輯:關於 Android 程式設計
前一章我們已經用 NDK 把 FFmpeg 編成了 Android 能用的共享庫,這一章的任務很明確:先只處理音訊。把容器裡的音訊流找出來、送進解碼器、把解出來的 PCM 資料量印出來,確認整條解碼路徑是通的。
至於播放,後續會比較幾種做法:Android 原生的 AudioTrack、SDL、OpenAL ES 以及 OpenSL ES。最終版本採用 OpenSL ES,主要是為了降低 CPU 佔用率。本章先把解碼跑通,播放留到下一章。
#include <stdio.h>
#include <stdlib.h>
#include <android/log.h>
#include "VideoPlayerDecode.h"
#include "../ffmpeg/libavutil/avutil.h"
#include "../ffmpeg/libavcodec/avcodec.h"
#include "../ffmpeg/libavformat/avformat.h"
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "graduation", __VA_ARGS__))
AVFormatContext *pFormatCtx = NULL;
int audioStream, delay_time, videoFlag = 0;
AVCodecContext *aCodecCtx;
AVCodec *aCodec;
AVFrame *aFrame;
AVPacket packet;
int frameFinished = 0;
JNIEXPORT jint JNICALL Java_com_zhangjie_graduation_videopalyer_jni_VideoPlayerDecode_VideoPlayer
(JNIEnv *env, jclass clz, jstring fileName)
{
const char* local_title = (*env)->GetStringUTFChars(env, fileName, NULL);
av_register_all(); // 註冊所有支援的檔案格式與編解碼器
// 只讀取檔案標頭,並不會填充流資訊
if (avformat_open_input(&pFormatCtx, local_title, NULL, NULL) != 0)
return -1;
// 取得檔案中的流資訊;此函式會讀取 packet 並確認所有流,
// 讓 pFormatCtx->streams 指向各條流,但不會改變檔案指標,
// 讀到的 packet 留給後面的解碼流程使用
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
return -1;
// 輸出檔案資訊,也就是命令列 ffmpeg 會印出的那串詳細資料。
// 第二個參數指定要 dump 哪一條流,-1 代表讓 ffmpeg 自己決定;
// 最後一個參數表示 dump 的是不是輸出檔,我們處理的是輸入檔,所以必須為 0
av_dump_format(pFormatCtx, -1, local_title, 0);
int i = 0;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream = i;
break;
}
}
if (audioStream < 0) return -1;
aCodecCtx = pFormatCtx->streams[audioStream]->codec;
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if (avcodec_open2(aCodecCtx, aCodec, NULL) < 0) return -1;
aFrame = avcodec_alloc_frame();
if (aFrame == NULL) return -1;
int ret;
while (videoFlag != -1) {
if (av_read_frame(pFormatCtx, &packet) < 0) break;
if (packet.stream_index == audioStream) {
ret = avcodec_decode_audio4(aCodecCtx, aFrame, &frameFinished, &packet);
if (ret > 0 && frameFinished) {
int data_size = av_samples_get_buffer_size(
aFrame->linesize, aCodecCtx->channels,
aFrame->nb_samples, aCodecCtx->sample_fmt, 0);
LOGI("audioDecodec :%d", data_size);
}
}
usleep(50000);
while (videoFlag != 0) {
if (videoFlag == 1) { // 暫停
sleep(1);
} else if (videoFlag == -1) { // 停止
break;
}
}
av_free_packet(&packet);
}
av_free(aFrame);
avcodec_close(aCodecCtx);
avformat_close_input(&pFormatCtx);
(*env)->ReleaseStringUTFChars(env, fileName, local_title);
}
JNIEXPORT jint JNICALL Java_com_zhangjie_graduation_videopalyer_jni_VideoPlayerDecode_VideoPlayerPauseOrPlay
(JNIEnv *env, jclass clz)
{
if (videoFlag == 1) {
videoFlag = 0;
} else if (videoFlag == 0) {
videoFlag = 1;
}
return videoFlag;
}
JNIEXPORT jint JNICALL Java_com_zhangjie_graduation_videopalyer_jni_VideoPlayerDecode_VideoPlayerStop
(JNIEnv *env, jclass clz)
{
videoFlag = -1;
}
流程就是 FFmpeg 解碼的標準四步:avformat_open_input 打開檔案、avformat_find_stream_info 讀出流資訊、逐一掃過 nb_streams 找出 AVMEDIA_TYPE_AUDIO 的那一條、最後用 av_read_frame 搭配 avcodec_decode_audio4 迴圈解碼。解出的資料大小透過 av_samples_get_buffer_size 算出來並印在 logcat。
播放控制則靠一個全域的 videoFlag:0 為播放中、1 為暫停、-1 為停止。Java 層只要呼叫對應的 native 方法改這個旗標,解碼迴圈就會在下一輪察覺。這種用旗標溝通的做法很直觀,但要注意它是跨執行緒讀寫的共享狀態,正式專案裡應該改用 volatile 或更嚴謹的同步機制。
#######################################################
########## ffmpeg-prebuilt #######
#######################################################
# declare the prebuilt library
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg-prebuilt
LOCAL_SRC_FILES := ffmpeg/android/armv7-a/libffmpeg-neon.so
LOCAL_EXPORT_C_INCLUDES := ffmpeg/android/armv7-a/include
LOCAL_EXPORT_LDLIBS := ffmpeg/android/armv7-a/libffmpeg-neon.so
LOCAL_PRELINK_MODULE := true
include $(PREBUILT_SHARED_LIBRARY)
########################################################
## ffmpeg-test-neon.so ########
########################################################
include $(CLEAR_VARS)
TARGET_ARCH_ABI = armeabi-v7a
LOCAL_ARM_MODE = arm
LOCAL_ARM_NEON = true
LOCAL_ALLOW_UNDEFINED_SYMBOLS = false
LOCAL_MODULE := ffmpeg-test-neon
LOCAL_SRC_FILES := jniffmpeg/Decodec_Audio.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ffmpeg/android/armv7-a/include \
$(LOCAL_PATH)/ffmpeg \
$(LOCAL_PATH)/ffmpeg/libavutil \
$(LOCAL_PATH)/ffmpeg/libavcodec \
$(LOCAL_PATH)/ffmpeg/libavformat \
$(LOCAL_PATH)/ffmpeg/libswscale \
$(LOCAL_PATH)/jniffmpeg \
$(LOCAL_PATH)
LOCAL_SHARED_LIBRARY := ffmpeg-prebuilt
LOCAL_LDLIBS := -llog -ljnigraphics -lz -lm $(LOCAL_PATH)/ffmpeg/android/armv7-a/libffmpeg-neon.so
include $(BUILD_SHARED_LIBRARY)
在專案的 jni 目錄下執行 ndk-build:
root@zhangjie:/Graduation/jni# ndk-build Install : libffmpeg-neon.so => libs/armeabi/libffmpeg-neon.so Compile arm : ffmpeg-test-neon <= Decodec_Audio.c SharedLibrary : libffmpeg-test-neon.so Install : libffmpeg-test-neon.so => libs/armeabi/libffmpeg-test-neon.so
把輸出的 libffmpeg-test-neon.so 與 libffmpeg-neon.so 都複製到 Android 專案的 libs/armeabi 目錄,重新執行 App、點選影片,如果解碼成功,logcat 就會持續印出每個音訊包的大小:
06-07 04:51:30.953: I/graduation(7014): audioDecodec :2048 06-07 04:51:31.000: I/graduation(7014): audioDecodec :2048 06-07 04:51:31.109: I/graduation(7014): audioDecodec :2048 06-07 04:51:31.156: I/graduation(7014): audioDecodec :2048 06-07 04:51:31.257: I/graduation(7014): audioDecodec :2048 ...
看到這串輸出,代表「讀容器 → 找音訊流 → 解碼」整條路徑已經打通。下一章就把這些 PCM 資料真正送到 OpenSL ES 播放出來。
Android Fragment 詳解:生命週期、通訊與 FragmentTransaction
Fragment 是 Android 3.0 之後引入的重要元件,代表 Activity 中的一個模組化片段。它可以是畫面的一部分,也可以被多個 Activity reu
Android ORM 框架 greenDAO 使用指南:建立、生成與資料庫操作
前言 greenDAO 是一個輕量且高效的 ORM 方案,可將 Java 物件映射到 SQLite 資料庫。它適合需要快速建立資料表、減少樣板程式碼的 Android 專
ListView 中 CheckBox 錯位問題的原因與解決
本節引言 作為 ListView 的經典問題之一,如果你嘗試過自定義帶有 CheckBox 的 item,那麼當 item 數量超過一屏時,幾乎一定會遇到 CheckBo
Android EditText 詳解及示例代碼
EditText 在 API 中的結構 java.lang.Object → android.view.View → android.widget.TextView →