本文介绍了swift xcode播放播放列表中的声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找播放列表中播放声音的快速编码,而不是作为资源添加到您的项目中的声音。
我主要使用
NSURL(fileURLWithPath:NSBundle.mainBundle()。pathForResource(sound_name,ofType:wav))
println(alertSound)
但是为此,你需要在你的包中有声音文件。但我找不到任何示例
选择通过iTunes购买的音频文件,并播放它们。



任何想法如何做到这一点?我可以访问我的音乐层播放列表文件,并在我的应用程序中使用它们吗?



感谢您的任何代码行。
rpw

解决方案

这些音乐文件由MPMediaItem实例表示。要获取它们,可以使用 MPMediaQuery ,如下所示:

  let mediaItems = MPMediaQuery.songsQuery ().items 

此时,所有歌曲都包含在音乐应用程序库中,所以您可以设置播放列表队列后,用 MPMusicPlayerController 播放:

  let mediaCollection = MPMediaItemCollection(items:mediaItems )

让player = MPMusicPlayerController.iPodMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)

player.play()

您可能需要按流派,艺术家,专辑等过滤歌曲。在这种情况下,您应该在获取媒体项目之前将谓词应用于查询:
$ b $ pre $ var $ query = MPMediaQuery.songsQuery()
let predicateByGenre = MPMediaPropertyPredicate(value:Rock,forProperty:MPMediaItemPropertyGenre)
query.filterPredicates = NSSet(object:predicateByGenre)

let mediaCollection = MPMediaItemCollection(items:query。项目)

let player = MPMusicPlayerController.iPodMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)

player.play()

干杯!


I am looking for a swift coding playing sound out of the player list and not sounds added as resource to your project. I mainly found the usage ofNSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_name", ofType: "wav")) println(alertSound)but for this you need to have the sound file in your bundle. But I couldn't find any exampleselecting audio files bought thru itunes and play them.

Any idea how to do this? Can I access my music layer playlist files and using them in my app?

Thanks for any code lines.rpw

解决方案

These music files are represented by MPMediaItem instances. To fetch them, you could use an MPMediaQuery, as follows:

let mediaItems = MPMediaQuery.songsQuery().items

At this point, you have all 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.iPodMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)

player.play()

You might need to filter songs by genre, artist, album and so on. In that case, you should apply a predicate to the query before fetching the media items:

var query = MPMediaQuery.songsQuery()
let predicateByGenre = MPMediaPropertyPredicate(value: "Rock", forProperty: MPMediaItemPropertyGenre)
query.filterPredicates = NSSet(object: predicateByGenre)

let mediaCollection = MPMediaItemCollection(items: query.items)

let player = MPMusicPlayerController.iPodMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)

player.play()

Cheers!

这篇关于swift xcode播放播放列表中的声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:38