【问题标题】:How to Play Sound File in Swift 2如何在 Swift 2 中播放声音文件
【发布时间】:2016-01-06 13:05:53
【问题描述】:

我正在使用下面的代码,但没有听到任何声音。

var audioPlayer:AVAudioPlayer?

override func viewDidLoad() {

    super.viewDidLoad()

    let path = NSBundle.mainBundle().pathForResource("SecondBeep", ofType: "wav")
    let url = NSURL.fileURLWithPath(path!)

    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: url)
    } catch {
        print("Player not available")
    }
    audioPlayer?.prepareToPlay()
    audioPlayer?.play()

}

AVFoundation 已正确导入,声音文件已添加到项目中。检查了其他 stackoverflow 主题,但没有帮助。

尝试更改为URLForResource,但也无济于事。

    let path = NSBundle.mainBundle().URLForResource("SecondBeep", withExtension: "wav")
    let url = NSURL.fileURLWithPath(path!.path!)

我该如何解决这个问题?

【问题讨论】:

  • 您能否更具体地说明“不起作用”?你的意思是它不播放?
  • 为什么不简单地使用 URLForResource 而不是 pathForResource?
  • @Arc676,什么都听不见。我看到在控制台中打印文件和播放器,但没有听到任何声音。
  • @LeoDabus,试过了,但也没有用。
  • @JohnBernard 编辑您的帖子并显示您的实际代码。还要确保您将文件复制到您的项目中,并在需要时检查副本

标签: swift audio


【解决方案1】:
import UIKit
import AVFoundation
class ViewController: UIViewController {
    var audioPlayer:AVAudioPlayer!
    override func viewDidLoad() {
        super.viewDidLoad()
        if let myAudioUrl = NSBundle.mainBundle().URLForResource("SecondBeep", withExtension: "wav") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOfURL: myAudioUrl)
                audioPlayer.prepareToPlay()
                audioPlayer.play()
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

【讨论】:

  • 谢谢。我创建了一个新项目并添加了声音文件,您的代码确实可以在模拟器中运行。但是当我将应用程序发送到我的 iPhone 6s Plus 时,它不起作用。对这个问题有什么想法吗?在手机应用中播放本地文件是安全问题还是权限问题?
  • 好的,我必须接受您的回答,因为它与模拟器一起使用。仍然无法在我的 iPhone 上运行。
  • @JohnBernard 如果你不明白,请告诉我
  • 我不确定。我检查了编译的 .app 文件,声音文件也在那里。模拟器播放声音,但 iPhone 不播放。
  • 尝试隔离您的问题,创建一个新的示例项目,然后将您的文件从查找器中拖放到 swift 文件之外。如果需要,请检查副本复选框
【解决方案2】:
import UIKit
import AVFoundation
class ViewController: UIViewController {

  var audioPlayer: AVAudioPlayer!  // Global variable

    override func viewDidLoad()
    {
        super.viewDidLoad()

        playVes()


        // Do any additional setup after loading the view, typically from a nib.
    }

    func playVes() {
        do {
            self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("123", ofType: "mp3")!))
            self.audioPlayer.play()

        } catch {
            print("Error")
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

【讨论】:

  • @BalajiGupta,这和我的代码基本一样,不是吗?
  • 这是 Swift 1 代码,而不是 Swift 2! OP 的代码示例在 Swift 2 中... 这么糟糕的答案怎么会有赞成票?叹息……
  • @BalajiGupta 你知道什么是全局变量吗?
  • 它的全局变量出现在这个视图控制器中。
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 2014-11-25
  • 2014-05-09
  • 2010-09-12
  • 2014-12-26
  • 1970-01-01
  • 2015-11-09
  • 2017-03-28
相关资源
最近更新 更多