Skip to content

Commit

Permalink
#142 feat: 화면을 벗어나면 재생이 멈추도록 조정
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantchoi committed Dec 1, 2022
1 parent f270b50 commit 6718f59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
30 changes: 19 additions & 11 deletions Segno/Segno/Data/Session/MusicSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,32 @@ final class MusicSession {
}

// 음악을 재생하는 함수
func playMusic() {
func togglePlayer() {
guard let song else { return }
if !isPlaying {
player.queue = [song]

Task {
do {
try await player.play()
} catch let error {
// TODO: 에러 처리
print(error.localizedDescription)
}
}
playMusic(song: song)
} else {
player.pause()
}

// 뷰 컨트롤러를 나갈 때 큐를 비워 준다.
// 뮤직세션, 샤잠세션은 싱글턴 인스턴스로 만들어 주는 것이 좋겠다.
}

private func playMusic(song: Song) {
player.queue = [song]

Task {
do {
try await player.play()
} catch let error {
// TODO: 에러 처리
print(error.localizedDescription)
}
}
}

func stopMusic() {
player.stop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ final class DiaryDetailViewController: UIViewController {
musicSession.fetchMusic(term: MusicInfo.yokohama)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
override func viewWillDisappear(_ animated: Bool) {
musicSession.stopMusic()
}

private func bindPlayButton() {
musicContentView
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

private func setupLayout() {
Expand Down Expand Up @@ -242,7 +242,7 @@ final class DiaryDetailViewController: UIViewController {

extension DiaryDetailViewController: MusicContentViewDelegate {
func playButtonTapped() {
musicSession.playMusic()
musicSession.togglePlayer()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ final class LoginViewController: UIViewController {
}

buttonStack.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.top.equalTo(subTitleLabel.snp.bottom)
}

buttonStack.arrangedSubviews.forEach {
Expand Down

0 comments on commit 6718f59

Please sign in to comment.