티스토리 뷰

Android/Kotlin

[Kotlin]  ExoPlayer

혀가 길지 않은 개발자 2020. 8. 15. 22:20

build.gradle (Module: app)

android {
    compileOptions {
    	targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // ExoPlayer
    implementation 'com.google.android.exoplayer:exoplayer:2.10.6'
}

 

 

 

 

 

AndroidManifest.xml

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

인터넷 권한 추가

 

 

 

 

 

 

 

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exoPlayerView"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>

</RelativeLayout>

activity_main.xml

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    private val uri: Uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
    private lateinit var simpleExoPlayer: SimpleExoPlayer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()

        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
        exoPlayerView.player = simpleExoPlayer

        var factory: DataSource.Factory = DefaultDataSourceFactory(this, "JamesKimExoPlayer")
        var mediaSource: ProgressiveMediaSource = ProgressiveMediaSource.Factory(factory).createMediaSource(uri)

        simpleExoPlayer.prepare(mediaSource)
        simpleExoPlayer.playWhenReady = true        // 자동 실행

    }

    override fun onStop() {
        super.onStop()

        exoPlayerView.player = null
        simpleExoPlayer.release()
    }
}

실제 기기로는 실행 되는데 에뮬레이터로 실행할 경우 에러 발생

 

에러 발생

 

 

 

 

 

AndroidManifest.xml

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

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

    <application
        android:usesCleartextTraffic="true"
        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>

android:usesCleartextTraffic="true"  추가

 

실행 결과

 

 

 

 

 

 


exo_player_control_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@id/exo_prev"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#BBBBFF"
        style="@style/ExoMediaButton.Previous"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"/>

    <ImageButton
        android:id="@id/exo_play"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFBBBB"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        style="@style/ExoMediaButton.Play"/>

    <ImageButton
        android:id="@id/exo_pause"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFBBBB"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        style="@style/ExoMediaButton.Pause"/>

    <ImageButton
        android:id="@id/exo_next"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#BBBBFF"
        style="@style/ExoMediaButton.Next"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

exo_player_control_view.xml

 

 

실행 결과

 

 

 

 

'Android > Kotlin' 카테고리의 다른 글

[Kotlin]  forEach  (0) 2020.08.20
[Kotlin]  MediaPlayer  (0) 2020.08.15
[Kotlin]  VideoView  (0) 2020.08.15
[Kotlin]  SeekBar  (0) 2020.08.15
[Kotlin]  @JvmOverloads  +  CustomView  (0) 2020.08.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함