初めてiPhoneアプリを製作しています。
1秒間隔でメトロノーム音を鳴らしつつ、録音できるアプリを製作したいのですが、AVAudioRecorderで録音を開始するとメトロノーム音が再生できません。
シミュレーター上では録音中にもメトロノーム音が鳴るのですが、実機で動作させると音が再生されません。
メトロノーム音の再生はAudioServicesPlaySystemSoundを使用しています。
必ずしもAudioServicesPlaySystemSoundで再生する必要はないのですが、メトロノーム音の感覚をtimerで変更することを考えてこちらを使用しています。
色々調べてみたのですが、ズバリの解決方法が見つかりません。
解決法をご存知の方がいらっしゃればご教授お願いいたします。
Swift
1import UIKit 2import AudioToolbox 3import AVFoundation 4 5 6class ViewController: UIViewController, AVAudioRecorderDelegate { 7 8 let fileManager = FileManager() 9 var soundId:SystemSoundID = 0 10 var audioRecorder: AVAudioRecorder? 11 var timer: Timer! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // AudioSession setup 16 let session = AVAudioSession.sharedInstance() 17 try! session.setCategory(AVAudioSessionCategoryPlayAndRecord) 18 try! session.setActive(true) 19 // SoundURL setup 20 let soundUrl = Bundle.main.url(forResource: "click", withExtension: "mp3")! 21 AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId) 22 //Recorder setup 23 let recordSetting : [String : Any] = [ 24 AVEncoderAudioQualityKey : AVAudioQuality.min.rawValue, 25 AVEncoderBitRateKey : 16, 26 AVNumberOfChannelsKey: 2, 27 AVSampleRateKey: 44100.0 28 ] 29 do { 30 try audioRecorder = AVAudioRecorder(url: fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("record.caf"), settings: recordSetting) 31 audioRecorder?.delegate = self 32 } catch { 33 print("error") 34 } 35 } 36 37 // 録音開始ボタン 38 @IBAction func pushRecordButton(sender: AnyObject) { 39 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.playClickSound), userInfo: nil, repeats: true) 40 audioRecorder?.record() 41 } 42 43 @objc func playClickSound(tm: Timer) { 44 AudioServicesPlaySystemSound(soundId) 45 } 46 47 // 録音停止ボタン 48 @IBAction func pushRecordStopButton(sender: AnyObject) { 49 audioRecorder?.stop() 50 timer.invalidate() 51 } 52} 53
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/09 22:36
2018/04/09 23:29
2018/04/11 08:21