Skip to content

Commit

Permalink
[simba/#23] feat::5주차 구현 완료
Browse files Browse the repository at this point in the history
mediaplayer 구현
sharedpreferences 구현
  • Loading branch information
BAEK0111 committed May 12, 2024
1 parent 092cf05 commit f60d975
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
1 change: 1 addition & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
31 changes: 22 additions & 9 deletions UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,23 @@ import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.example.umc_6th.databinding.ActivityMainBinding
import com.google.gson.Gson

class MainActivity : AppCompatActivity() {

lateinit var binding: ActivityMainBinding
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
private var song : Song = Song()
private var gson : Gson = Gson()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.SplashTheme)
setTheme(R.style.Theme_UMC_6th)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

initBottomNavigation()

val song = Song(
binding.mainMiniplayerTitleTv.text.toString(),
binding.mainMiniplayerSingerTv.text.toString(),
0,
60,
false
)

activityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
Expand All @@ -49,6 +44,7 @@ class MainActivity : AppCompatActivity() {
intent.putExtra("second", song.second)
intent.putExtra("playTime", song.playTime)
intent.putExtra("isPlaying", song.isPlaying)
intent.putExtra("music",song.music)
// startActivity(intent)
activityResultLauncher.launch(intent)
}
Expand Down Expand Up @@ -94,4 +90,21 @@ class MainActivity : AppCompatActivity() {
false
}
}
override fun onStart() {
super.onStart()
val sharedPreferences = getSharedPreferences("song", MODE_PRIVATE)
val jsonToSong = sharedPreferences.getString("songData", null)
Log.d("jsonToSong", jsonToSong.toString())
song = if(jsonToSong == null) { // 최초 실행 시
Song("라일락", "아이유(IU)", 0, 60, false, "music_lilac")
} else { // SongActivity에서 노래가 한번이라도 pause 된 경우
gson.fromJson(jsonToSong, Song::class.java)
}
setMiniPlayer(song)
}
private fun setMiniPlayer(song : Song) {
binding.mainMiniplayerTitleTv.text = song.title
binding.mainMiniplayerSingerTv.text = song.singer
binding.mainMiniplayerProgressSb.progress = (song.second * 100000 / song.playTime)
}
}
3 changes: 2 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ data class Song(
val singer : String = "",
var second : Int = 0,
var playTime : Int = 60,
var isPlaying : Boolean = false
var isPlaying : Boolean = false,
var music : String = ""
)
40 changes: 37 additions & 3 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.umc_6th

import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivitySongBinding
import com.google.gson.Gson
import java.util.Timer

class SongActivity : AppCompatActivity() {
Expand All @@ -14,6 +16,9 @@ class SongActivity : AppCompatActivity() {
lateinit var song: Song
lateinit var timer : Timer

private var mediaPlayer : MediaPlayer? = null
private var gson : Gson = Gson()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySongBinding.inflate(layoutInflater) // binding 초기화
Expand Down Expand Up @@ -47,14 +52,36 @@ class SongActivity : AppCompatActivity() {
}
}

override fun onPause() {
super.onPause()
setPlayerStatus(false)

song.second = (song.playTime * binding.songProgressSb.progress) / 100000
val sharedPreferences = getSharedPreferences("song", MODE_PRIVATE)
val editor = sharedPreferences.edit()
val songToJson = gson.toJson(song)
editor.putString("songData", songToJson)
Log.d("songData", songToJson.toString())
editor.apply()
}

override fun onDestroy() {
super.onDestroy()
timer.interrupt()
mediaPlayer?.release() // 미디어 플레이어가 갖고 있던 리소스를 해제한다.
mediaPlayer = null // 미디어 플레이어를 해제한다.
}


private fun initSong(){
if(intent.hasExtra("title") && intent.hasExtra("singer")){
song = Song(
intent.getStringExtra("title")!!,
intent.getStringExtra("singer")!!,
intent.getIntExtra("second",0),
intent.getIntExtra("playTime",0),
intent.getBooleanExtra("isPlaying",false)
intent.getBooleanExtra("isPlaying",false),
intent.getStringExtra("music")!!
)
}
startTimer()
Expand All @@ -67,19 +94,26 @@ class SongActivity : AppCompatActivity() {
binding.songEndTimeTv.text = String.format("%02d:%02d",song.playTime / 60,song.playTime % 60)
binding.songProgressSb.progress = (song.second * 1000 / song.playTime)

val music = resources.getIdentifier(song.music,"raw",this.packageName)
mediaPlayer = MediaPlayer.create(this,music)
setPlayerStatus(song.isPlaying)
}

fun setPlayerStatus(isPlaying : Boolean){
song.isPlaying = isPlaying
timer.isPlaying = isPlaying

if(isPlaying){
if(isPlaying){ // 재생중
binding.songMiniplayerIv.visibility= View.GONE
binding.songPauseIv.visibility = View.VISIBLE
} else{
mediaPlayer?.start()
} else{ // 일시정지
binding.songMiniplayerIv.visibility=View.VISIBLE
binding.songPauseIv.visibility = View.GONE

if(mediaPlayer?.isPlaying == true){
mediaPlayer?.pause()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions UMC_6th/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<SeekBar
android:id="@+id/main_miniplayer_progress_sb"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="10dp"
android:background="@null"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:layout_marginBottom="-3dp"
android:layout_marginBottom="-4dp"
android:progressBackgroundTint="@color/song_player_bg"
android:progressTint="@color/song_player"
android:progress="0"
Expand Down
Binary file added UMC_6th/app/src/main/res/raw/music_lilac.mp3
Binary file not shown.

0 comments on commit f60d975

Please sign in to comment.