?这节课我们开始利用ffmpeg和opencv在Android环境下来实现一个rtmp播放器,与第2课在PC端实现播放器的思路类似,只不过在处理音视频显示和播放的细节略有不同。
1.压缩备份上节课工程文件夹并修改工程文件夹为demo20,将demo20导入到Eclipse或者在原工程上继续下列的开发步骤。
2.右单击工程目录下的jni节点并添加一个新的C++类fmlp,如下图所示:
3.删除工程中的其它按钮,并添加id为startBtn的按钮:
<Button
??????? android:id="@+id/startBtn"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:layout_alignParentBottom="true"
??????? android:layout_centerHorizontal="true"
??????? android:onClick="start"
??????? android:text="Start"
???? />
在MainActivity.java中添加以下代码:
Button startBtn;
startBtn=(Button)findViewById(R.id.startBtn);????????????????
public native String startFF(String rtmpURL);
public native String stopFF();
4.绑定单击事件start用于启动C++中的FFmpeg线程:
public void start(View view){?
backWidth = Integer.toString(320);
????????? backHeight=Integer.toString(240);
rtmpURL = "rtmp://192.168.0.100/vod/2.mp4";
msgTxt.setText(startFF(rtmpURL));????????????????
}?
5.在jni目录下添加fmlp类:
fmlp.h内容如下:
class fmlp {
public:
fmlp();
virtual ~fmlp();
string init();
};
fmlp.cpp内容如下:
std::stringfmlp::init() {
??? return"Hello fmlp...";
}
6.在demo.cpp中添加启动FFmpeg的代码:
JNIEXPORT jstring JNICALL Java_com_example_demo_MainActivity_startFF(JNIEnv *env, jobject obj) {
if(myFmlp==NULL){
????????? myFmlp =new fmlp();
}
std::string str=myFmlp->init();
return env->NewStringUTF(str.c_str());
}
7.在Android.mk中添加fmlp编译选项以使fmlp.cpp被编译,完整的Android.mk内容如下
LOCAL_PATH := $(call my-dir)
#ffmpeg lib
include$(CLEAR_VARS)
LOCAL_MODULE := avcodec
LOCAL_SRC_FILES := lib/libavcodec.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := avdevice
LOCAL_SRC_FILES := lib/libavdevice.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := avfilter
LOCAL_SRC_FILES := lib/libavfilter.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := avformat
LOCAL_SRC_FILES := lib/libavformat.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := avutil
LOCAL_SRC_FILES := lib/libavutil.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := postproc
LOCAL_SRC_FILES := lib/libpostproc.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := swresample
LOCAL_SRC_FILES := lib/libswresample.so
include$(PREBUILT_SHARED_LIBRARY)
include$(CLEAR_VARS)
LOCAL_MODULE := swscale
LOCAL_SRC_FILES := lib/libswscale.so
include$(PREBUILT_SHARED_LIBRARY)
#x264 lib
include$(CLEAR_VARS)
LOCAL_MODULE := libx264
LOCAL_SRC_FILES := lib/libx264.161.so
include$(PREBUILT_SHARED_LIBRARY)
#opencv lib
include$(CLEAR_VARS)
LOCAL_MODULE := opencv
LOCAL_SRC_FILES :=lib/libopencv_java3.so
include$(PREBUILT_SHARED_LIBRARY)
#demo
include$(CLEAR_VARS)
LOCAL_MODULE??? := demo
LOCAL_SRC_FILES := fmlp.cpp demo.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
##-landroid参数 for native windows
LOCAL_LDLIBS := -llog -landroid
#LOCAL_LDLIBS := -llog -lz
LOCAL_SHARED_LIBRARIES :=opencv libx264 avcodec avdevice avfilter avformat avutil postproc swresample swscale
include$(BUILD_SHARED_LIBRARY)
调试运行,能正确返回信息"Hello fmlp..."则表示程序运行正常。