无法在Swift游乐场中枚举MP3标签

时间:2023-01-23 19:12:43

I'm trying to enumerate the ID3 tags in an MP3 file in an Xcode playground (using Swift) but all I get is an empty array. I've also tried 'nil' for the options (I also tried the one below out of desperation):

我试图在Xcode游乐场(使用Swift)的MP3文件中枚举ID3标签,但我得到的只是一个空数组。我也尝试了'nil'选项(我也在绝望中试过了下面的那个):

import Cocoa
import AVFoundation

var url = NSURL(fileURLWithPath: "/Users/marc/test.mp3")
let opts = [ AVURLAssetPreferPreciseDurationAndTimingKey: true ]
var asset = AVURLAsset(URL: url, options: opts)
var metaData = asset.commonMetadata

for item in metaData {
    print("key: \(item.commonKey), value: \(item.stringValue)")
}

Am I doing something wrong, and can anyone else get this to work?

我做错了什么,其他人可以让它工作吗?

1 个解决方案

#1


1  

Playgrounds are sandboxed. You can't read from or write to the filesystem.

游乐场是沙盒。您无法读取或写入文件系统。

But you can read the files that are inside the Playground.

但是您可以阅读Playground中的文件。

Open the project navigator:

打开项目导航器:

View > Navigators > Show Project Navigator

查看>导航器>显示项目导航器

And drop your MP3 in the Resources folder.

并将您的MP3放在Resources文件夹中。

Once it's there, drag the file from inside the navigator to the source code area: it will make an icon representing the file URL.

一旦它在那里,将文件从导航器内拖到源代码区:它将创建一个代表文件URL的图标。

You can then use your code with this file:

然后,您可以将此代码用于此文件:

let url = (the file icon dragged from the project navigator)

let asset = AVURLAsset(URL: url, options: nil)
let metaData = asset.commonMetadata

for item in metaData {
    if let key = item.commonKey, let value = item.stringValue {
        print("key: \(key), value: \(value)")
    }
}

Screenshot:

无法在Swift游乐场中枚举MP3标签

#1


1  

Playgrounds are sandboxed. You can't read from or write to the filesystem.

游乐场是沙盒。您无法读取或写入文件系统。

But you can read the files that are inside the Playground.

但是您可以阅读Playground中的文件。

Open the project navigator:

打开项目导航器:

View > Navigators > Show Project Navigator

查看>导航器>显示项目导航器

And drop your MP3 in the Resources folder.

并将您的MP3放在Resources文件夹中。

Once it's there, drag the file from inside the navigator to the source code area: it will make an icon representing the file URL.

一旦它在那里,将文件从导航器内拖到源代码区:它将创建一个代表文件URL的图标。

You can then use your code with this file:

然后,您可以将此代码用于此文件:

let url = (the file icon dragged from the project navigator)

let asset = AVURLAsset(URL: url, options: nil)
let metaData = asset.commonMetadata

for item in metaData {
    if let key = item.commonKey, let value = item.stringValue {
        print("key: \(key), value: \(value)")
    }
}

Screenshot:

无法在Swift游乐场中枚举MP3标签