1、环境搭建
首先需要导入所需要的包include、armeabi-v7a。
![](https://i-blog.csdnimg.cn/blog_migrate/e88c60c2165960e1fd683d74852e7b8b.png)
然后跟项目建立连接,在CMakeList.txt,并做了相关的解释:
cmake_minimum_required(VERSION 3.4.1)
file(GLOB source_file src/main/cpp/*.cpp) //cpp文件下所有的包
# Declares and names the project.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${source_file})
include_directories(src/main/cpp/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}") 导入libs下的所有的包
target_link_libraries( # Specifies the target library.
native-lib
avfilter avformat avcodec avutil swresample swscale
# Links the target library to the log library
# included in the NDK.
log z android) //armeabi下的包
然后在build.gradle里面进行配置:
ndk {
abiFilters 'armeabi-v7a'
}
然后在native-lib下导入看看能否成功。
extern "C" {
#include <libavformat/avformat.h>
}
下面正式进入视频解码与播放的阶段:
准备阶段:
首先在创建一个类,在里面先写好准备、开始、画布等功能。
package com.example.player08;
import android.media.MediaPlayer;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
/*
提供java 进行播放 停止等操作
*/
public class DNPlayer implements SurfaceHolder.Callback {
static {
System.loadLibrary("native-lib");
}
private String dataSource;
private SurfaceHolder holder;
private OnPrepareListener listener;
/**
* 让使用 设置播放的文件 或者 直播地址
*/
public void setDataSource(String dataSource) {
this.dataSource = dataSource;
}
/**
* 设置播放显示的画布
*
* @param surfaceView
*/
public void setSurfaceView(SurfaceView surfaceView) {
holder = surfaceView.getHolder();
holder.addCallback(this);
}
public void onError(int errorCode){
System.out.println("Java接到回调:"+errorCode);
}
public void onPrepare(){
if (null != listener){
listener.onPrepare();
}
}
public void setOnPrepareListener(OnPrepareListener listener){
this.listener = listener;
}
public interface OnPrepareListener{
void onPrepare();
}
/**
* 准备好 要播放的视频
*/
public void prepare() {
native_prepare(dataSource);
}
/**
* 开始播放
*/
public void start() {
native_start();
}
/**
* 停止播放
*/
public void stop() {
}
public void release() {
holder.removeCallback(this);
}
/**
* 画布创建好了
*
* @param holder
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
/**
* 画布发生了变化(横竖屏切换、按了home都会回调这个函数)
*
* @param holder
* @param format
* @param width
* @param height
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
native_setSurface(holder.getSurface());
}
/**
* 销毁画布 (按了home/退出应用/)
*
* @param holder
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
native void native_prepare(String dataSource);
}
在MainActivity里面进行地址获取等信息:
package com.example.player08;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.player08.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
// Used to load the 'player08' library on application startup.
private DNPlayer dnPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCr