这节课我们开始利用ffmpeg和opencv在Android环境下来实现一个rtmp播放器,与第2课在PC端实现播放器的思路类似,只不过在处理音视频显示和播放的细节略有不同。
1.压缩备份上节课工程文件夹并修改工程文件夹为demo20,将demo20导入到Eclipse或者在原工程上继续下列的开发步骤。
2.右单击工程目录下的jni节点并添加一个新的C++类fmlp,如下图所示:
3.删除工程中的其它按钮,并添加id为startBtn的按钮:
<Buttonandroid: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 libinclude$(CLEAR_VARS)LOCAL_MODULE := avcodecLOCAL_SRC_FILES := lib/libavcodec.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := avdeviceLOCAL_SRC_FILES := lib/libavdevice.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := avfilterLOCAL_SRC_FILES := lib/libavfilter.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := avformatLOCAL_SRC_FILES := lib/libavformat.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := avutilLOCAL_SRC_FILES := lib/libavutil.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := postprocLOCAL_SRC_FILES := lib/libpostproc.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := swresampleLOCAL_SRC_FILES := lib/libswresample.soinclude$(PREBUILT_SHARED_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE := swscaleLOCAL_SRC_FILES := lib/libswscale.soinclude$(PREBUILT_SHARED_LIBRARY)#x264 libinclude$(CLEAR_VARS)LOCAL_MODULE := libx264LOCAL_SRC_FILES := lib/libx264.161.soinclude$(PREBUILT_SHARED_LIBRARY)#opencv libinclude$(CLEAR_VARS)LOCAL_MODULE := opencvLOCAL_SRC_FILES :=lib/libopencv_java3.soinclude$(PREBUILT_SHARED_LIBRARY)#demoinclude$(CLEAR_VARS)LOCAL_MODULE := demoLOCAL_SRC_FILES := fmlp.cpp demo.cppLOCAL_C_INCLUDES += $(LOCAL_PATH)/include##-landroid参数 for native windowsLOCAL_LDLIBS := -llog -landroid#LOCAL_LDLIBS := -llog -lzLOCAL_SHARED_LIBRARIES :=opencv libx264 avcodec avdevice avfilter avformat avutil postproc swresample swscaleinclude$(BUILD_SHARED_LIBRARY)
调试运行,能正确返回信息"Hello fmlp..."则表示程序运行正常。