操场上的 Swift AVFoundation 不输出声音
Posted
技术标签:
【中文标题】操场上的 Swift AVFoundation 不输出声音【英文标题】:Swift AVFoundation in playground not outputting sound 【发布时间】:2017-03-30 01:36:40 【问题描述】:我的摩尔斯电码翻译器不会输出应有的声音。我已经在没有此功能的情况下测试了扬声器和我的方法,它可以完美运行,但它与程序的其余部分无关。编译器没有给我任何错误,操场也没有崩溃,它只是不播放声音。音量和铃声已满。
func speakTheCode(message: String)
var speaker = AVAudioPlayer()
let longBeep = #fileLiteral(resourceName: "beep_long.mp3")
let shortBeep = #fileLiteral(resourceName: "beep_short.mp3")
let dash = "-"
let dot = "."
for character in message.characters
if character == dash[dash.startIndex]
speaker = try! AVAudioPlayer(contentsOf: longBeep)
speaker.prepareToPlay()
print("-")
else if character == dot[dot.startIndex]
speaker = try! AVAudioPlayer(contentsOf: shortBeep)
speaker.prepareToPlay()
print(".")
speaker.play()
我已经搞砸了好几个小时的代码,但没有任何效果。我做错了什么(如果有的话)?
【问题讨论】:
【参考方案1】:在一些游乐场播放音频时似乎存在问题。看到这个帖子:
Playing a sound in a Swift Playground
但是,我能够对您的代码进行一些更改并使其正常工作。这是我的代码:
class Morse:NSObject, AVAudioPlayerDelegate
private var message = ""
private var dotSound:AVAudioPlayer!
private var dashSound:AVAudioPlayer!
private let dash = Character("-")
private let dot = Character(".")
private var index:String.Index!
init(message:String)
super.init()
do
if let url = Bundle.main.url(forResource:"beep_short", withExtension:"mp3")
self.dotSound = try AVAudioPlayer(contentsOf:url)
self.dotSound.delegate = self
self.dotSound.prepareToPlay()
catch
NSLog("Error loading dot audio!")
do
if let url = Bundle.main.url(forResource:"beep_long", withExtension:"mp3")
self.dashSound = try AVAudioPlayer(contentsOf:url)
self.dashSound.delegate = self
self.dashSound.prepareToPlay()
catch
NSLog("Error loading dash audio!")
self.message = message
self.index = message.startIndex
func playCharacter()
let character = message.characters[index]
NSLog("Character: \(character)")
if character == dash
dashSound.play()
else if character == dot
dotSound.play()
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
NSLog("Finished playing")
if index != message.endIndex
self.index = message.index(after:index)
playCharacter()
let m = Morse(message:"...---")
m.playCharacter()
PlaygroundPage.current.needsIndefiniteExecution = true
我必须启用无限期执行才能让代码完全执行。此外,我在加载第二个音频文件时遇到了一些问题,但我没有进一步调查是否是我的测试文件的问题或其他问题,因为它大部分都有效。
【讨论】:
【参考方案2】:@Fahim 仍然显示错误
类莫尔斯:NSObject,AVAudioPlayerDelegate 私人 var 消息 = "" 私人 var dotSound:AVAudioPlayer! 私人 var dashSound:AVAudioPlayer! 私人让破折号=字符(“-”) private let dot = Character(".") 私有变量索引:String.Index!
init(message:String)
super.init()
do
if let url = Bundle.main.url(forResource:"beep_short", withExtension:"mp3")
self.dotSound = try AVAudioPlayer(contentsOf:url)
self.dotSound.delegate = self
self.dotSound.prepareToPlay()
catch
NSLog("Error loading dot audio!")
do
if let url = Bundle.main.url(forResource:"beep_long", withExtension:"mp3")
self.dashSound = try AVAudioPlayer(contentsOf:url)
self.dashSound.delegate = self
self.dashSound.prepareToPlay()
catch
NSLog("Error loading dash audio!")
self.message = message
self.index = message.startIndex
func playCharacter()
let character = message.characters[index]
NSLog("Character: \(character)")
if character == dash
dashSound.play()
else if character == dot
dotSound.play()
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
NSLog("Finished playing")
if index != message.endIndex
self.index = message.index(after:index)
playCharacter()
让 m = Morse(message:"...---") m.playCharacter()
PlaygroundPage.current.needsIndefiniteExecution = true
【讨论】:
当您将 cmets 作为官方答案发布时会令人困惑。你的这个“答案”应该是对 Fahim 答案的评论,你应该编辑你的问题以显示这个更新的代码。以上是关于操场上的 Swift AVFoundation 不输出声音的主要内容,如果未能解决你的问题,请参考以下文章
tableview 是空的,但我看到操场上的数据来自 urlsession
在 Swift 的 AVFoundation 中使用夜间模式
Swift 4 - 在 mac os 上使用 AVAssetWriter 进行 avfoundation 屏幕和音频录制 - 视频冻结