2017-03-30 33 views
1

我的莫爾斯電碼轉換器不會輸出聲音,因爲它應該。我已經測試了沒有這個功能的揚聲器和我的方法,它的工作完美無瑕,但與其他程序無關。編譯器不給我任何錯誤,操場不會崩潰,它只是不會播放聲音。音量和鈴聲已滿。Swift AVFoundation在遊樂場不輸出聲音

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() 
    } 
} 

我一直在亂搞代碼幾個小時,沒有任何工作。我做錯什麼(如果有的話)?

回答

0

播放音頻似乎有些操場問題。看到這個線程:

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 

我必須啓用無限期執行才能讓代碼執行。另外,我在加載第二個音頻文件時遇到了一些問題,但是我沒有進一步調查,看看它是否是我的測試文件或其他問題,因爲它大部分工作。

-1

@Fahim還是它顯示錯誤

類莫爾斯:NSObject的,AVAudioPlayerDelegate { 私人VAR消息= 「」 私人VAR dotSound:AVAudioPlayer! private var dashSound:AVAudioPlayer! private let dash =字符(「 - 」) private let dot =字符(「。」) 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() 
    } 
} 

}

令M =莫爾斯(消息: 「...---」) m.playCharacter()

PlaygroundPage.current.needsIndefiniteExecution =真

+0

這是令人困惑當您發表評論作爲正式答案時。你的這個「答案」應該是對Fahim答案的評論,你應該編輯你的問題來顯示這個更新後的代碼。 –