###前提・実現したいこと
swift3.0でやっています。音声の録音、再生機能を含むアプリを開発しており、端末内に音声ファイル保存用のフォルダーを作り、そこに録音したファイルを保存する。
###発生している問題・エラーメッセージ
下記のdocumentFilePathという関数で設定しているファイルの保存先を変更すると、audiorecorderで録音することができなくなります。
コメントにしてある
return (dirURL?.appendingPathComponent(newFilename)! as NSURL?)!
の方に返り値を変更すると、録音できるのですが、
return newDir!.appendingPathComponent(newFilename) as NSURL
の方にすると録音ができなくなります。
###該当のソースコード
func setupAudioRecorder(audioRecorder: AVAudioRecorder?) { // 再生と録音機能をアクティブにする let session = AVAudioSession.sharedInstance() try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.mixWithOthers) try! session.setActive(true) let recordSetting : [String : AnyObject] = [ AVEncoderAudioQualityKey : AVAudioQuality.min.rawValue as AnyObject, AVEncoderBitRateKey : 16 as AnyObject, AVNumberOfChannelsKey: 2 as AnyObject, AVSampleRateKey: 44100.0 as AnyObject ] do { //保存先とレコーディングのパラメータを設定 try audioRecorder = AVAudioRecorder(url: self.documentFilePath(fileName) as URL, settings: recordSetting) } catch { print("初期設定でerror出たよ(-_-;)") } } func documentFilePath(_ fileName:String)-> NSURL { let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask) as [NSURL] let dirURL = urls[0] let newDir = dirURL?.appendingPathComponent("newDir") let newFilename = fileName + fileExtension //return (dirURL?.appendingPathComponent(newFilename)! as NSURL?)! return newDir!.appendingPathComponent(newFilename) as NSURL }
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
ご回答に対して返信遅れてしまい、申し訳ありませんでした。
print(newDir)
と、print(newDir!.appendingPathComponent(newFilename) as NSURL)
の中身についてですが、
それぞれ
print(newDir)
Optional(file:///var/mobile/Containers/Data/Application/4754CB46-2E89-4A27-8600-A59BE5CCBACA/Documents/newDir)
print(newDir!.appendingPathComponent(newFilename) as NSURL)
file:///var/mobile/Containers/Data/Application/4754CB46-2E89-4A27-8600-A59BE5CCBACA/Documents/newDir/20_sample0.caf
という結果でした。
なぜかディレクトリを作ってからそのフォルダーに録音ファイルを入れようとすると、レコーダーが走らず、
audioRecorder1?.isRecording)!
がfalseとなってしまいます。
あなたの回答
tips
プレビュー