実現したいこと
下記のソースコードで録音時間を15秒に制限したい。15秒経つと自動で録音が停止する。
試したこと
録音時間を制限するには、record(forDuration:)を使うとできる事を知りました。そこで、
Appleデベロッパーのサイトを参考にしました。英語なのと知識不足でわからずでした。
次に、他にrecord(forDuration:)の使い方について調べてみましたが、該当のサイトが見当たらずわかりませんでした。
ソースコード
import UIKit import AVFoundation class ViewController: UIViewController { var audioRecorder: AVAudioRecorder! var audioEngine: AVAudioEngine! var audioFile: AVAudioFile! var audioPlayerNode: AVAudioPlayerNode! override func viewDidLoad() { super.viewDidLoad() let session = AVAudioSession.sharedInstance() do { try session.setCategory(.playAndRecord, mode: .default) try session.setActive(true) let settings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100, AVNumberOfChannelsKey: 2, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] audioRecorder = try AVAudioRecorder(url: getAudioFileUrl(), settings: settings) audioRecorder.delegate = self as? AVAudioRecorderDelegate } catch let error { print(error) } } @IBAction func record(_ sender: Any) { if !audioRecorder.isRecording { audioRecorder.record() } else { audioRecorder.stop() } } @IBAction func play(_ sender: Any) { audioEngine = AVAudioEngine() do { audioFile = try AVAudioFile(forReading: getAudioFileUrl()) audioPlayerNode = AVAudioPlayerNode() audioEngine.attach(audioPlayerNode) audioEngine.connect(audioPlayerNode, to: audioEngine.outputNode, format: audioFile.processingFormat) audioPlayerNode.stop() audioPlayerNode.scheduleFile(audioFile, at: nil) try audioEngine.start() audioPlayerNode.play() } catch let error { print(error) } } func getAudioFileUrl() -> URL { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let docsDirect = paths[0] let audioUrl = docsDirect.appendingPathComponent("recording.m4a") return audioUrl } }
よろしくお願い致します。
追記
import UIKit import AVFoundation class ViewController: UIViewController { @IBOutlet weak var recordLebal: UILabel! var audioRecorder: AVAudioRecorder! var audioEngine: AVAudioEngine! var audioFile: AVAudioFile! var audioPlayerNode: AVAudioPlayerNode! override func viewDidLoad() { super.viewDidLoad() let session = AVAudioSession.sharedInstance() do { try session.setCategory(.playAndRecord, mode: .default) try session.setActive(true) let settings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100, AVNumberOfChannelsKey: 2, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] audioRecorder = try AVAudioRecorder(url: getAudioFileUrl(), settings: settings) audioRecorder.delegate = self as? AVAudioRecorderDelegate } catch let error { print(error) } } @IBAction func record(_ sender: Any) { if !audioRecorder.isRecording { audioRecorder.record(forDuration: 15) recordLebal.text = "録音中" } else { audioRecorder.stop() recordLebal.text = "録音終了" } } @IBAction func play(_ sender: Any) { audioEngine = AVAudioEngine() do { audioFile = try AVAudioFile(forReading: getAudioFileUrl()) audioPlayerNode = AVAudioPlayerNode() audioEngine.attach(audioPlayerNode) audioEngine.connect(audioPlayerNode, to: audioEngine.outputNode, format: audioFile.processingFormat) audioPlayerNode.stop() audioPlayerNode.scheduleFile(audioFile, at: nil) try audioEngine.start() audioPlayerNode.play() } catch let error { print(error) } recordLebal.text = "再生中" } func getAudioFileUrl() -> URL { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let docsDirect = paths[0] let audioUrl = docsDirect.appendingPathComponent("recording.m4a") return audioUrl } }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/23 11:45
2020/03/23 15:35
2020/03/24 06:58