swift中的AVfoundation - 如何包含多种文件类型

时间:2018-04-24 22:54:26

标签: ios swift avfoundation

我想让我的代码搜索多种文件类型(mp3和wav以及flac),但我无法弄清楚如何构建它。目前我有:

for song in songPath
{
    var mySong = song.absoluteString
    if mySong.contains (".flac")

let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: "flac")

由于

1 个答案:

答案 0 :(得分:1)

您可以使用Set集合类和filter方法:

let fileTypes = Set(["flac", "mp3", "wav"])
let filesToPlay = songPath.filter {fileTypes.contains($0.pathExtension)}

例如,如果songPath定义如下:

let songPath = [URL(string:"audio/blah.flac")!, URL(string:"audio/fdfd.mp3")!, URL(string:"audio/ggg.ogg")!]

print(filesToPlay)打印:

  

[audio / blah.flac,audio / fdfd.mp3]