使用google开源框架Exoplayer开发自定义播放器

http://ju.outofmemory.cn/entry/143633

背景:

移动端播放视频的需求如日中天, 最近特此使用google的开源框架Exoplayer来开发Android端的视频播放器, 输出为播放器SDK及使用SDK的DEMO.

Screenshot_2015-02-06-10-07-26

本文内容:

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.

开发流程 – OverView

本人考虑到保证ExoPlayer第三方库的完整性和独立性, 及方便日后更新, 所以先将ExoPlayer打包成jar, 然后再引入到自己的工程;

工程以DEMO为主工程, 再在DEMO工程中添加player module, 以此生成playerSDK, player module libs添加ExoPlayer jar包.

34812093-3256-4FE9-82E8-32222DFBA7AD

开发流程 – 1. 打包ExoPlayer为jar包

将ExoPlayer从github上clone一份下来,  然后用终端工具进入工程根目录, 执行

./gradlew jarRelease

开发流程 – 2. 创建DEMO工程

2AF7E8A6-E975-4BD4-AFC1-90F8F3F3B7E6

8B3CBCCB-F43C-4CBF-ADEA-F8BC6A67D87D

C4F6F21B-4D32-4120-969D-605485BC46AB

开发流程 – 3. 创建PlayerSDK Module

1867C915-04ED-4A2A-A10F-659C135DEB67

FAE8A484-5D8B-4A9B-AE2A-E80460EC8A06 34632876-EA75-48B2-A4EC-602CE79FA290 9D44AA4C-AC92-4BCB-A9B3-BF9AD8D401B4 22FBC738-819F-4EA1-8359-233FBFE7D38E

开发流程 – 4. PlayerSDK UI布局

UI结构:

A23E87A3-8713-49FC-A032-2460203024E0

41F8E366-2C22-4283-AA50-97D92BEFDF0B

<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. 功能实现

配置工程添加依赖:

C2D5B07F-B82D-4C21-BCA7-ACD44366A168 A1A0450C-02DE-4245-B267-4810C907EDD6

生命周期及调用流程:

D97308CD-7896-4708-B335-C1A2FACB6F34

这里需要注意的一点是在调用Exoplayer的playWhenReady接口时需要等到surfaceView的surfaceCreated后才能调用, 否则会失败.

编译工程:

编译后在playersdk/build/outputs下会生成aar包, 即为SDK的输出包

具体代码可参考:https://github.com/kimhou/Android/tree/master/VideoDemoPro

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值