Created
October 22, 2016 14:22
-
-
Save t-kashima/212d5359e3396cecf162d36d4b6a89e9 to your computer and use it in GitHub Desktop.
ExoPlayer2のテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.unuuu.exoplayer | |
import android.net.Uri | |
import android.os.Bundle | |
import android.os.Handler | |
import android.support.v7.app.AppCompatActivity | |
import com.google.android.exoplayer2.DefaultLoadControl | |
import com.google.android.exoplayer2.ExoPlayerFactory | |
import com.google.android.exoplayer2.SimpleExoPlayer | |
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory | |
import com.google.android.exoplayer2.source.ExtractorMediaSource | |
import com.google.android.exoplayer2.source.LoopingMediaSource | |
import com.google.android.exoplayer2.trackselection.AdaptiveVideoTrackSelection | |
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector | |
import com.google.android.exoplayer2.trackselection.MappingTrackSelector | |
import com.google.android.exoplayer2.ui.SimpleExoPlayerView | |
import com.google.android.exoplayer2.upstream.* | |
import com.google.android.exoplayer2.util.Util | |
class MainActivity : AppCompatActivity() { | |
private lateinit var mediaDataSourceFactory: DataSource.Factory | |
private var mainHandler: Handler = Handler() | |
private var player: SimpleExoPlayer? = null | |
private val bandwidthMeter = DefaultBandwidthMeter() | |
private var trackSelector: MappingTrackSelector? = null | |
private var isPlayerNeedsSource: Boolean = false | |
private lateinit var simpleExoPlayerView: SimpleExoPlayerView | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mediaDataSourceFactory = buildDataSourceFactory(bandwidthMeter) | |
simpleExoPlayerView = findViewById(R.id.activity_main_view_player) as SimpleExoPlayerView | |
simpleExoPlayerView.requestFocus() | |
} | |
override fun onResume() { | |
super.onResume() | |
if (player == null) { | |
initializePlayer() | |
} | |
} | |
override fun onPause() { | |
super.onPause() | |
if (player != null) { | |
releasePlayer() | |
} | |
} | |
private fun buildDataSourceFactory(bandwidthMeter: DefaultBandwidthMeter): DataSource.Factory { | |
return DefaultDataSourceFactory(this, bandwidthMeter, buildHttpDataSourceFactory(bandwidthMeter)) | |
} | |
private fun buildHttpDataSourceFactory(bandwidthMeter: DefaultBandwidthMeter): HttpDataSource.Factory { | |
return DefaultHttpDataSourceFactory(Util.getUserAgent(this, "ExoPlayer"), bandwidthMeter) | |
} | |
private fun initializePlayer() { | |
if (player == null) { | |
val videoTrackSelectionFactory = AdaptiveVideoTrackSelection.Factory(bandwidthMeter) | |
// 第一引数をnullにするとnullチェックで落ちる | |
trackSelector = DefaultTrackSelector(mainHandler, videoTrackSelectionFactory) | |
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, DefaultLoadControl()) | |
simpleExoPlayerView.useController = false | |
simpleExoPlayerView.player = player | |
player!!.playWhenReady = true | |
isPlayerNeedsSource = true | |
} | |
if (isPlayerNeedsSource) { | |
val uri = Uri.parse("動画のURL") | |
val mediaSource = LoopingMediaSource(ExtractorMediaSource(uri, mediaDataSourceFactory, DefaultExtractorsFactory(), null, null)) | |
player?.prepare(mediaSource, true, false) | |
isPlayerNeedsSource = false | |
} | |
} | |
private fun releasePlayer() { | |
if (player != null) { | |
player!!.release() | |
player = null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment