Android/Kotlin

ExoPlayer

lipnus 2018. 11. 28. 22:47
반응형

인도형님 유튜브: https://www.youtube.com/watch?v=HZuWeJ_Sa5A

공식사이트: https://google.github.io/ExoPlayer/



1.Gradle

implementation 'com.google.android.exoplayer:exoplayer:2.9.1'

이걸 하니까 

implementation 'com.android.support:appcompat-v7:27.1.1'

이 부분에서 에러가 났는데 버전을 똑같이 27.1.1로 바꿔주니까 된다.



compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

이 부분을 꼭 넣어줘야 한다.


android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.lipnus.exoplayer"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

요 자리에 넣으면 된다.



2.Permission

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




3.XML

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/exo_player_view">

</com.google.android.exoplayer2.ui.SimpleExoPlayerView>

simpleExoPlayerView는 deprecated되어있다. 일단 예제에 나온대로 구현.




4.MainActivity.kt

class MainActivity : AppCompatActivity() {

private lateinit var exoPlayer: SimpleExoPlayer

private var videoURL: String = "http://blueappsoftware.in/layout_design_android_blog.mp4"

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

try{
var bandwidthMeter: BandwidthMeter = DefaultBandwidthMeter()
var trackSelector: TrackSelector = DefaultTrackSelector(AdaptiveTrackSelection.Factory(bandwidthMeter))
exoPlayer= ExoPlayerFactory.newSimpleInstance(this, trackSelector)

val videoURI = Uri.parse(videoURL)

val dataSourceFactory = DefaultHttpDataSourceFactory("exoplayer_video")
val extractorsFactory = DefaultExtractorsFactory()
val mediaSource = ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null)

exo_player_view.setPlayer(exoPlayer)
exoPlayer.prepare(mediaSource)
exoPlayer.setPlayWhenReady(true)
}catch (e:Exception){
Log.e("MainAcvtivity"," exoplayer error "+ e.toString());
}


}
}


반응형

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

Companion Object  (0) 2018.12.09
캐스팅(Casting)  (0) 2018.12.09
Intent  (0) 2018.11.28
JSON형식을 Kotlin 데이터클래스로 만들기  (0) 2018.11.28
그래픽 라이브러리 없이 Retrofit만 사용하여 이미지 출력  (0) 2018.11.28