Swift AVAudioPlayer (Xcode) - 在应用程序包外部播放音频文件

标签 swift xcode macos avaudioplayer avkit

在我的 Xcode 项目中,我设置了以下代码(简化代码):

import Cocoa
import AVKit

class ViewController: NSViewController {

    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            guard let filePath = Bundle.main.path(forResource: "sample", ofType: "mp3") else {
                print("ERROR: Failed to retrieve music file Path")
                return
            }
            let fileURL = URL.init(fileURLWithPath: filePath)
            print(" PATH: \(filePath)")
            print("  URL: \(fileURL)")
            try audioPlayer = AVAudioPlayer.init(contentsOf: fileURL)
        } catch {
            print("ERROR: Failed to retrieve music file URL")
        }

        audioPlayer.play() 
    }
}

//CONSOLE PRINTS ----------:
// PATH: /Users/vakho/Library/Developer/Xcode/DerivedData/MusicPlayer-foqzsckozmcjnofvlvhuwabfssqi/Build/Products/Debug/MusicPlayer.app/Contents/Resources/sample.mp3
//  URL: file:///Users/vakho/Library/Developer/Xcode/DerivedData/MusicPlayer-foqzsckozmcjnofvlvhuwabfssqi/Build/Products/Debug/MusicPlayer.app/Contents/Resources/sample.mp3

通过首先转换为 URL,我成功地将sample.mp3(包含在应用程序包中)的文件路径传递给audioPlayer。调用play()函数播放音频文件。

但是,由于我正在创建一个音乐播放器应用程序,我希望能够播放驻留在目录文件夹中的音频文件,例如桌面、下载文件夹等......但是当我尝试传递应用程序包外部音频文件的文件路径,代码中断:

import Cocoa
import AVKit

class ViewController: NSViewController {

    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            let filePathOptional: String? = "/Users/vakho/Downloads/sample.mp3"
            guard let filePath = filePathOptional else {
                print("ERROR: Failed to retrieve music file Path")
                return
            }
            let fileURL = URL.init(fileURLWithPath: filePath)
            print(" PATH: \(filePath)")
            print("  URL: \(fileURL)")
            try audioPlayer = AVAudioPlayer.init(contentsOf: fileURL)
        } catch {
            print("ERROR: Failed to retrieve music file URL")
        }

        audioPlayer.play()
    }
}

//CONSOLE PRINTS ----------:
// PATH: /Users/vakho/Downloads/sample.mp3
//  URL: file:///Users/vakho/Downloads/sample.mp3
//ERROR: Failed to retrieve music file URL

//ERRORS ----------:
//  (lldb)
//  Thread 1: EXC_BAD_ACCESS (code=1, address=0x38)

据我所知,AVAudioPlayer 和 AVKit 库旨在访问应用程序 Assets 和捆绑媒体文件以及来自互联网的流媒体。但无法播放目录中的媒体。

我发现了几个有关该问题的线索,但全部不完整或未得到答复,例如:https://forums.developer.apple.com/thread/18272

所以,如果 AVKit 不能做我认为它应该做的事情,我可以使用什么库或方法来访问目录文件? OSX 的音乐播放器应用程序当然能够提示用户扫描目录并访问音乐文件。他们是如何做到的?

最佳答案

看来问题就在这里:

guard let filePath = filePathOptional else {
    print("ERROR: Failed to retrieve music file Path")
    return
}

请将打印更改为:

print("ERROR: Failed to retrieve music file Path \(error.localizedDescription)")

我想您会发现问题是您无权访问该文件。 默认情况下,您只能访问应用程序和桌面文件夹的沙箱。

另请尝试将文件放入桌面并播放。

关于Swift AVAudioPlayer (Xcode) - 在应用程序包外部播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540569/

相关文章:

ios - 你如何设置 UILayoutPriority?

c++ - 如何在 Xcode 上设置文件以仅在设备上构建而不在模拟器上构建?

ios - 如何检测设备上的 iCloud 帐户是否已更改?

macos - 不使用 cgo 或 SWIG 时不允许使用 C 源文件

ios - 是否可以确定与打开的 NSStream 关​​联的 NSRunLoop/NSThread?

ios - 优化 - 步进可能表现得很奇怪;变量在 Xcode 9.1 中可能不可用

ios - 如何在 Swift 3 中编写键盘通知

ios - 选择器 View 未加载项目

ios - 我什么时候以及为什么要使用 ARC 将局部变量声明为 __weak?

xcode - 如何在 Xcode 6 上运行 Xcode 7 playground?