在iPhone上的Spotify中播放当前歌曲 [英] Get current song playing in Spotify on iPhone

查看:348
本文介绍了在iPhone上的Spotify中播放当前歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问 MPMusicPlayerController.systemMusicPlayer()(下面的代码)适用于获取Apple Music应用程序中播放内容的曲目信息,但我们是否可以访问信息当前在Spotify应用程序中播放的歌曲?

Accessing the MPMusicPlayerController.systemMusicPlayer() (code below) works for getting track info for what's playing in the Apple Music app, but is there a way we can access information of the current song playing in the Spotify app?

此代码发布在此答案我需要知道如何获取有关哪个播放器当前正在播放的信息(播放器,spotify,napster ......)使用 MPNowPlayingInfoCenter 无论是使用Apple Music还是Spotify等都是零。

This code posted in this answer I need to know how to get information about which player is currently streaming (player, spotify, napster...) uses MPNowPlayingInfoCenter which is nil whether using Apple Music or Spotify etc.

  let player = MPMusicPlayerController.systemMusicPlayer()

    @IBAction func getMusicButton(_ sender: UIButton) {




        if let mediaItem = player.nowPlayingItem {
            let title: String = mediaItem.value(forProperty: MPMediaItemPropertyTitle) as! String
            let albumTitle: String = mediaItem.value(forProperty: MPMediaItemPropertyAlbumTitle) as! String
            let artist: String = mediaItem.value(forProperty: MPMediaItemPropertyArtist) as! String

            print("\(title) on \(albumTitle) by \(artist)")
        }

    }


推荐答案

Apple文档声明有两种类型的音乐播放器:

The Apple Documentation states there are two types of music player:



  • 应用程序音乐播放器在您的应用中本地播放音乐。它不知道音乐应用程序正在播放的项目,也不知道
    是否会影响音乐应用程序的状态。有两个应用程序音乐播放器:
    applicationMusicPlayer applicationQueuePlayer 。应用程序
    队列播放器可以更好地控制队列内容
    并且是首选播放器。

  • 系统音乐播放器使用代表您的内置音乐应用程序。在实例化时,它采用当前的音乐应用状态,
    ,例如现在播放项目的标识。如果用户在播放音乐时从您的应用中切换
    ,则该音乐将继续播放
    。音乐应用程序然后有你的音乐播放器最近设置的
    重复模式,随机播放模式,播放状态和现在播放的项目。

  • An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it affect the Music app’s state. There are two application music players: applicationMusicPlayer and applicationQueuePlayer. The application queue player provides greater control over the contents of the queue and is the preferred player.
  • The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The Music app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.

第一个只在你的应用程序中,第二个是Apple Music应用程序,因为你无法在普通应用程序中伸出应用程序的沙箱,这在App Store中是不可能的应用。

The first is only within your app and the second is the Apple Music app, since you can't reach out of your app's sandbox in a normal app this is not possible in App Store apps.

如果您仍想创建自己的想法,可以在越狱设备上进行开发。 这个github项目有一个简单的示例,说明如何获取当前播放的歌曲在越狱调整中。如果你想试试,这个是一个很好的,是否有点老,仍然准确的教程如何开始进行调整。

If however you still want to create your idea you can develop this on a jailbroken device. This github project has an easy example of how to get the current playing song in a jailbreak tweak. If you want to try it, this is an excellent, be it a bit old, still accurate tutorial on how to get started developing tweaks.

更新

我刚遇到这个Spotify SDK 将让用户登录并播放用户Spotify音乐。通过这种方式,音乐流将位于您的应用内,您可以访问applicationMusicPlayer中的所有信息。不利的一面是,设置这项工作要比获得有关Spotify的播放曲目的信息要多得多,而且用户必须使用您的应用而不是Spotify应用播放他的音乐。

I just ran into this Spotify SDK that will let users login and play the users Spotify music. This way the music stream will be inside your app and you have access to all info in the applicationMusicPlayer. The downside is that this will be a lot more work to set up than just getting the information about Spotify's playing track and the user will have to play his music from your app instead of the Spotify app.

这篇关于在iPhone上的Spotify中播放当前歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆