Skip to content

Commit

Permalink
[Maro/#23] feat :: 5주차 구현 (#26)
Browse files Browse the repository at this point in the history
* [maro/#23] feat :: 5주차 구현 완료 But Memo 실행 안 됨 이슈 발생

* [maro/#23] feat :: 5주차 구현 완료 But Memo 실행 안 됨 이슈 발생

* [maro/#23] feat :: 5주차 구현 완료
  • Loading branch information
leesumin0526 authored May 18, 2024
1 parent 965b364 commit 82f6178
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 28 deletions.
66 changes: 57 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ android {

dependencies {

implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.fragment:fragment-ktx:1.3.0")
implementation ("androidx.viewpager2:viewpager2:1.0.0")
Expand Down
4 changes: 4 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
Expand All @@ -13,6 +14,9 @@ import java.util.Timer
import java.util.TimerTask
import android.os.Handler
import android.os.Looper
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class HomeFragment : Fragment() {

Expand Down
49 changes: 33 additions & 16 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 @@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.example.umc_6th.databinding.ActivityMainBinding
Expand All @@ -13,43 +14,59 @@ 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()
)

activityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data
if (data != null) {
val message = data.getStringExtra("message")
Log.d("message", message!!)
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data
if (data != null) {
val message = data.getStringExtra("message")
Log.d("message", message!!)
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
}

binding.mainPlayerCl.setOnClickListener {
val intent = Intent(this, SongActivity::class.java)
intent.putExtra("title", song.title)
intent.putExtra("singer", song.singer)
intent.putExtra("singer",song.singer)
intent.putExtra("second", song.second)
intent.putExtra("playTime", song.playTime)
intent.putExtra("isPlaying", song.isPlaying)
intent.putExtra("music", song.music)
activityResultLauncher.launch(intent)
}
}

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{
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)
}

private fun initBottomNavigation() {

supportFragmentManager.beginTransaction()
Expand Down
36 changes: 35 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.example.umc_6th

import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.os.PersistableBundle
import android.provider.MediaStore.Audio.Media
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 @@ -15,6 +18,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)
Expand Down Expand Up @@ -56,7 +62,8 @@ class SongActivity : AppCompatActivity() {
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 @@ -74,6 +81,8 @@ 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)
}

Expand All @@ -85,9 +94,14 @@ class SongActivity : AppCompatActivity() {
if(isPlaying){ // 재생중
binding.songMiniplayerIv.visibility = View.GONE
binding.songPauseIv.visibility = View.VISIBLE
mediaPlayer?.start()
} else { // 일시정지
binding.songMiniplayerIv.visibility = View.VISIBLE
binding.songPauseIv.visibility = View.GONE

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

Expand Down Expand Up @@ -131,4 +145,24 @@ 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
}
}
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 = ""
)
Binary file added UMC_6th/app/src/main/res/drawable/memo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions UMC_6th/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
app:layout_constraintBottom_toTopOf="@id/main_player_cl"
app:layout_constraintTop_toTopOf="parent" />

<SeekBar
android:id="@+id/main_miniplayer_progress_sb"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@null"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:layout_marginBottom="-4dp"
android:progressBackgroundTint="@color/song_player_bg"
android:progressTint="@color/song_player"
android:progress="0"
android:max="100000"
android:thumb="@color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/main_player_cl" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_player_cl"
Expand Down Expand Up @@ -111,3 +127,5 @@
app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>


Loading

0 comments on commit 82f6178

Please sign in to comment.