Audio is playing in simulator but not on device(音频在模拟器中播放,但不在设备上播放)
本文介绍了音频在模拟器中播放,但不在设备上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个应用程序,它可以播放音频文件,并可以在Xcode模拟器(v10.2)中使用AVPlayer的一个实例来播放这些文件,但当试图在我的设备上播放这个应用程序时,音频文件无法播放。
我已经阅读了对同一问题的回复,并检查了:
- 设备上的静音振铃器开关未打开
- 我尚未在视图DidLoad中实例化AVPlayer
- 音频文件的名称在代码中的大小写与文件中的大小写相同
以下是我设置录音机的代码:
func setUpRecorder(storyItem : StoryItem) {
// Generate unique ID for recording for the StoryItem
let uuid = UUID().uuidString + ".m4a"
let audioFileName = getDocumentDirectory().appendingPathComponent(uuid)
do {
try self.realm.write {
// storyItem.recording = audioFileName.absoluteString
storyItem.recording = uuid
}
} catch {
print("Error saving recording (error)")
}
self.createStoryTableView.reloadData()
let recordSettings = [AVFormatIDKey : kAudioFormatAppleLossless,
AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey : 2,
AVSampleRateKey : 44100.0 ] as [String : Any]
do {
audioRecorder = try AVAudioRecorder(url: audioFileName, settings: recordSettings)
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
} catch {
print(error)
}
}
播放音频文件的方法如下:
func setUpPlayer(storyItem:StoryItem){
let audioFileName = getDocumentDirectory().appendingPathComponent(storyItem.recording)
getAudioDuration(audioFileName: audioFileName)
if storyItem.recording == "" {
let alert = UIAlertController(title: "There's no recording", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Cancel", style: .default) { (action) in
}
// This is what happens when cancel is pressed
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
} else {
player = AVPlayer(url: audioFileName)
player.volume = 1.0
}
}
如有任何建议,我将不胜感激。会不会是使用唯一标识符(UUID)作为音频文件的名称?
推荐答案
尝试在录制之前配置AVAudioSession共享实例。
这样:
// Configure AVAudioSession
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch {
assertionFailure("Failed to configure `AVAAudioSession`: (error.localizedDescription)")
}
这篇关于音频在模拟器中播放,但不在设备上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:音频在模拟器中播放,但不在设备上播放
基础教程推荐
猜你喜欢
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
