ios - 从 String 获取用于 AVPlayer 的 URL 的问题

标签 ios swift url avplayer nsurl

我构造了一个字符串,它是我想在 AVPlayer 中播放的文件的目录路径和文件名。但我正在努力将其转换为 URL。

let dataPath = ("\(packDirectory)/").appending((cachedFilePath as NSString).lastPathComponent)
print(dataPath)
audioFileURL = dataPath
self.audioPlayerItem = AVPlayerItem(url: NSURL.fileURL(withPath: audioFileURL) as URL)
print(self.audioPlayerItem)

print(dataPath) 返回:

/Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/788085B9-242E-46E7-9644-6A3BF9D515DB/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3

print(self.audioPlayerItem) 返回:- url 部分:

URL = file:///Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/788085B9-242E-46E7-9644-6A3BF9D515DB/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%2520Name%2520of%2520the%2520Wind%252029-92.mp3

我不太了解这些东西,但我可以看到 2 个问题。

1) file://:我特意从我的“dataPath”中删除了它,因为在使用文件管理器时它找不到任何带有这个前缀的东西。我为此使用了这段代码:

userDirectory.removeSubrange(userDirectory.startIndex..<userDirectory.index(userDirectory.startIndex, offsetBy: 7))
let packDirectory = userDirectory.appending("Next-\(self.selectedPack!)")

2) .lastComponent 中的编码已从 %20 更改为 %2520

很困惑!

---- 编辑----

let urlItem = AVPlayerItem(url: URL(fileURLWithPath: audioFileURL))
if let urlAsset = urlItem.asset as? AVURLAsset {
    self.audioPlayerItem = AVPlayerItem(url: NSURL(string: urlAsset.url.path) as! URL)
    print(urlAsset.url.path)
}
print(self.audioPlayerItem!)

print(urlAsset.url.path) 返回:

/Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/BAD0194A-8CDD-44CE-BF99-B9FF35E23BCA/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3

print(self.audioPlayerItem!) 返回:

<AVPlayerItem: 0x7bf81b60, asset = <AVURLAsset: 0x7bf87c70, URL = /Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Applicati ... 9-92.mp3>>

self.audioPlayerItem = AVPlayerItem(url: URL(fileURLWithPath: urlAsset.url.path))

打印:

file:///Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/7C212656-8E1C-44C8-9951-4444FB5EF853/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%2520Name%2520of%2520the%2520Wind%252029-92.mp3

甚至使用类似的东西:

let asset = AVAsset(url: URL(string: urlAsset.url.path)!) as AVAsset

导致部分 url 丢失:

<AVPlayerItem: 0x79716ed0, asset = <AVURLAsset: 0x797185d0, URL = /Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Applicati ... 9-92.mp3>>

没有播放。所以基本上我认为它不会使用 file://前缀,但是当我在没有它的情况下插入字符串时,某些东西会切断应用程序中 i 处的路径???

最佳答案

  1. 您可以阅读 URL documentationabsoluteStringpath . 要在缺少 file:/// 的情况下获取 self.audioPlayerItem 的 URL,您可以访问 self.audioPlayerItem 的 Assets 并获取 的路径 Assets .url

    let asset = self.audioPlayerItem?.currentItem?.asset
    if asset == nil {
        return nil
    }
    if let urlAsset = asset as? AVURLAsset {
        return urlAsset.url.path
    }
    

已编辑:如果您使用本地文件,则使用 URL.init(fileURLWithPath: ) 而不是 URL.init(string: ) 初始化 URL >。如果您使用 URL.init(string: ),字符串将是完整路径(包含 file:///),而不是路径。

  1. 没关系,解码结果是一样的。尝试阅读引用文献 A html space is showing as %2520 instead of %20

P/S:该片段只是伪造的,请遵循该片段的想法。

关于ios - 从 String 获取用于 AVPlayer 的 URL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42404420/

相关文章:

ios - 是否可以仅覆盖表单的暗模式?

ios - NSDate 比用户本地时间晚一小时

arrays - 从数组中删除对象

c# Convert.To Base64(String) 没有脉冲

java - 网络驱动程序 URL 中的通配符

ios - 使用 Spotify iOS SDK 3.0 在 SPTRequest block 周围运行 for 循环

ios - 如何使UITableView高度动态变化到特定高度,然后在不使用ios中使用AutolayOut的情况下进行滚动

ios - 如何在 SwiftUI 中设置清晰/透明背景的导航栏?

swift - Core ML swift 用例

html - 将空查询字符串传递给 URL 是一种错误的形式吗?