Skip to content

Commit

Permalink
[kara/#18] feat : 4주차 구현완료
Browse files Browse the repository at this point in the history
  • Loading branch information
hyowon0204 committed May 7, 2024
1 parent d6cface commit b4935ec
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 20 deletions.
3 changes: 2 additions & 1 deletion UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.myfirstapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
Expand All @@ -19,13 +16,14 @@ class MainActivity : AppCompatActivity() {

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

setBottomNavigationView()

//MainActivity에서 SongActivity로 데이터 값 넘기기 위한 변수명 지정
val song = Song(binding.mainPlayTitleTv.text.toString(), binding.mainPlaySingerTv.text.toString())
val song = Song(binding.mainPlayTitleTv.text.toString(), binding.mainPlaySingerTv.text.toString(),0,60,false)

//SongActivity에서 Mainactivity로 데이터 넘긴거 받고 가수명과 노래 제목 toast로 출력
activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
Expand All @@ -43,6 +41,9 @@ class MainActivity : AppCompatActivity() {
val intent = Intent(this, SongActivity::class.java)
intent.putExtra("title", song.title)
intent.putExtra("singer",song.singer)
intent.putExtra("second",song.second)
intent.putExtra("playTime",song.playTime)
intent.putExtra("isPlaying",song.isPlaying)
activityResultLauncher.launch(intent)
}

Expand Down
7 changes: 5 additions & 2 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/Song.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.myfirstapp

data class Song(
val title : String = "",
val singer : String = ""
val title: String = "",
val singer: String = "",
var second: Int = 0,
var playTime: Int = 0,
var isPlaying: Boolean = false
)
86 changes: 75 additions & 11 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/SongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ package com.example.myfirstapp

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.myfirstapp.databinding.ActivityMainBinding
import com.example.myfirstapp.databinding.ActivitySongBinding

class SongActivity : AppCompatActivity() {

lateinit var binding: ActivitySongBinding
private var isColorChanged = false
lateinit var song : Song
lateinit var timer: Timer

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySongBinding.inflate(layoutInflater)
setContentView(binding.root)

initSong()
setPlayer(song)

var title : String? = null
var singer : String? = null

//MainActivity에서 SongActivity로 넘긴 데이터 값 받는 곳
if(intent.hasExtra("title") && intent.hasExtra("singer")){
title = intent.getStringExtra("title")
singer = intent.getStringExtra("singer")
binding.songMusicTitleTv.text = title
binding.songMusicSingerTv.text = singer
if(intent.hasExtra("title")&&intent.hasExtra("singer")){
binding.songMusicTitleTv.text = intent.getStringExtra("title")
binding.songMusicSingerTv.text = intent.getStringExtra("singer")
}

//SongActivity에서 Mainactivity로 데이터 넘기는 곳
Expand All @@ -45,10 +47,6 @@ class SongActivity : AppCompatActivity() {
binding.songMiniplayerPauseIv.setOnClickListener{
setPlayerStatus(true)
}
if(intent.hasExtra("title")&&intent.hasExtra("singer")){
binding.songMusicTitleTv.text = intent.getStringExtra("title")
binding.songMusicSingerTv.text = intent.getStringExtra("singer")
}

binding.songRepeatIv.setOnClickListener {
if (isColorChanged) {
Expand All @@ -71,6 +69,32 @@ class SongActivity : AppCompatActivity() {
}
}

override fun onDestroy() {
super.onDestroy()
timer.interrupt()
}
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)
)
}
startTimer()
}
private fun setPlayer(song: Song){
binding.songMusicTitleTv.text = intent.getStringExtra("title")
binding.songMusicSingerTv.text = intent.getStringExtra("singer")
binding.songStartTimeTv.text = String.format("%02d:%02d",song.second / 60, song.second % 60)
binding.songEndTimeTv.text = String.format("%02d:%02d",song.playTime / 60, song.playTime % 60)
binding.songSeekbarSb.progress = (song.second *1000 /song.playTime)

setPlayerStatus(song.isPlaying)
}

// override fun onBackPressed() {
// super.onBackPressed()
// val intent = Intent(this, MainActivity::class.java)
Expand All @@ -80,6 +104,8 @@ class SongActivity : AppCompatActivity() {
// }

fun setPlayerStatus(isPlaying : Boolean){
song.isPlaying = isPlaying
timer.isPlaying = isPlaying
if(isPlaying){ //재생중
binding.songMiniplayerPlayIv.visibility = View.VISIBLE
binding.songMiniplayerPauseIv.visibility = View.GONE
Expand All @@ -89,4 +115,42 @@ class SongActivity : AppCompatActivity() {
binding.songMiniplayerPauseIv.visibility = View.VISIBLE
}
}

private fun startTimer() {
timer = Timer(song.playTime, song.isPlaying)
timer.start()
}
inner class Timer(private val playTime: Int, var isPlaying: Boolean = true):Thread(){
private var second : Int = 0
private var mills: Float = 0f

override fun run() {
super.run()
try{
while(true){
if(second >= playTime){
break
}
if(isPlaying){
sleep(50)
mills += 50

runOnUiThread {
binding.songSeekbarSb.progress = ((mills / playTime)*100).toInt()
}
if(mills % 1000 == 0f){
runOnUiThread {
binding.songStartTimeTv.text = String.format("%02d:%02d",second / 60, second % 60)
}
second++
}
}
}
}catch(e: InterruptedException){
Log.d("SongActivity","쓰레드가 죽었습니다")
}

}

}
}
7 changes: 5 additions & 2 deletions UMC_6th/app/src/main/res/layout/activity_song.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="10dp"
android:background="@null"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:progressTint="@color/song_player"
android:progressBackgroundTint="@color/song_player_bg"
android:progress="0"
Expand Down Expand Up @@ -198,13 +201,13 @@
android:src="@drawable/btn_miniplayer_previous"/>

<ImageView
android:id="@+id/song_miniplayer_play_iv"
android:id="@+id/song_miniplayer_pause_iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/btn_miniplayer_play"/>

<ImageView
android:id="@+id/song_miniplayer_pause_iv"
android:id="@+id/song_miniplayer_play_iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/btn_miniplay_mvpause"
Expand Down
4 changes: 4 additions & 0 deletions UMC_6th/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
</style>

<style name="Theme.MyApplication" parent="Base.Theme.MyApplication" />
<style name="SplashTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowBackground"> @drawable/splash</item>
<item name="android:statusBarColor">@color/transparent </item>
</style>
</resources>

0 comments on commit b4935ec

Please sign in to comment.