-
Notifications
You must be signed in to change notification settings - Fork 1
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
[Audio] 페이지에 오디오 플레이어 연결 #122
Changes from 31 commits
0d5bbd1
59f72eb
32890b8
b87ac6c
ba4e4dc
52ab72f
f9a7975
7be6699
25ee31b
df027f8
1620cbe
19393ba
dda2954
dbc0862
77e3b9a
9b0d6a9
e4bb229
e45f320
8e4ca0a
a532d6f
2972f41
f3fc468
67670f0
7d8105d
004f6e3
3200219
ebba5de
2f5c8cc
9b334e1
844e0f8
047311f
a01cc47
3fa7a1a
ee798d1
aec03b9
3ee5c61
74d5165
9239faf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ final class CreateAudioViewController: UIViewController { | |
private var cancellables = Set<AnyCancellable>() | ||
// auido | ||
private var audioRecorder: AVAudioRecorder? | ||
private var isRecording = false | ||
// auido metering | ||
private var upBarLayers: [CALayer] = [] | ||
private var downBarLayers: [CALayer] = [] | ||
|
@@ -29,8 +28,6 @@ final class CreateAudioViewController: UIViewController { | |
AVNumberOfChannelsKey: 2, | ||
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue | ||
] | ||
// UUID | ||
private let identifier: UUID = UUID() | ||
|
||
// MARK: - UI Component | ||
// title and buttons | ||
|
@@ -101,7 +98,8 @@ final class CreateAudioViewController: UIViewController { | |
} | ||
|
||
required init?(coder: NSCoder) { | ||
self.viewModel = CreateAudioViewModel() | ||
guard let viewModelFactory = try? DIContainer.shared.resolve(CreateAudioViewModelFactory.self) else { return nil } | ||
self.viewModel = viewModelFactory.make { _ in } | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
|
@@ -111,17 +109,13 @@ final class CreateAudioViewController: UIViewController { | |
|
||
setup() | ||
bind() | ||
configureAudioSession() | ||
configureAddSubviews() | ||
configureConstraints() | ||
configureAddActions() | ||
input.send(.viewDidLoad) | ||
} | ||
|
||
override func viewDidDisappear(_ animated: Bool) { | ||
self.input.send(.viewDidDisappear) | ||
} | ||
|
||
// MARK: - setup | ||
// MARK: - Setup | ||
private func setup() { | ||
view.backgroundColor = .white | ||
setupBars() | ||
|
@@ -157,55 +151,31 @@ final class CreateAudioViewController: UIViewController { | |
} | ||
} | ||
|
||
private func requestMicrophonePermission() { | ||
AVAudioSession.sharedInstance().requestRecordPermission { @Sendable granted in | ||
if !granted { | ||
Task { @MainActor in | ||
let alert = UIAlertController( | ||
title: "마이크 권한 필요", | ||
message: "설정에서 마이크 권한을 허용해주세요.", | ||
preferredStyle: .alert | ||
) | ||
alert.addAction(UIAlertAction(title: "OK", style: .default)) | ||
self.present(alert, animated: true, completion: nil) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - bind | ||
private func bind() { | ||
let output = viewModel?.transform(input: input.eraseToAnyPublisher()) | ||
output?.sink(receiveValue: { [weak self] event in | ||
switch event { | ||
case .updatedAudioFileURL: | ||
// TODO: - update audio file url | ||
MHLogger.debug("updated audio file URL") | ||
case .savedAudioFile: | ||
// TODO: - show audio player | ||
MHLogger.debug("saved audio file") | ||
case .deleteTemporaryAudioFile: | ||
// TODO: - delete temporary audio file | ||
MHLogger.debug("delete temporary audio file") | ||
case .audioStart: | ||
self?.startRecording() | ||
case .audioStop: | ||
self?.stopRecording() | ||
} | ||
}).store(in: &cancellables) | ||
output?.receive(on: DispatchQueue.main) | ||
.sink(receiveValue: { [weak self] event in | ||
switch event { | ||
case let .audioFileURL(url): | ||
self?.configureAudioSession(for: url) | ||
case .audioStart: | ||
self?.startRecording() | ||
case .audioStop: | ||
self?.stopRecording() | ||
case .recordCompleted: | ||
self?.dismiss(animated: true) | ||
} | ||
}).store(in: &cancellables) | ||
} | ||
|
||
// MARK: - configure | ||
|
||
private func configureAudioSession() { | ||
let fileName = "\(identifier).m4a" | ||
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] | ||
let audioFileURL = documentDirectory.appendingPathComponent(fileName) | ||
|
||
// MARK: - Configuration | ||
private func configureAudioSession(for url: URL) { | ||
try? audioSession.setCategory(.record, mode: .default) | ||
|
||
audioRecorder = try? AVAudioRecorder(url: audioFileURL, settings: audioRecordersettings) | ||
audioRecorder = try? AVAudioRecorder(url: url, settings: audioRecordersettings) | ||
audioRecorder?.isMeteringEnabled = true | ||
print("URL: \(url)") | ||
} | ||
|
||
private func configureAddSubviews() { | ||
|
@@ -263,6 +233,56 @@ final class CreateAudioViewController: UIViewController { | |
timeTextLabel.setWidthAndHeight(width: 60, height: 16) | ||
} | ||
|
||
private func configureAddActions() { | ||
addTappedEventToAudioButton() | ||
addTappedEventToCancelButton() | ||
addTappedEventToSaveButton() | ||
} | ||
|
||
private func addTappedEventToAudioButton() { | ||
audioButton.addAction(UIAction { [weak self] _ in | ||
self?.input.send(.audioButtonTapped) | ||
}, for: .touchUpInside) | ||
} | ||
private func addTappedEventToCancelButton() { | ||
cancelButton.addAction( | ||
UIAction { [weak self] _ in | ||
self?.input.send(.recordCancelled) | ||
}, | ||
for: .touchUpInside) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 개행 컨벤션이 일정하지 않은데 통일시켜주면 좋을 것 같습니다 ! |
||
private func addTappedEventToSaveButton() { | ||
saveButton.addAction( | ||
UIAction { [weak self] _ in | ||
self?.input.send(.saveButtonTapped) | ||
}, for: .touchUpInside) | ||
} | ||
|
||
// MARK: - Helper | ||
private func requestMicrophonePermission() { | ||
let alert = UIAlertController( | ||
title: "마이크 권한 필요", | ||
message: "설정에서 마이크 권한을 허용해주세요.", | ||
preferredStyle: .alert | ||
) | ||
alert.addAction(UIAlertAction(title: "OK", style: .default)) | ||
Task { | ||
if #available(iOS 17, *) { | ||
if await !AVAudioApplication.requestRecordPermission() { | ||
self.present(alert, animated: true, completion: nil) | ||
} | ||
} else { | ||
AVAudioSession.sharedInstance().requestRecordPermission { granted in | ||
Task { @MainActor in | ||
if !granted { | ||
self.present(alert, animated: true, completion: nil) | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: 이쪽 혹시 Test 해보셨을까요?? 아마 requestRecordPermission 후행 클로저에 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이게 맞을까요? 테스트는 아직... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 16.4시뮬로 테스트해봤었는데 잘 됐었습니다! |
||
} | ||
} | ||
} | ||
|
||
private func startRecording() { | ||
try? audioSession.setActive(true) | ||
|
||
|
@@ -356,29 +376,4 @@ final class CreateAudioViewController: UIViewController { | |
let seconds = recordingSeconds % 60 | ||
timeTextLabel.text = String(format: "%02d:%02d", minutes, seconds) | ||
} | ||
|
||
private func configureAddActions() { | ||
addTappedEventToAudioButton() | ||
addTappedEventToCancelButton() | ||
addTappedEventToSaveButton() | ||
} | ||
|
||
private func addTappedEventToAudioButton() { | ||
audioButton.addAction(UIAction { [weak self] _ in | ||
self?.input.send(.audioButtonTapped) | ||
}, for: .touchUpInside) | ||
} | ||
private func addTappedEventToCancelButton() { | ||
cancelButton.addAction( | ||
UIAction { [weak self]_ in | ||
self?.dismiss(animated: true) | ||
}, | ||
for: .touchUpInside) | ||
} | ||
private func addTappedEventToSaveButton() { | ||
saveButton.addAction(UIAction { _ in | ||
self.input.send(.saveButtonTapped) | ||
self.dismiss(animated: true) | ||
}, for: .touchUpInside) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: print문은 지워주면 좋을 것 같습니당 !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GOOD😊