【问题标题】:skip to item of index number... on iOS Media Player with Swift使用 Swift 在 iOS 媒体播放器上跳转到索引号项目...
【发布时间】:2015-09-08 08:35:06
【问题描述】:

我想使用项目的索引号跳到另一首歌曲(您可以使用 playlist.indexOfNowPlayingItem 找到)。有没有一种简单的方法可以使用 mediaplayer.skipToNextItem 之类的功能但设置索引号(例如 mediaplayer.skipToItem(4) 如果我想跳到播放列表的第四项)?

【问题讨论】:

    标签: ios swift media-player


    【解决方案1】:

    如果你查看苹果的documentation,你可以看到只有跳到下一项、开始和上一项的方法。但是有一个属性nowPlayingItem 可以让我们获取当前曲目,或者设置要播放的曲目,问题是您是如何设置当前歌曲队列的...

    假设您正在使用:

    1.媒体项目集合

    var songs = [song1, song2]
    var player = MPMusicPlayerController.applicationMusicPlayer()
    var collection = MPMediaItemCollection(items: songs)
    
    player.setQueueWithItemCollection(collection)
    

    所以要设置要播放的新歌曲,我们只需这样做:

    player.nowPlayingItem = song2 (it must be already in our collection)
    

    或者如果你想使用索引号,因为我们有我们的歌曲在数组中,我们也可以这样做:

    player.nowPlayingItem = songs[1]
    

    2。媒体查询

    var query = MPMediaQuery.songsQuery() // query of songs sorted by name
    var songs = query.items as NSarray
    var player = MPMusicPlayerController.applicationMusicPlayer()
    
    player.setQueueWithQuery(query)
    

    现在要设置要播放的新歌曲,我们只需这样做:

    player.nowPlayingItem = songs[1]
    

    【讨论】:

    • 首先,感谢您的优质回答!但是按照您所说的使用媒体查询,它在声明歌曲后告诉我“使用未声明的类型 NSArray”。我尝试了用我的查询创建媒体项目集合的第一种方法,但是在 player.nowPlayingItem = song[1] 之后它告诉我“不能下标 MPMediaItem 类型的值?使用类型的索引()。所以我搜索了关于nowPlayingItem 我找到了答案,女巫是创建一个集合,使用该集合设置队列,然后将歌曲设置为这样播放: player.nowPlayingItem = collection.items[1]
    【解决方案2】:

    解决方案是,创建一个集合为MPMediaItemCollection,设置要播放的歌曲:player.nowPlayingItem = collection.items[1]

    例如:

    var query = MPMediaQuery.songsQuery()
    var collection = MPMediaItemCollection(items: query.items!) //it needs the "!"
    let player = MPMusicPlayerController.applicationMusicPlayer()
    
    player.setQueueWithItemCollection(collection)
    
    player.nowPlayingItem = collection.items[1]
    

    注意:我在这里找到了答案:Play Song at specific index of MPMediaItemCollection in Swift

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 2015-11-20
      • 2014-09-10
      • 1970-01-01
      相关资源
      最近更新 更多