【问题标题】:Playing Random Audio File using Swift使用 Swift 播放随机音频文件
【发布时间】:2014-11-22 05:22:03
【问题描述】:

我在 iPhone 模拟器中使用 “摇动手势” 时不断收到以下错误:

致命错误:在展开可选值时意外发现 nil

这是我的相关代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var soundFiles = ["kidLaughing", "pewpew", "pingas", "runningfeet"]
    var player: AVAudioPlayer = AVAudioPlayer()

    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
        if event.subtype == .MotionShake {
            var randomSoundFile = Int(arc4random_uniform(UInt32(soundFiles.count)))
            var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("sounds/" + soundFiles[randomSoundFile], ofType: "mp3")!)

            var error: NSError? = nil

            player = AVAudioPlayer(contentsOfURL: NSURL(string: fileLocation), error: &error)

            player.play()
        }
    }
}

我有一个名为 sounds 的文件夹,其中包含 4 个 mp3 文件。错误发生在这行代码上:

  var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("sounds/" + soundFiles[randomSoundFile], ofType: "mp3")!)

我已经尝试了所有我能想到的方法来让它发挥作用,但我所尝试的一切都没有奏效。任何帮助表示赞赏!

【问题讨论】:

    标签: ios objective-c swift avaudioplayer nsbundle


    【解决方案1】:

    这意味着当您断言它不是时,存在一个值为 nil 的值。将崩溃的行分成其组件并找出究竟是什么:

    var soundFile = soundFiles[randomSoundFile]
    var path = "sounds/" + soundFile
    var fullPath = NSBundle.mainBundle().pathForResource(path, ofType: "mp3")!
    var fileLocation = NSString(string:fullPath) // fyi, this line is pointless, fullPath is already a string
    

    我猜它在 pathForResource 行上崩溃了,因为实际上无法找到该文件。确保您实际上将“声音”目录链接到您的包中。

    【讨论】:

      【解决方案2】:

      //你也可以试试这个随机播放声音。

      import UIKit
      import AVFoundation
      class GameScene: SKScene {
      var pinballVoiceArray = ["BS_PinballBounce1.mp3", "BS_PinballBounce2.mp3", "BS_PinballBounce3.mp3"]
      var randomIndex = 0
      override func didMoveToView(view: SKView) {
          //use the random sound function
          playPinballSound()
         }
      func playPinballSound () {
          randomIndex = Int(arc4random_uniform(UInt32(pinballVoiceArray.count)))
          var selectedFileName = pinballVoiceArray[randomIndex]
          runAction(SKAction.playSoundFileNamed(selectedFileName, waitForCompletion: false))
          }
      }
      

      // 调用函数 playPinballSound 来播放随机声音。

      【讨论】:

      • 完美!谢谢!
      • 欢迎您
      【解决方案3】:
          let path = NSBundle.mainBundle().pathForResource("Ring", ofType: "mp3")
          let fileURL = NSURL(fileURLWithPath: path!)
          play = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
          play.prepareToPlay()
          play.delegate = self
          play.play()
      

      这只是一个例子

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。

        在将带有声音文件的文件夹复制到项目时,我通过选择“创建文件夹引用”而不是“创建组”解决了这个问题。

        希望这也适用于您。我对 Swift 和整个 iOS 编程非常陌生,所以我不知道幕后究竟发生了什么。

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题。我所做的只是“将文件添加到 MyProject...”并添加整个声音文件夹。一旦 Swift 获得了对该文件夹的引用,就无需在 pathForResource 参数中包含该文件夹。

          【讨论】:

            猜你喜欢
            • 2017-03-30
            • 1970-01-01
            • 2014-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多