Android Application
작성자 임베디드코리아
작성일23-10-23 23:32
조회1,445회
댓글0건
Kotlin 멀티미디어 : 음악 듣기
1) 버튼을 이용한 음악 듣기
-------------------------------------------------------------------------------------
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"
tools:context=".MainActivity">
<Button
android:id="@+id/plyaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
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
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var playBTN: Button
var mediaplayer : MediaPlayer?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "음악 듣기"
playBTN = findViewById(R.id.plyaButton)
mediaplayer = MediaPlayer.create(this, R.raw.sample)
//버튼을 클릭하면 mp3를 재생하도록 하자.
playBTN.setOnClickListener {
//재생할 파일을 준비한다.
mediaplayer?.start()
}
}
override fun onDestroy () {
super.onDestroy ()
mediaplayer?.release ()
}
}
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MultiMediaMusic" parent="Theme.AppCompat.Light">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.MultiMediaMusic" parent="Base.Theme.MultiMediaMusic" />
</resources>
2) Switch를 이용한 음악 듣기
-------------------------------------------------------------------------------------
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"
tools:context=".MainActivity">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="음악듣기"
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
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "음악 듣기"
var mPlayer: MediaPlayer
mPlayer = MediaPlayer.create(this, R.raw.sample)
var switch1 = findViewById<Switch>(R.id.switch1)
switch1.setOnClickListener {
if (switch1.isChecked == true)
mPlayer.start()
else
mPlayer.stop()
}
}
}
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MultiMediaSwitchMP3" parent="Theme.AppCompat.Light">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.MultiMediaSwitchMP3" parent="Base.Theme.MultiMediaSwitchMP3" />
</resources>