Android开发Android技术知识Android知识

exoplayer简单使用

2016-12-16  本文已影响1028人  大玩具

带你入门的小demo(大言不惭)。详细介绍请去exoplayer website。

1.gradle中添加依赖
compile 'com.google.android.exoplayer:exoplayer:r2.0.4' //权限<uses-permission android:name="android.permission.INTERNET"/>
2.布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/main_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button2" />    <com.google.android.exoplayer2.ui.SimpleExoPlayerView        android:id="@+id/view"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

3.activity

public class MainActivity extends Activity {    Button btn, btn2;    SimpleExoPlayerView view;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove titlebar        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN); //Remove notificationbar        setContentView(R.layout.activity_main);        btn = (Button) findViewById(R.id.button);        btn2 = (Button) findViewById(R.id.button2);        view = (SimpleExoPlayerView) findViewById(R.id.view);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                creteAPlayer();            }        });        btn2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                playVideo();            }        });    }    private void playVideo() {        // Measures bandwidth during playback. Can be null if not required.        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();        // Produces DataSource instances through which media data is loaded.        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(MainActivity.this,                Util.getUserAgent(MainActivity.this, "snow"), bandwidthMeter);        // Produces Extractor instances for parsing the media data.        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();        // This is the MediaSource representing the media to be played.        MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,                dataSourceFactory, extractorsFactory, null, null);        // Prepare the player with the source.        playe.prepare(videoSource);        playe.setPlayWhenReady(true);    }    SimpleExoPlayer playe;    Uri mp4VideoUri = Uri.parse("your url");    private void creteAPlayer() {        Handler mainHandler = new Handler();        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();        TrackSelection.Factory videoTrackSelectionFactory =                new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);        TrackSelector trackSelector =                new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);        // 2. Create a default LoadControl        LoadControl loadControl = new DefaultLoadControl();        // 3. Create the player        playe = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);        view.setPlayer(playe);        ArrayList a = new ArrayList();        Iterator i = a.iterator();    }}

以上。代码怎么在markdown中有序排列

上一篇 下一篇

猜你喜欢

热点阅读