ijkplayer接入Android项目

build.gradle中加入依赖

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "cn.com.codingce.mediaandroid"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "" //cppFlags '-std=c++14' // cppFlags "" // 默认包含四大平台
abiFilters 'armeabi-v7a'
// abiFilters 'arm64-v8a'
}
}

ndk {
//toolchain "clang"
//toolchainVersion "3.5"
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
// args "-DANDROID_ARM_NEON=TRUE"
abiFilters 'armeabi-v7a'
// abiFilters 'arm64-v8a'
}
}

sourceSets {
main {
jniLibs.srcDirs = ["libs"]
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

//指定 CMakeLists 路径
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}

buildFeatures {
viewBinding true
}

packagingOptions {
exclude 'lib/armeabi-v7a/libMeeLiveVideo.so'
}
}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//第三方
implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
//导入子模块
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<cn.com.codingce.mediaandroid.activity.ijk.VideoPlayerIJK
android:id="@+id/mVideoPlayerIJK"
android:layout_width="match_parent"
android:layout_height="250dp" />

</RelativeLayout>

IjkPlayer

package cn.com.codingce.mediaandroid.activity.ijk;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.com.codingce.mediaandroid.R;

public class IjkPlayer extends AppCompatActivity {

private static final String TAG = "MainActivity";

String url = "http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4";
// String url = "http://voc.hunanqubiezhen.cn/MTY1NjY1NzQ2NzY4NyMyOTQjdm9j.voc";

private VideoPlayerIJK mVideoPlayerIJK;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ijk_player);

mVideoPlayerIJK = findViewById(R.id.mVideoPlayerIJK);
mVideoPlayerIJK.setVideoPath(url);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
mVideoPlayerIJK.start();
}

@Override
protected void onDestroy() {
mVideoPlayerIJK.stop();
try {
super.onDestroy();
} catch (Exception e) {
Log.e(TAG, "onDestroy Exception" + e.getMessage());
}
}
}

VideoPlayerIJK

package cn.com.codingce.mediaandroid.activity.ijk;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
*
*/
public class VideoPlayerIJK extends FrameLayout {

private static final String TAG = "VideoPlayerIJK";

/**
* 由ijkplayer提供,用于播放视频,需要给他传入一个surfaceView
*/
private IMediaPlayer mMediaPlayer = null;

/**
* 视频文件地址
*/
private String mPath = "";

private SurfaceView surfaceView;

private VideoPlayerListener listener;
private Context mContext;

public VideoPlayerIJK(@NonNull Context context) {
super(context);
initVideoView(context);
}

public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initVideoView(context);
}

public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
initVideoView(context);
}

private void initVideoView(Context context) {
mContext = context;

//获取焦点,不知道有没有必要~。~
setFocusable(true);
}

/**
* 设置视频地址。
* 根据是否第一次播放视频,做不同的操作。
*
* @param path the path of the video.
*/
public void setVideoPath(String path) {
if (TextUtils.equals("", mPath)) {
//如果是第一次播放视频,那就创建一个新的 surfaceView
mPath = path;
createSurfaceView();
//SurfaceView 创建回调并没有load
//load();
} else {
//否则就直接load
mPath = path;
load();
}
}

/**
* 新建一个 surfaceview
*/
private void createSurfaceView() {
//生成一个新的 surface view
surfaceView = new SurfaceView(mContext);
surfaceView.getHolder().addCallback(new LmnSurfaceCallback());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.MATCH_PARENT, Gravity.CENTER);
surfaceView.setLayoutParams(layoutParams);
this.addView(surfaceView);
}

/**
* surfaceView的监听器
*/
private class LmnSurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e(TAG, "surfaceCreated: 创建完");
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//surfaceview 创建成功后, 加载视频
load();
Log.e(TAG, "surfaceChanged: 创建成功后, 加载视频");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed: 销毁");

}
}

/**
* 加载视频
*/
private void load() {
//每次都要重新创建IMediaPlayer
createPlayer();
try {
mMediaPlayer.setDataSource(mPath);
} catch (IOException e) {
e.printStackTrace();
}
//给 mediaPlayer 设置视图
mMediaPlayer.setDisplay(surfaceView.getHolder());

mMediaPlayer.prepareAsync();
}

/**
* 创建一个新的player
*/
private void createPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.setDisplay(null);
mMediaPlayer.release();
}
IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();

ijkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);

//开启硬解码
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);

mMediaPlayer = ijkMediaPlayer;

if (listener != null) {
mMediaPlayer.setOnPreparedListener(listener);
mMediaPlayer.setOnInfoListener(listener);
mMediaPlayer.setOnSeekCompleteListener(listener);
mMediaPlayer.setOnBufferingUpdateListener(listener);
mMediaPlayer.setOnErrorListener(listener);
}
}

/**
* 设置监听器
* @param listener
*/
public void setListener(VideoPlayerListener listener) {
this.listener = listener;
if (mMediaPlayer != null) {
mMediaPlayer.setOnPreparedListener(listener);
}
}

/下面封装了一下控制视频的方法/

public void start() {
if (mMediaPlayer != null) {
mMediaPlayer.start();
}
}

public void release() {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
}

public void pause() {
if (mMediaPlayer != null) {
mMediaPlayer.pause();
}
}

public void stop() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
}
}


public void reset() {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
}
}


public long getDuration() {
if (mMediaPlayer != null) {
return mMediaPlayer.getDuration();
} else {
return 0;
}
}


public long getCurrentPosition() {
if (mMediaPlayer != null) {
return mMediaPlayer.getCurrentPosition();
} else {
return 0;
}
}


public void seekTo(long l) {
if (mMediaPlayer != null) {
mMediaPlayer.seekTo(l);
}
}
}

VideoPlayerListener

package cn.com.codingce.mediaandroid.activity.ijk;

import tv.danmaku.ijk.media.player.IMediaPlayer;


/**
*
*/
public abstract class VideoPlayerListener implements IMediaPlayer.OnBufferingUpdateListener
, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnInfoListener
, IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnErrorListener
, IMediaPlayer.OnSeekCompleteListener {
}

预览

ijkplayer接入Android项目梳理_gradle