개발(Android)/android studio

[Android Studio/Kotlin] Volume, Music Streaming

shinyelee 2022. 5. 17. 22:04

[안드로이드 스튜디오/코틀린] 음량, 음악 재생

Volume

MainActivity.kt

package com.shinyelee.android_study2

import android.content.Context
import android.media.AudioManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.shinyelee.android_study2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private var vBinding : ActivityMainBinding? = null
    private val binding get() = vBinding!!

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        vBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

    }

    fun Audio(v: View) {
        var audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
        audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL)
        audioManager.getStreamVolume(AudioManager.STREAM_RING)
        audioManager.getStreamVolume(AudioManager.STREAM_ALARM)
    }

    override fun onDestroy() {
        vBinding = null
        super.onDestroy()
    }

}

음량 아이콘을 클릭하면
메뉴가 뜨고, 이퀄라이저 아이콘을 클릭하면
음악, 통화, 벨소리, 알람 음량이 뜸


Music Streaming

res폴더 하위에 Android Resource Directory 생성
Resource type을 raw로 변경 후 OK
파일명은 꼭 알파벳 소문자로!

activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:text="BUTTON"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.shinyelee.android_study2

import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.shinyelee.android_study2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private var vBinding : ActivityMainBinding? = null
    private val binding get() = vBinding!!

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        vBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btn.setOnClickListener { Click(binding.btn) }

    }

    fun Click(v: View) {
        val mediaPlayer = MediaPlayer.create(this, R.raw.iu_friday)
        mediaPlayer.start()
        // 중지할 땐 반대로
//        mediaPlayer.stop()
        mediaPlayer.setOnCompletionListener { mediaPlayer.release() }
    }

    override fun onDestroy() {
        vBinding = null
        super.onDestroy()
    }

}

버튼을 클릭하면 노래가 재생됨


참고

반응형