iOS + Swift,如何访问所有音乐文件 [英] iOS + Swift, How to access all music files
问题描述
我正在使用Swift在iOS中制作应用程序.我想要做的就是像iTunes一样将所有音乐文件都存储在iPhone存储设备上:我想显示标题,专辑以及播放曲目.
I'm making an app in iOS using swift. What I want to do is get all music file on the iPhone storage, just like iTunes does: I want to display titles, albums, as well as play tracks.
我是iOS编程的新手:为了实现自己的目标,我应该学习和使用哪种iOS框架?
I'm new with iOS programming: what iOS framework should I learn and use in order to achieve my goal?
谢谢.
推荐答案
这个问题(至少对我来说)有点不清楚.
您要创建一种音乐播放器应用程序吗?您要添加自己的音乐(A)还是使用用户在其音乐应用程序库(B)中拥有的音乐?
You are looking to create a kind of music player application ? You want to add your own music (A) or use the music that the user have in his Music App Library (B) ?
在第二种情况下,您可能正在寻找类似的东西.
In the second case, you might be looking for something like this.
音乐文件由MPMediaItem实例表示.您可以使用通过MPMediaQuery检索它们,例如:
Music files are represented by MPMediaItem instances. You could use retrieve them through a MPMediaQuery, for example :
// All
let mediaItems = MPMediaQuery.songsQuery().items
// Or you can filter on various property
// Like the Genre for example here
var query = MPMediaQuery.songsQuery()
let predicateByGenre = MPMediaPropertyPredicate(value: "Rock", forProperty: MPMediaItemPropertyGenre)
query.filterPredicates = NSSet(object: predicateByGenre)
此时,您已将所有(或一些过滤器)歌曲包含在音乐应用程序库"中,因此在设置播放列表队列后,您可以使用MPMusicPlayerController
播放它们:
At this point, you have all (or some if you filter) songs included in Music App Library, so you can play them with a MPMusicPlayerController
after setting a playlist queue :
let mediaCollection = MPMediaItemCollection(items: mediaItems)
let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()
可能有可能以某种方式访问歌曲中的元数据(标题,流派,艺术家等).
There is probably the possibility to access somehow the metadata (title, genre, artist, ...) from the songs.
这可能无法在模拟器上使用.
这篇关于iOS + Swift,如何访问所有音乐文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!