MainActivity.java
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.VideoView;
import static com.example.recyclerview.Data.IMAGE_DRAWABLES;
import static com.example.recyclerview.Data.PROFILE_TEXT;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = null;
}
public void music(View view) {
switch (view.getId()) {
case R.id.btn_play:
if(mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.media_sample_01);
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopMusic();
}
});
mediaPlayer.start();
break;
case R.id.btn_pause:
if(mediaPlayer != null)
mediaPlayer.pause();
break;
case R.id.btn_stop:
if(mediaPlayer != null) {
mediaPlayer.stop();
stopMusic();
}
break;
}
}
private void stopMusic() {
mediaPlayer.release();
mediaPlayer = null;
}
@Override
protected void onStop() {
super.onStop();
stopMusic();
}
}
activity_main.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"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/text_view_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MediaPlayer Demo"
android:textSize="32dp"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerVertical="true">
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="music"
android:text="play" />
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="music"
android:text="pause" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="music"
android:text="stop" />
</LinearLayout>
</RelativeLayout>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] Image download using AsyncTask (0) | 2021.06.14 |
---|---|
[Android/Java] bitmap 예제(00) (0) | 2021.06.13 |
[Android/Java] Video view (0) | 2021.06.13 |
[Android/Java] 체크박스 (0) | 2021.06.13 |
[Android/Java] radio button (0) | 2021.06.13 |