ExoPlayer google 试用exoplayer:r2.3.0

来源:互联网 发布:oracle sql性能优化 编辑:程序博客网 时间:2024/06/06 07:51

ExoPlayer  2.3效果

HLS 和MP4均测试无问题




代码如下

build.xml


apply plugin: 'com.android.application'android {    compileSdkVersion 23    buildToolsVersion "25.0.2"    defaultConfig {        applicationId "com.example.test_ijk"        minSdkVersion 15        targetSdkVersion 23        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:23.4.0'    testCompile 'junit:junit:4.12'    compile 'com.google.android.exoplayer:exoplayer:r2.3.0'    compile 'com.google.android.exoplayer:exoplayer:r1.5.11'}



layout.xml

<?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:id="@+id/activity_main"    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.example.test_ijk.MainActivity">    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"        android:onClick="start"        android:text="start play" />    <com.google.android.exoplayer2.ui.SimpleExoPlayerView        android:id="@+id/simpleExoPlayerView"        android:layout_gravity="left|top"        android:layout_centerInParent="true"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        />    <View        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:visibility="gone"        class="com.google.android.exoplayer.VideoSurfaceView"        android:id="@+id/surfaceView"        android:layout_gravity="left|top"        android:layout_centerInParent="true" /></RelativeLayout>



package com.example.test_ijk;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.google.android.exoplayer2.DefaultLoadControl;import com.google.android.exoplayer2.ExoPlayerFactory;import com.google.android.exoplayer2.LoadControl;import com.google.android.exoplayer2.SimpleExoPlayer;import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;import com.google.android.exoplayer2.extractor.ExtractorsFactory;import com.google.android.exoplayer2.source.ExtractorMediaSource;import com.google.android.exoplayer2.source.MediaSource;import com.google.android.exoplayer2.source.hls.HlsMediaSource;import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;import com.google.android.exoplayer2.trackselection.TrackSelection;import com.google.android.exoplayer2.trackselection.TrackSelector;import com.google.android.exoplayer2.ui.SimpleExoPlayerView;import com.google.android.exoplayer2.upstream.BandwidthMeter;import com.google.android.exoplayer2.upstream.DataSource;import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;import com.google.android.exoplayer2.util.Util;/** * android google exoplay *///https://google.github.io/ExoPlayer/guide.html#creating-the-player//http://blog.csdn.net/dengpeng_/article/details/54910840public class MainActivity extends AppCompatActivity {    String url="";    Context context;    TrackSelection.Factory videoTrackSelectionFactory;    Handler mainHandler = new Handler();    MediaSource videoSource;    SimpleExoPlayer player ;    public void start(View v){    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        context=MainActivity.this;        setContentView(R.layout.activity_main);        // 1.创建一个默认TrackSelector        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();        TrackSelection.Factory videoTrackSelectionFactory =                new AdaptiveTrackSelection.Factory(bandwidthMeter);        TrackSelector trackSelector =                new DefaultTrackSelector(videoTrackSelectionFactory);        // 2.创建一个默认的LoadControl        LoadControl loadControl = new DefaultLoadControl();        // 3.创建播放器         player = ExoPlayerFactory.newSimpleInstance(context,trackSelector,loadControl);        SimpleExoPlayerView simpleExoPlayerView= (SimpleExoPlayerView) findViewById(R.id.simpleExoPlayerView);        // 将player关联到View上        simpleExoPlayerView.setPlayer(player);        DefaultBandwidthMeter bandwidthMeter2 = new DefaultBandwidthMeter();// Produces DataSource instances through which media data is loaded.        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,                Util.getUserAgent(context, "yourApplicationName"), bandwidthMeter2);        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();        url="http://mpv.videocc.net/ce0812b122/a/ce0812b122bf0fb49d79ebd97cbe98fa_1.mp4";    // test hls        //url="http://hls.videocc.net/ce0812b122/c/ce0812b122c492470605bc47d3388a09_3.m3u8";        if(url.contains(".m3u8")){        videoSource =new HlsMediaSource(Uri.parse(url),dataSourceFactory,null,null);        }else{            //test mp4            videoSource = new ExtractorMediaSource(Uri.parse(url),                    dataSourceFactory, extractorsFactory, null, null);        }        player.prepare(videoSource);// Prepare the player with the source.    }    @Override    protected void onDestroy() {        super.onDestroy();        player.release();    }}


原创粉丝点击