Android使用ijkplayer+nginx进行视频直播

对于直播的播放器,目前大部分的软件都是通过实现ffmpeg来进行录制、转换以及流化音视频的一套解决方案,不过由于编译与开放FFMPEG是基于Linux来编译的,可能会遇到很多的问题,所以做起来也并不是这么容易,所以我们可以通过一些开源的工具接入到我们的视频直播播放器。这个就是ijkplayer,目前花椒、映客的客户端都是通过这个开源的工具来实现直播功能的。如果对我的文章感兴趣,欢迎订阅我的头条号:一点热,yeehot.com

上一节,我介绍了如何使用使用Nginx搭建视频直播服务器,那么,今天我们继续回以这个服务器作为推流的接收地址,那么我们开始如何嵌入ijkplayer到我们的应用

快速入口

使用Nginx搭建视频直播服务器

ijkplayer的github地址为:https://github.com/Bilibili/ijkplayer

我们需要将ijkplayer的源码下载到本地,在下载前,我们需要做一些准备工作

我这里的环境是mac,如果不是MAC的话,也可以运行在Linux下,其实也是一样,windows就需要安装cgwin

1、首先我们需要安装homebrew, git, yasm

这里我就不做详细的介绍,毕竟这个不是今天的重点

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew install git

brew install yasm

2、配置Android sdk和Android NDK环境变量

我们从Android官网下载相应的SDK和NDK,大家最好就下载最新版本的NDK,我这里是

NDKr12.1.2977051 detected

下载好后大家可以直接输入

vi /etv/profile

或者是

sudo vi ~/.bash_profile

source ~/.bash_profile

3、下载ijkplaer

以下这几个过程的时间可能会比较长,大家耐心的等待吧。

git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-android

cd ijkplayer-android

git checkout -B latest k0.6.0

./init-android.sh

cd android/contrib

./compile-ffmpeg.sh clean

./compile-ffmpeg.sh all

cd ..

./compile-ijk.sh all

编译的过程会,显示出一大堆的C相关的数据,最后转换成.so文件

最后在Android目录下,会看到很多的架构的文件,点击进去会看到我们需要的.so文件


4、将ijkplayer添加到我们的项目

我们点击Android studio的file->new->import module,然后选择我们刚刚下载的ijkplayer-android项目,选中ijkplayer

然后会出现如下的提示,我这里由于项目已经添加了,所以出现警告,我们不要把example添加进去就好了。我的项目也把exo删除了。

5、修改一下我们的build.gradle,特别要注意黑体字部分

代码如下

android {

compileSdkVersion 23

buildToolsVersion "23.0.2"

allprojects {

repositories {

jcenter()

}

}

defaultConfig {

applicationId "com.yeehot.lession1"

minSdkVersion 9

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

ext {

compileSdkVersion = 23

buildToolsVersion = "23.0.2"

minSdkVersion = 9

targetSdkVersion = 23

}

dependencies {

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

testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.4.0'

compile 'com.android.support:preference-v7:23.4.0'

compile 'com.squareup:otto:1.3.8'

compile project(':ijkplayer-java')

compile project(':ijkplayer-armv7a')

}

6、根据需要把sample里面的widget和services部分代码复制到自己的项目中,

我项目复制的代码如下图所示

7、修改AndroidManifest.xml,播放的页面要添加播放的种类,还有添加我们的播放服务MediaPlayerService

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="video/*" />

<data android:mimeType="audio/*" />

<data android:scheme="http" />

<data android:scheme="file" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="video/*" />

</intent-filter>

</activity>

<service

android:name="com.yeehot.lession1.services.MediaPlayerService"

android:enabled="false"

android:exported="false"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name" >

</service>

</application>

</manifest>

8、最后我们可以在我们的播放页面写代码了,推送流我们用上一节服务器的地址rtmp://192.168.3.6/live

public class MainActivity extends AppCompatActivity {

private String mVideoPath;

private Uri mVideoUri;

private IjkVideoView mVideoView;

private Settings mSettings;

private boolean mBackPressed;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 加载静态SO

IjkMediaPlayer.loadLibrariesOnce(null);

IjkMediaPlayer.native_profileBegin("libijkplayer.so");

mSettings = new Settings(this);

// handle arguments

//yeehot.com

mVideoPath = "rtmp://192.168.3.6/live";

// mMediaController.setSupportActionBar(actionBar);

mVideoView = (IjkVideoView) findViewById(R.id.video_view);

mVideoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {

@Override

public boolean onInfo(IMediaPlayer mp, int what, int extra) {

Log.e("直播输出",mp.getDuration()+"-"+what+"-"+extra);

return false;

}

});

// prefer mVideoPath

if (mVideoPath != null)

mVideoView.setVideoPath(mVideoPath);

else if (mVideoUri != null)

mVideoView.setVideoURI(mVideoUri);

else {

// Log.e(TAG, "Null Data Source\n");

finish();

return;

}

mVideoView.start();

}

@Override

public void onBackPressed() {

mBackPressed = true;

super.onBackPressed();

}

@Override

protected void onStop() {

super.onStop();

if (mBackPressed || !mVideoView.isBackgroundPlayEnabled()) {

mVideoView.stopPlayback();

mVideoView.release(true);

mVideoView.stopBackgroundPlay();

} else {

mVideoView.enterBackground();

}

IjkMediaPlayer.native_profileEnd();

}

}

我们这个main.xml布局为

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.yeehot.lession1.MainActivity"

android:background="@android:color/transparent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="你好,yeehot.com。怎么找你?" />

<com.yeehot.lession1.widget.media.IjkVideoView

android:id="@+id/video_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center"></com.yeehot.lession1.widget.media.IjkVideoView>

</FrameLayout>

最终效果如下

实际的还需要我们修改,我这里只是简单的加入我们的播放器

今天就讲到这里,欢迎继续关注我的头条号:一点热,yeehot.com,如果有什么问题,欢迎留言咨询,我看到之后会第一时间回复大家的。也欢迎收藏与转发,如果需要转载到其他网站,请与我联系

举报
评论 0