Skip to content

Commit

Permalink
modify sdk mini version, and add ui in activity
Browse files Browse the repository at this point in the history
  • Loading branch information
weiancheng committed Dec 10, 2017
1 parent f34579a commit b940627
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package weian.cheng.mediaplayerwitexoplayer

import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.widget.Button
import weian.cheng.mediaplayerwithexoplayer.ExoPlayerWrapper

class MainActivity:AppCompatActivity() {
Expand All @@ -10,11 +15,50 @@ class MainActivity:AppCompatActivity() {

private lateinit var player: ExoPlayerWrapper

private val permissionsStorage: Array<String> = arrayOf(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE)
private val permissionsRequestCode = 1

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

player = ExoPlayerWrapper(this.applicationContext)
player.play(url)

var play = findViewById<Button>(R.id.btn_play) as Button
play.setOnClickListener { view ->
player.play(url)
}

var stop = findViewById<Button>(R.id.btn_stop) as Button
stop.setOnClickListener { view ->
player.stop()
}

var next = findViewById<Button>(R.id.btn_next) as Button
next.setOnClickListener { view ->
if (player.getPlayerState() == ExoPlayerWrapper.PlayerState.Play) {
player.stop()
}
player.play(local)
}

var previous = findViewById<Button>(R.id.btn_prev) as Button
previous.setOnClickListener { view ->
if (player.getPlayerState() == ExoPlayerWrapper.PlayerState.Play) {
player.stop()
}
player.play(url)
}
}

private fun requirePermission() {
ActivityCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE).takeIf {
it == PackageManager.PERMISSION_DENIED
}?.let {
ActivityCompat.requestPermissions(this,
permissionsStorage, permissionsRequestCode)
}
}

}
44 changes: 44 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,56 @@
tools:context="weian.cheng.mediaplayerwitexoplayer.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06"/>

<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="148dp"
android:layout_marginTop="100dp"
android:text="Play"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"/>

<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="44dp"
android:layout_marginTop="152dp"
android:text="stop"
app:layout_constraintEnd_toStartOf="@+id/btn_play"
app:layout_constraintTop_toTopOf="parent"/>

<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="next"
app:layout_constraintTop_toBottomOf="@+id/btn_stop"
tools:layout_editor_absoluteX="16dp"/>

<Button
android:id="@+id/btn_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="40dp"
android:layout_marginTop="28dp"
android:text="previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_next"
app:layout_constraintTop_toBottomOf="@+id/btn_play"
app:layout_constraintVertical_bias="0.03"/>

</android.support.constraint.ConstraintLayout>
2 changes: 1 addition & 1 deletion mediaplayerwithexoplayer2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {


defaultConfig {
minSdkVersion 23
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class ExoPlayerWrapper(context: Context) {
exoPlayer.seekTo(time.times(1000).toLong())
}

fun isPlaying(): Boolean = isPlaying

fun getPlayerState() = playerState

private fun initExoPlayer(url: String) {
Expand Down Expand Up @@ -154,7 +152,7 @@ class ExoPlayerWrapper(context: Context) {
}

override fun onPlayerError(error: ExoPlaybackException?) {
Log.i("ExoPlayerWrapper", "onPlayerError")
Log.i("ExoPlayerWrapper", "onPlayerError: " + error.toString())
}

override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
Expand Down

0 comments on commit b940627

Please sign in to comment.