Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix/audio] 오디오 중단 & 재시작 할때의 음성 표시 바 수정 #148

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -28 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 AVNumberOfChannelsKey랑 AVEncoderAudioQualityKey는 몬가요..?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AVNumberOfChannelsKey: 음성 채널 수(Int)
AVEncoderAudioQualityKey: 오디오 품질(Enum)
입니다. 아래의 링크 참고해서 AVSampleRateKey 값과 AVEncoderAudioQualityKey 값을 맞췄습니다.

https://hoseiocean.medium.com/an-ios-developer-dive-in-avfaudio-choosing-the-sampling-rate-ea72217f4b45

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에 음성 채널 수가 2였는데 지금 1로 바뀐 이유는 뭔가요 ?!
그리고 품질이라면 high에서 Medium으로 낮추신 이유도 궁금합니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래의 사진을 참조하면

Rate 별 타입을 확인할 수 있는데, 현재 Rate 값인 44100에 medium이 어울린다고 판단해 수정했습니다. 그리고, 음성 채널 수는 단순한 녹음 기능이기에 1개만 있으면 충분하다 생각했습니다. 기존의 2개는 world wide web의 떠도는 코드를 가져와서 그렇습니다 ㅎ ㅠ.

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 감사합니다 !!

]

// 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
Loading