open coding

open coding. 안드로이드 스튜디오 웹비디오 재생하기. ExoPlayer

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2021. 1. 15. 23:18
반응형

ExoPlayer: 비디오 재생 최신버전.

(SimpleExoPlayer(video), PlayerView)

//최신버전 PlayerView 이용해서 비디오(exoPlayer) 재생하기 - 웹비디오 재생.

//playerView 에 이미 여러 controller들이 들어있다.

 

1.

<gradle.app> ExoPlayer library 추가해야함(dependency) - github.com/google/ExoPlayer

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.www.exoplayer"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.google.android.exoplayer:exoplayer-core:2.8.2'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.2'

}

/*
ExoPlayer library 추가해줘야함.
dash는 쓰지않을거라 지우고, 숫자는 크게 선호하는걸로 걍. 어차피 안되면 오류 뜨니깐.

implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'*/

2.

<Manifest> 인터넷 권한 - 웹비디오 불러오니

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.www.exoplayer">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
        </activity>
    </application>

</manifest>

3.

<MainActivity>

package com.www.exoplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;

import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

public class MainActivity extends AppCompatActivity {
                    //최신버전 PlayerView 이용해서 비디오(exoPlayer) 재생하기 - 웹비디오 재생.
                    //playerView 에 이미 여러 controller들이 들어있다.

            //내가 이용할 웹비디오 주소 : http://buildappswithpaulo.com/videos/outro_music.mp4



    //define variables
    private PlayerView playerView;      //비디오 재생시 맨앞 화면으로 띄울 playerView
    private SimpleExoPlayer player;     //비디오

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //connect to xml
        playerView = findViewById(R.id.playerView);

    }//finish



//비디오 설정 및 start 설정
    @Override
    protected void onStart() {
        super.onStart();

        player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
                //defaultTrackSelector : defualt configuration
        playerView.setPlayer(player);


        DataSource.Factory datasourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, getString(R.string.app_name)));
        MediaSource videoSource = new ExtractorMediaSource.Factory(datasourceFactory)
                .createMediaSource(Uri.parse("http://buildappswithpaulo.com/videos/outro_music.mp4"));



        player.prepare(videoSource);

    }

//onDestory 설정 잊지 말기

    @Override
    protected void onStop() {
        super.onStop();
        playerView.setPlayer(null);
        player.release();
        player = null;
    }
}