[안드로이드 스튜디오/코틀린] 음량, 음악 재생
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
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()
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Fragment, Include (0) | 2022.05.18 |
---|---|
[Android Studio/Kotlin] Motion Sensor, Wi-Fi (0) | 2022.05.18 |
[Android Studio/Kotlin] Time Picker, Saving Data (0) | 2022.05.17 |
[Android Studio/Kotlin] Dialog, Share (0) | 2022.05.17 |
[Android Studio/Kotlin] Vibration, Push Notification (0) | 2022.05.17 |