Spotify API - Detect a change on Active player - api

I'm playing with the Spotify API in a new component on my VueJS playground, and right now I'm able to control the main application from my web app (just see the current track info, be able to play, pause, skip to next song and to the prevoious one).
The thing is that to display the current information of the current song, I'm doing requests to the playback state endpoint (/me/player) to get all the info. I'm doing it every second unless when the user does an action (play, pause, next...), in that case I force a call to get the current state with all the info.
I think that something like a subscription or an event to detect changes on the player would be so much optimum.
I didn't find something like that on the API Reference. Anyone did a workaround for this?
If you want to see the code of the component, It's available on my GitHub.
Thank you in advance :)
p.s: I'm not used to ask in StackOverflow yet so sorry if I'm missing something.

Related

Creating a Playlist for an Apple Music User

I've created an app in React Native which allows a user to search for songs, which uses the Apple Music API. So far, so good.
For the next step. What I want to do is use the songs the user has searched for, and let them save them as a playlist in their Apple Music account (assuming they are a subscriber, etc.).
However, there seems to be a lack of documentation and examples on how to do this (at least compared to Spotify - I'm recreating a Spotify App I made in the past).
I'll need to get authorisation from the user, and then use this endpoint: https://developer.apple.com/documentation/applemusicapi/create_a_new_library_playlist, but I can't find out how exactly to do this. Other parts of the API documentation seem to simply state "With proper authorization from the user, you can also create or modify playlists and apply ratings to the user's content." but never actually explain or link to how to get this authorization.
Can anyone point me in the right direction? And let me know if what I'm trying to do is actually possible with Apple Music - it seems like it is, but the way I'm going around in circles, I'm not so sure anymore. Thank you
I don't know if this will help or not (I'm an iOS dev, not a React dev), but here's the call to save a playlist to a user's Music library, in Swift. Maybe it'll help point you in the right direction(?)
If you'd rather not have Swift code polluting your thread, let me know and I'll remove it.
import StoreKit
// ...
// request user token
if let devToken = UserDefaults.standard.string(forKey: "devToken") {
SKCloudServiceController().requestUserToken(forDeveloperToken: devToken, completionHandler: { userToken, _ in
guard let _ = userToken else { return }
self.addPlaylist()
})
}
// add playlist
func addPlaylist() {
MPMediaLibrary.default().addItem(withProductID: "pl.u-065LACYzL34", completionHandler: { _, error in
guard error == nil else {
print("add playlist sad")
return
}
// success
})
}
I've been looking at trying to do this for a while now.
MusicKit JS works in the browser, but since there is no browser in iOS, that's a no-go: there is no Window.musicKit.getInstance(). And I've yet to find any other way to authorize MusicKit, using JavaScript, except for MusicKit JS.
You can access the Apple music Catalog via the API with only a developer token. You can even access (read only) a user's playlist, if it's public and you know the global id, but that's not what we're after here. We want access to a user's Library so that we can do stuff (except delete, because you can't delete stuff through the Apple Music API).
To authorize access to a user's Library in iOS, you need to use StoreKit, per this, and in order to do that with React Native, you need to use Native Modules per this.
The problem about that second bit, is that I got no idea how to do this. I've read a few things, but I'm not connecting the dot's yet.
If you (or anyone) has figured this out, dropping a note with a how-to here would be awesome. If I do figure it out, I'll come back here and post.
Update: I found this article, which comes close to explaining it really well.
The issues I'm having here are that in the Add a React Native iOS Project section, there's an unfinished sentence ("Here we create..."), and I can't seem to find React.xcodeproj when I run through the steps the author has provided.
Additional remarks: you can run Javascript on a Page in Shortcuts but only when you run shortcuts from sharesheet from Safari.
Also, contrary to what you may seen in many tutorials keep in mind that for POST requests a header Music-User-Token has a value without word "Bearer". In GET request using "Bearer" word will also work. I've built webpage to get music-user-token to clipboard and save it to use it in Shortcuts.

ZF2 - How to get notification like how facebook do it?

My problem now is: How do i get the same effect when facebook(Desktop) give me a notification and add to the total number of notification that have no being read? Example someone tag me on the facebook and there is "1" pop up above the notification icon.
1) I search for push notification but if is not wrong is for mobile devices.
2) Event Manager. Search around but still no luck linking Event Manager and what i want. (I totally think is using event manager but have no idea to start.
Thanks for the help in advance.
Cheers

Star a song in spotify from an external source?

I'm looking for a way to star a song in Spotify without having to go to the app itself.
For instance:
I'm playing a game on fullscreen, or I'm working on something and Spotify is on for music. I hear a great song and I want to star it.
Now instead of going to the app, I want to star the song using a shortcut or a dedicated external button.
Whether it is a shortcut, a USB button, or a button on a remote, I believe this function would be really helpful. I've searched through the Spotify core API, but I couldn't find anything that enables me to "star" a song.
Any ideas?
Since you tagged CocoaLibSpotify in this question, well, CocoaLibSpotify has this functionality built right in - the SPTrack object has the starred property. It'd be fairly simple to write a little app that, when triggered, fetches the currently playing track using the Spotify client's AppleScript bridge and use CocoaLibSpotify to star it.
Alternatively, the Apps API can star a track as well (the Track object has the starred property). You could use the player object to get the current track and star it. However, linking that to an external button will be tricky. One (hacky) way would be to have the button trigger a system URL launch of spotify:app:star-this-track:invocation:1 where the number at the end increments each time you push the button. Then, the application's ARGUMENTSCHANGED event will fire, at which point you can star the track.

Callbacks on SPPlaylist are not called

I'm having problems with receiving changes on a playlist (added, removed, and moved tracks).
I have the following flow:
create SPSession
login
create Playlist from URI
Then I change the playlist in the Spotify desktop app by adding, removing and moving some tracks. Exactly 2 changes are notified via callbacks on my SPPlaylistDelegate. But then it stops. No changes in the playlist are notified anymore. This was working perfeclty on an older version of CocoaLibSpotify.
Can anyone help me with this one?
Without code I can't really answer concretely, but a few things to watch out for:
First, simple stuff:
1) Does this happen in the sample apps?
2) Are you keeping a strong reference to the playlist so it doesn't get deallocated?
More advanced:
3) Are you sure the events aren't being coalesced into one change that encapsulates more than one action?
4) Are you checking the items property of the playlist — are they in sync?
5) How long are you waiting for the changes? They can take a little while to come through — just because one event was immediate doesn't mean the next one will be.
But I really need to see some code to answer this properly.

Need a way to alert users when building spotify app

I'm building a spotify app that does some work in the background and should then alert the user if it finds a match. I can't find a way to notify the user on the main plane when I have found something of interest.
I'd like to have something like a counter on my app name or some form of notification in order to alert the user that something has changed in my app and that they should come to the app.
Does anyone know if this is possible ?
thanks,
Sean
You would need to notify them via Facebook, Spotify doesn't allow you to identify the individual users on the Spotify platform through the api.
Also there is the issue of that your App unloads after 60seconds of the user not being on the App Pane (off in a playlist etc).
So whilst a counter icon that you suggest would be wonderful.
The App Unload-er needs to go away first.