Over the limit
[Kotlin] 음악 재생 프로그램 만들기 본문
MainActivity.kt 소스 코드
package com.example.musicserviceapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
class MainActivity : AppCompatActivity() {
lateinit var soundIntent:Intent
lateinit var btnStart :Button
lateinit var btnStop:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
soundIntent = Intent(this, MusicService::class.java)
btnStart = findViewById(R.id.btnStart)
btnStop = findViewById(R.id.btnStop)
btnStart.setOnClickListener{
startService(soundIntent)
Log.i("서비스 테스트", "startService()")
}
btnStop.setOnClickListener {
stopService(soundIntent)
Log.i("서비스 테스트", "stopService()")
}
}
}
activity_main.xml 소스 코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="음악서비스 시작"/>
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="음악서비스 중지"/>
</LinearLayout>
Musicservice.kt 소스코드
package com.example.musicserviceapp
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder
import android.util.Log
class MusicService : Service() {
lateinit var mp : MediaPlayer
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i("서비스 테스트", "onStartCommand()")
mp=MediaPlayer.create(this, R.raw.song)
mp.isLooping=true
mp.start()
return super.onStartCommand(intent, flags, startId)
}
override fun onCreate(){
Log.i("서비스 테스트", "onCreate()")
super.onCreate()
}
override fun onDestroy() {
Log.i("서비스 테스트", "onDestroy()")
mp.stop()
super.onDestroy()
}
}
'Framework > Android Studio' 카테고리의 다른 글
firebase 파이어베이스 구글로그인 연동하기 (0) | 2021.08.26 |
---|---|
공동구매/배달 앱 소개 영상 (0) | 2021.08.25 |
net::ERR_CACHE_MISS 오류 (0) | 2021.07.19 |
[Kotlin] 타이머 만들기 (0) | 2021.07.15 |
[Kotlin] BMI 측정기 만들기 (0) | 2021.07.15 |