Created
June 26, 2018 00:40
-
-
Save leesc22/998ab1f9a45c860145bf9b16ca179793 to your computer and use it in GitHub Desktop.
MediaPlayer class in Android
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
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:orientation="vertical" | |
android:padding="16dp" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/play_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Play"/> | |
<Button | |
android:id="@+id/pause_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Pause"/> | |
<Button | |
android:id="@+id/middle_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Skip to middle"/> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<Button | |
android:id="@+id/lower_volume_button" | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:text="-"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="8dp" | |
android:paddingRight="8dp" | |
android:textSize="16sp" | |
android:text="Volume"/> | |
<Button | |
android:id="@+id/higher_volume_button" | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:text="+"/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains 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.example.android.musicplayer | |
import android.media.MediaPlayer | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.Toast | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
private lateinit var mediaPlayer: MediaPlayer | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mediaPlayer = MediaPlayer.create(this, R.raw.dog) | |
var volume = 0.5f | |
mediaPlayer.setVolume(volume, volume) | |
play_button.setOnClickListener{ | |
mediaPlayer.start() | |
} | |
pause_button.setOnClickListener{ | |
mediaPlayer.pause() | |
} | |
middle_button.setOnClickListener{ | |
if (mediaPlayer.isPlaying) { | |
val duration = mediaPlayer.duration | |
val middle = duration / 2 | |
mediaPlayer.seekTo(middle) | |
} else { | |
Toast.makeText(this, "Please click Play button.", Toast.LENGTH_SHORT).show() | |
} | |
} | |
lower_volume_button.setOnClickListener { | |
if (volume > 0f) { | |
volume -= 0.1f | |
mediaPlayer.setVolume(volume, volume) | |
} | |
} | |
higher_volume_button.setOnClickListener { | |
if (volume < 1.0f) { | |
volume += 0.1f | |
mediaPlayer.setVolume(volume, volume) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment