http://ju.outofmemory.cn/entry/143633
背景:
移动端播放视频的需求如日中天, 最近特此使用google的开源框架Exoplayer来开发Android端的视频播放器, 输出为播放器SDK及使用SDK的DEMO.
本文内容:
ExoPlayer相关内容较多, 本文仅介绍使用其来制作一简单的视频播放器SDK, 媒体格式仅支持Mp4, 播放器功能包括播放暂停, 快进快退及相关状态UI同步.
ExoPlayer:
ExoPlayer是一个google在2014年才推出的介于现有MediaPlayer和自定义媒体播放器之间的预建播放器;
由于其基于MediaPlayer API Level 16+开发, 所以其只支持API Level 16+, 即Android 4.1+, 但其支持的特性和可扩展性确实不错, 特别是支持DRM数字版权保护技术, 这在越发重视资源版权的今天是相当有用的.
相比MediaPlayer具有更多特性和可扩展性:
支持Dash和Smoothstreaming播放;
Android4.3+支持DRM(Digital Rights Management);
丰富的回调API, 可用于自定义播放器定制.
官网介绍:
http://developer.android.com/guide/topics/media/exoplayer.html
http://www.cnblogs.com/lsjwzh/p/3890405.html
github:
https://github.com/google/ExoPlayer
开发环境:
由于ExoPlayer是google使用Android Studio+Gradle来创建的工程, 因此需要安装Android Studio和Gradle.
- 本为采用osx系统开发;
- Android Studio Mac版-1641136百度网盘地址:http://pan.baidu.com/s/1eQd9TD4;
- Gradle也需要单独下载安装, 因为Android Studio中的的安装环境被墙了, https://gradle.org/;
- jar1.7;
- android sdk api level 16+;
开发流程 – OverView
本人考虑到保证ExoPlayer第三方库的完整性和独立性, 及方便日后更新, 所以先将ExoPlayer打包成jar, 然后再引入到自己的工程;
工程以DEMO为主工程, 再在DEMO工程中添加player module, 以此生成playerSDK, player module libs添加ExoPlayer jar包.
开发流程 – 1. 打包ExoPlayer为jar包
将ExoPlayer从github上clone一份下来, 然后用终端工具进入工程根目录, 执行
./gradlew jarRelease
开发流程 – 2. 创建DEMO工程
开发流程 – 3. 创建PlayerSDK Module
开发流程 – 4. PlayerSDK UI布局
UI结构:
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/action_bar_root"
android:background="#ff000000">
<view
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.exoplayer.VideoSurfaceView"
android:id="@+id/surfaceView"
android:layout_gravity="left|top"
android:layout_centerInParent="true" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/replayButton"
android:src="http://www.kimhou.net/@drawable/replay"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone"
android:background="#a0000000"/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadingIcon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="false"
android:visibility="visible" />
</RelativeLayout>
开发流程 – 5. 配置PlayerSDK
通过修改AndroidManifest.xml来配置全屏, 横屏, 网络访问权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.kimhou.playersdk" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PlayerSDKActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
开发流程 – 6. 功能实现
配置工程添加依赖:
生命周期及调用流程:
这里需要注意的一点是在调用Exoplayer的playWhenReady接口时需要等到surfaceView的surfaceCreated后才能调用, 否则会失败.
编译工程:
编译后在playersdk/build/outputs下会生成aar包, 即为SDK的输出包
具体代码可参考:https://github.com/kimhou/Android/tree/master/VideoDemoPro