Skip to content

Commit

Permalink
[Simba/#18] feat :: 4주차 구현 (#20)
Browse files Browse the repository at this point in the history
* [simba/#18] feat 4주차 구현

* [simba/#18] fix 에러 수정
  • Loading branch information
BAEK0111 authored May 7, 2024
1 parent a4a13da commit 092cf05
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 47 deletions.

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

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

2 changes: 1 addition & 1 deletion UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ android {
namespace = "com.example.umc_6th"
compileSdk = 34


buildFeatures{
viewBinding = true
dataBinding = true
}
defaultConfig {
applicationId = "com.example.umc_6th"
Expand Down
6 changes: 4 additions & 2 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
android:supportsRtl="true"
android:theme="@style/Theme.UMC_6th"
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" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".SongActivity"
android:exported="true"/>
Expand Down
58 changes: 30 additions & 28 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 @@ -13,42 +13,44 @@ class MainActivity : AppCompatActivity() {

lateinit var binding: ActivityMainBinding
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

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

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

binding.mainPlayerCl.setOnClickListener{
//startActivity(Intent(this,SongActivity::class.java))
val intent = Intent(this,SongActivity::class.java)
intent.putExtra("title",song.title)
intent.putExtra("singer",song.singer)
startActivity(intent)
}

initBottomNavigation()

Log.d("Song",song.title+song.singer)

// 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()
// }
// }
// }
val song = Song(
binding.mainMiniplayerTitleTv.text.toString(),
binding.mainMiniplayerSingerTv.text.toString(),
0,
60,
false
)

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)
startActivity(intent)
binding.mainPlayerCl.setOnClickListener {
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)
// startActivity(intent)
activityResultLauncher.launch(intent)
}
}

Expand Down
5 changes: 4 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 @@ -2,5 +2,8 @@ package com.example.umc_6th

data class Song(
val title : String = "",
val singer : String = ""
val singer : String = "",
var second : Int = 0,
var playTime : Int = 60,
var isPlaying : Boolean = false
)
99 changes: 87 additions & 12 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivitySongBinding
Expand All @@ -10,12 +11,17 @@ import java.util.Timer
class SongActivity : AppCompatActivity() {

lateinit var binding: ActivitySongBinding
lateinit var song: Song
lateinit var timer : Timer

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

initSong()
setPlayer(song)

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

Expand All @@ -26,28 +32,97 @@ class SongActivity : AppCompatActivity() {
binding.songSingerNameTv.text = singer
}

binding.songDownIb.setOnClickListener{
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("message", title + " _ " + singer)
setResult(RESULT_OK, intent)
finish()
}
// binding.songDownIb.setOnClickListener{
// val intent = Intent(this, MainActivity::class.java)
// intent.putExtra("message", title + " _ " + singer)
// setResult(RESULT_OK, intent)
// finish()
// }

binding.songMiniplayerIv.setOnClickListener{
setPlayerStatus(false)
setPlayerStatus(true)
}
binding.songPauseIv.setOnClickListener{
setPlayerStatus(true)
setPlayerStatus(false)
}
}

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.songSingerNameTv.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.songProgressSb.progress = (song.second * 1000 / song.playTime)

setPlayerStatus(song.isPlaying)
}

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

if(isPlaying){
binding.songMiniplayerIv.visibility= View.VISIBLE
binding.songMiniplayerIv.visibility= View.GONE
binding.songPauseIv.visibility = View.VISIBLE
} else{
binding.songMiniplayerIv.visibility=View.VISIBLE
binding.songPauseIv.visibility = View.GONE
}
else{
binding.songMiniplayerIv.visibility=View.GONE
binding.songPauseIv.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
}

while (!isPlaying) {
sleep(200)
}

if (isPlaying) {
sleep(50)
mills += 50

runOnUiThread {
binding.songProgressSb.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("Song","쓰레드가 죽었습니다. ${e.message}")
}
}
}
}
4 changes: 3 additions & 1 deletion UMC_6th/app/src/main/res/drawable/splash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white"/>
<item>
<bitmap android:src="@drawable/ic_flo_logo" android:gravity="center"/>
<bitmap
android:src="@drawable/ic_flo_logo"
android:gravity="center"/>
</item>

</layer-list>
3 changes: 2 additions & 1 deletion UMC_6th/app/src/main/res/layout/activity_song.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<SeekBar
android:id="@+id/song_progress_sb"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="10dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
Expand All @@ -160,6 +160,7 @@
android:progressBackgroundTint="@color/song_player_bg"
android:progressTint="@color/song_player"
android:thumb="@color/transparent"
android:max="100000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/song_like_iv_layout"/>
Expand Down
5 changes: 4 additions & 1 deletion UMC_6th/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>

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

0 comments on commit 092cf05

Please sign in to comment.