본문 바로가기
개발 공부/안드로이드 스튜디오(코틀린)

안드로이드 스튜디오(노래 재생)

by momo'sdad 2023. 10. 18.

● 노래 담을 Android Resource Directory 설정

 

 

Player 코드 및 화면

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayerActivity">

    <Button
        android:id="@+id/btnStart"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:backgroundTint="#673AB7"
        android:text="재생"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnPause"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:text="일시정지"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/btnStart"
        app:layout_constraintEnd_toEndOf="@+id/btnStart"
        app:layout_constraintStart_toStartOf="@+id/btnStart" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:text="멈춤"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="@+id/btnStart"
        app:layout_constraintStart_toStartOf="@+id/btnStart"
        app:layout_constraintTop_toBottomOf="@+id/btnStart" />

    <Button
        android:id="@+id/btnPre"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:text="이전곡"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/btnStart"
        app:layout_constraintEnd_toStartOf="@+id/btnStart"
        app:layout_constraintTop_toTopOf="@+id/btnStart" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:text="다음곡"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/btnStart"
        app:layout_constraintStart_toEndOf="@+id/btnStart"
        app:layout_constraintTop_toTopOf="@+id/btnStart" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Player 화면의 ID: - btnStart: 재생 버튼

- btnPause: 일시정지 버튼

- btnStop: 멈춤 버튼

- btnpre: 이전곡 버튼

- btnNext: 다음곡 버튼

 PlayerActivity 코드

package com.example.ex221004;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;

import com.example.ex221004.databinding.ActivityPlayerBinding;

public class PlayerActivity extends AppCompatActivity {

    ActivityPlayerBinding binding;
    MediaPlayer mp3;
    int[] musics; // 노래 담을 배열
    int index; // 노래의 현재 위치

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityPlayerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        musics = new int[]{R.raw.dolphin, R.raw.dancemonkey, R.raw.eight, R.raw.redsunset};
        index = 0;
        mp3 = MediaPlayer.create(PlayerActivity.this, R.raw.dolphin);

        binding.btnNext.setOnClickListener(view -> {
            index++;
            if(index == 4){
                index = 0;
            }
            if(mp3.isPlaying()==true){
                mp3.stop();
            }
            mp3 = MediaPlayer.create(PlayerActivity.this, musics[index]);
            mp3.start();
        });
        binding.btnPre.setOnClickListener(view -> {
            index--;
            if(index == -1){
                index = 3;
            }
            if(mp3.isPlaying()==true){
                mp3.stop();
            }
            mp3 = MediaPlayer.create(PlayerActivity.this, musics[index]);
            mp3.start();
        });
        binding.btnStart.setOnClickListener(view -> {
            if(mp3.isPlaying()==false) {
                mp3.start();
                }
            });
        binding.btnPause.setOnClickListener(view -> {
            mp3.pause();
        });
        binding.btnStop.setOnClickListener(view -> {
            if(mp3.isPlaying()==true){
                mp3.stop();
            }
        });
    }
}

int[] musics; // 노래 담을 배열

int index; // 노래의 현재 위치

musics = new int[]{R.raw.dolphin, R.raw.dancemonkey, R.raw.eight, R.raw.redsunset};

# 노래 목록 리스트

index = 0; // 인덱스 초기화

  • 다음곡 버튼 눌렀을때

binding.btnNext.setOnClickListener(view -> {

index++;

if(index == 4){ # 인덱스가 4가되면 -> 0으로

index = 0;

}

if(mp3.isPlaying()==true){ # 재생되고 있을때

mp3.stop();

}

  • 이전곡 버튼 눌렀을때

binding.btnPre.setOnClickListener(view -> {

index--;

if(index == -1){ # 인덱스가 -1이되면 -> 3으로

index = 3;

}

if(mp3.isPlaying()==true){ # 재생되고 있을때

mp3.stop();

}

mp3 = MediaPlayer.create(PlayerActivity.this, musics[index]);

mp3.start();

});

  • 재생 버튼 눌렀을때

binding.btnStart.setOnClickListener(view -> {

if(mp3.isPlaying()==false) {

mp3.start();

}

});

  • 멈춤 버튼 눌렀을때

binding.btnStop.setOnClickListener(view -> {

if(mp3.isPlaying()==true){ # 재생되고 있을때

mp3.stop();

}

});

  • 일지정지 버튼 눌렀을때

binding.btnPause.setOnClickListener(view -> {

mp3.pause();

});

반응형