커뮤니티 오디오 녹음

- audioSession.setCategory mode videoRecording으로 수정
This commit is contained in:
Yu Sung 2024-08-08 01:34:14 +09:00
parent 0be8d0d98a
commit 85a871693c
3 changed files with 18 additions and 11 deletions

View File

@ -23,6 +23,8 @@ struct CreatorCommunityRecordingVoiceView: View {
@Binding var fileName: String
@Binding var soundData: Data?
@State private var tempFileName = ""
var body: some View {
ZStack {
Color.black.opacity(0.7)
@ -65,9 +67,8 @@ struct CreatorCommunityRecordingVoiceView: View {
.onTapGesture {
if !soundManager.isLoading {
if !soundManager.isRecording {
let fileName = "\(Date().timeIntervalSince1970 * 1000).m4a"
self.fileName = fileName
soundManager.startRecording(fileName)
tempFileName = "now_voice_\(Int(Date().timeIntervalSince1970 * 1000)).m4a"
soundManager.startRecording(tempFileName)
} else {
soundManager.stopRecording()
recordMode = .PLAY
@ -84,7 +85,7 @@ struct CreatorCommunityRecordingVoiceView: View {
Text("삭제")
.font(.custom(Font.medium.rawValue, size: 15.3))
.foregroundColor(Color(hex: "bbbbbb").opacity(0))
.foregroundColor(Color.graybb.opacity(0))
Spacer()
@ -112,7 +113,6 @@ struct CreatorCommunityRecordingVoiceView: View {
soundManager.stopAudio()
soundManager.deleteAudioFile()
recordMode = .RECORD
fileName = "녹음"
}
Spacer()
@ -134,7 +134,6 @@ struct CreatorCommunityRecordingVoiceView: View {
soundManager.stopAudio()
soundManager.deleteAudioFile()
recordMode = .RECORD
fileName = "녹음"
}
Text("녹음완료")
@ -147,6 +146,7 @@ struct CreatorCommunityRecordingVoiceView: View {
do {
let soundData = try Data(contentsOf: soundManager.getAudioFileURL())
self.soundData = soundData
self.fileName = tempFileName
self.isShowing = false
} catch {
errorMessage = "녹음파일을 생성하지 못했습니다.\n다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."

View File

@ -26,14 +26,14 @@ class CreatorCommunitySoundManager: NSObject, ObservableObject {
var player: AVAudioPlayer!
var audioRecorder: AVAudioRecorder!
var fileName = "record.m4a"
var fileName = "now_voice.m4a"
let audioSession = AVAudioSession.sharedInstance()
func prepareRecording() {
isLoading = true
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord, mode: .default)
try audioSession.setCategory(.playAndRecord, mode: .videoRecording)
try audioSession.setActive(true)
audioSession.requestRecordPermission() { [weak self] allowed in
DispatchQueue.main.async {
@ -56,6 +56,9 @@ class CreatorCommunitySoundManager: NSObject, ObservableObject {
func startRecording(_ fileName: String) {
self.fileName = fileName
player?.stop()
player = nil
isLoading = true
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
@ -66,6 +69,9 @@ class CreatorCommunitySoundManager: NSObject, ObservableObject {
]
do {
try audioSession.setCategory(.playAndRecord, mode: .videoRecording)
try audioSession.setActive(true)
audioRecorder = try AVAudioRecorder(url: getAudioFileURL(), settings: settings)
audioRecorder.record()
isRecording = true
@ -91,9 +97,9 @@ class CreatorCommunitySoundManager: NSObject, ObservableObject {
isLoading = true
DispatchQueue.main.async {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playback, mode: .default)
try self.audioSession.setCategory(.playback, mode: .default)
try self.audioSession.setActive(true)
if let url = url {
self.player = try AVAudioPlayer(data: Data(contentsOf: url))

View File

@ -91,6 +91,7 @@ struct CreatorCommunityWriteView: View {
.background(Color.bg)
.cornerRadius(5.3)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.overlay(
RoundedRectangle(cornerRadius: 5.3)
.stroke(Color.button, lineWidth: 1)