Skip to content

Commit

Permalink
Merge pull request #148 from boostcampwm-2024/hotfix/audio
Browse files Browse the repository at this point in the history
Hotfix/audio
  • Loading branch information
yuncheol-AHN authored Dec 5, 2024
2 parents 143de4b + f027ed6 commit bc05e2f
Showing 1 changed file with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final class CreateAudioViewController: UIViewController {
private let audioRecordersettings: [String: Any] = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.medium.rawValue
]

// MARK: - UI Component
Expand Down Expand Up @@ -161,6 +161,27 @@ final class CreateAudioViewController: UIViewController {
}
}

private func setupBarsDefault() {
let width = 300 / numberOfBars - 5
let barSpacing = 5

for index in 0..<numberOfBars {
upBarLayers[index].frame = CGRect(
x: index * (width + barSpacing),
y: Int(volumeHalfHeight),
width: width,
height: -2
)

downBarLayers[index].frame = CGRect(
x: index * (width + barSpacing),
y: 0,
width: width,
height: 2
)
}
}

// MARK: - bind
private func bind() {
let output = viewModel?.transform(input: input.eraseToAnyPublisher())
Expand Down Expand Up @@ -233,7 +254,6 @@ final class CreateAudioViewController: UIViewController {
audioButton.layer.cornerRadius = 24
audioButton.setWidthAndHeight(width: 48, height: 48)
audioButton.setCenter(view: audioButtonBackground)
NSLayoutConstraint.activate(audioButtonConstraints)

timeTextLabel.setAnchor(
top: meteringBackgroundView.bottomAnchor, constantTop: 10,
Expand Down Expand Up @@ -286,15 +306,20 @@ final class CreateAudioViewController: UIViewController {
}

private func startRecording() {
setupBarsDefault()
try? audioSession.setActive(true)

timeTextLabel.text = "00:00"

audioRecorder?.prepareToRecord()
audioRecorder?.record()
// timer about audio metering level
meteringLevelTimer?.invalidate()
meteringLevelTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in
Task {
await self?.updateAudioMetering()
if await (self?.audioRecorder?.isRecording ?? true) {
await self?.updateAudioMetering()
}
}
}
// timer about audio record time
Expand All @@ -307,6 +332,7 @@ final class CreateAudioViewController: UIViewController {
}

// audio button to start
audioButtonSetStartImage()
audioButton.layer.cornerRadius = 6
NSLayoutConstraint.deactivate(audioButton.constraints)
audioButton.setWidthAndHeight(width: 32, height: 32)
Expand All @@ -323,10 +349,9 @@ final class CreateAudioViewController: UIViewController {
recordTimer?.invalidate()

recordingSeconds = 0
timeTextLabel.text = "00:00"

// audio button to stop
audioButton.layer.cornerRadius = 24
audioButtonSetRotateImage()
NSLayoutConstraint.deactivate(audioButton.constraints)
audioButton.setWidthAndHeight(width: 48, height: 48)
audioButton.setCenter(view: audioButtonBackground)
Expand All @@ -335,6 +360,24 @@ final class CreateAudioViewController: UIViewController {
saveButton.isEnabled = true
}

private func audioButtonSetRotateImage() {
let image = UIImage(
systemName: "arrow.clockwise",
withConfiguration: UIImage.SymbolConfiguration(pointSize: 36)
)
audioButton.setImage(image, for: .normal)
audioButton.tintColor = .black

audioButton.backgroundColor = .white
audioButtonBackground.layer.borderColor = UIColor.white.cgColor
}

private func audioButtonSetStartImage() {
audioButton.setImage(nil, for: .normal)
audioButton.backgroundColor = .red
audioButtonBackground.layer.borderColor = UIColor.gray.cgColor
}

private func updateAudioMetering() {
guard let recorder = audioRecorder else { return }
recorder.updateMeters()
Expand Down

0 comments on commit bc05e2f

Please sign in to comment.