Detecting which audio track is playing on device in iOS Swift - ios

I'm creating a mood tracking app that, among other things, should use information about the songs the user listens to. Specifically, I'm interested in just extracting the titles that are otherwise visible from the locked screen view, when a track is playing. I've search the interwebs and have had no luck finding a solution to access this data using Swift.
Can anyone help?

The short answer is no.
You may be able to retrieve song information from the iTunes library (if it's not a shared song using Home Sharing). Read about MPMusicPlayerController for more information. Specially the nowPlayingItem is probably what you're looking for.
However, most third-party applications such as Spotify, Deezer or Pandora will most likely have implemented their own player, which does not integrate with the iPod music player. This blog post also covers some of the details.

It is not about your ask, but it is about theme.
I made a code to get information about track in Itunes Media, and if you need get album art or correctly track name or artist, well I think it is will help you. The code is free to use in github.
ItunesSearch

Related

iOS - play a song from iTunes which the user has not purchased?

I have not started coding my app but I have an idea and want to see if this part of it is possible. Would it be ok if, given a track ID, I played the song (through MediaPlayer) in the app, even if the user had not bought the song and do not have it in their library?
If not, what would be the best way to play music that anyone (or most people, at least) can listen to as they use my app?
You can play music that:
is in the user's library
or, is embedded in your app
or, is available online where your app can readily access it (assuming reachability etc.)

iOS : is it really impossible to get info of current track being played in third-party apps (like Spotify)?

So far, I have red many conflicting answers about this.
In this SO thread, it is said to use:
let player = MPMusicPlayerController.systemMusicPlayer()
if let mediaItem = player.nowPlayingItem {
// ...
}
However, this only works with the iOS player. If the current song is being played by Spotify for example, mediaItem will be nil.
I understand that Apple's policy doesn't allow to access any other application's data. The only thing I am able to do right now is to know if a song is playing from another player with the help of AVAudioPlayer's secondaryAudioShouldBeSilencedHint and isOtherAudioPlaying.
I want to know, however, if there is another way to access it, like using Spotify framework? (I am absolutely non familiar with it, that's just making assumptions).
Thanks for your help.
I am not sure about iOS but the current track can be read from Spotify on a Mac via AppleScript. I use this technique from Objective C. If you're interested I can post the code.
Spotify publish their API for AppleScript here https://developer.spotify.com/applescript-api/
If you're looking for a generic way of determining what is playing then I think that you will be disappointed. Each application will have a different way of retrieving this information.
So yes and no. If you want to specifically only check if Spotify is playing, then perhaps the Spotify iOS SDK provides functionality for such a thing. I really don't know about that SDK's functionality.
I would venture to guess that your actual goal is to see if any third party framework is playing; Pandora, Tidal, Apple Music, Amazon Prime Music, etc. In which case, you would need a framework for each one that provided such functionality.
Apps are sandboxed from each other for security, so yes, there is no way to tell the current track information other than if you have the framework in place and it provides that functionality.

How to get another app's currently playing audio [duplicate]

This question already has an answer here:
Audio Information of Current Track iOS Swift
(1 answer)
Closed 6 years ago.
How can I access another app's currently playing audio - the actual audio item but metadata is welcome too. I can see that this question has been asked a lot but with few solutions being offered over the years. I understand apple's philosophy for probably not wanting an app to be able to do this. I also understand that such a request is probably outside of the iOS API. With that being said, I would really like some kind of solution.
Logically, I feel that
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
should return the info for whatever is currently playing; however, as others have mentioned, this value is always nil for audio being played outside of your app. Notably, popular audio apps seem to fail to use the MPNowPlayingInfoCenter class, making such audio fail to appear.
If using the default music app, one can use
MPMusicPlayerController.systemMusicPlayer().nowPlayingItem
However, what is a more consistent way to access audio playing through the podcasts app, Spotify, Pandora, Safari, etc?
Has anyone found a solution to this? Are there any old Objective-C frameworks that support this functionality?
One approach might be viable if there is there some way I can access the audio path of the item currently being played. For example, if I could get the path of the currently playing item, I could create an AV object from it:
AVAudioPlayer(contentsOfURL: audioUrl)
So is there a way I can get the audio url of the currently playing item and use it that way?
Is another approach better?
If a native solution does not exist, is it possible to bodge something together for it to work? Any advice or ideas welcome.
Edit: I don't believe anybody has been able to achieve this; however, I think a lot of people would like to. If this has been addressed, please link it! :)
This isn't currently possible in iOS. Just changing your AVAudioSession category options to .MixWithOthers, what will be an option to get info Song Info from other apps, causes your nowPlayingInfo to be ignored.
iOS only considers non-mixing apps for inclusion in MPNowPlayingInfoCenter, because there is uncertainty as to which app would show up in (e.g.) Control Center if there are multiple mixing apps playing at the same time.
Proposal: An option would be to use a music fingerprinting algorithm to recognize what is being played by recording it from your App.
Some interesting projects in this direction:
Gracenote https://developer.gracenote.com/ Gracenote (which is owned by Sony) has opened up it's SDKs and APIs and has a proper dev portal.
EchoNest & Spotify API http://developer.echonest.com/ Fusioned with Spotify since March 2016
ACRCloud https://www.acrcloud.com/ offers ACR solutions for custom files such as TV commercials, music tec

Record Video and sharing it in ios app

In my app I want to give the user the option to record a video and share it. What is the easiest way to achieve this? (I have been trying to add video recording for days now and keep hitting dead ends). Can anyone offer some help?
To allow functionality to record videos within your app, please go to the Apple documentation for UIImagePickerController for taking pictures and movies
UIImagePickerController Class Reference Link
Once you have your video, sharing it is a little more difficult as unlike text or images, the movie file sizes can often be very large and may have to rule out simple sharing via email, text-message etc.
The best way to share in this case, would be to connect to an external API, like YouTube, where you can upload the video from the app. Here's a link to YouTube documentation for this
YouTube API Documentation Link
I suggest these as good places to start, as this is what you are asking for.
Hope this helps

Why can't I link to Rick Astley in iOS?

No, wait, don't go! I'm serious.
I was (am?) attempting to rickroll my coworkers in celebration of defeating the iOS beast in our latest battle, when the hydra decided to get the last laugh. Clicking on links to, or openURL with, either of http://www.youtube.com/watch?v=dQw4w9WgXcQ or http://www.youtube.com/watch?v=oHg5SJYRHA0 (the two major rickrolling videos) results in a "Could not load movie" error. But when I find the video through the Youtube app, it plays fine. Other video URLs chosen at random and run through the exact same pathways work fine.
I'm not sure how else to ask this, other than WTF? Could Apple / Youtube be special-casing these videos?
Official music videos often have all kinds of restrictions for mobile playback... try a less popular video of the song?
Could Apple / Youtube be special-casing these videos?
Sure, they might. The Wikipedia entry on "rickrolling" says this:
The meme is a bait and switch; a person provides a hyperlink seemingly
relevant to the topic at hand, but actually leads to Astley's video.
Fooling the user is very much against the spirit of the iOS app store review guidelines, and it's probable that you're not the first person to try this. So it's possible that certain videos are special-cased, or that certain videos are marked with an attribute that prevents them from loading.
Further down in the article, Wikipedia also says:
In October/November 2009, a worm designed to infect jailbroken iPhones
changed the wallpaper of infected phones to a picture of Rick Astley
overlaid with the text "ikee is never going to give you up".
This seems like an even better reason for iOS to have code that avoids videos involving Rick Astley. Not that the worm is directly connected to the videos, but if the unexpected appearance of Rick Astley is a meme that's associated with malware, you can imagine Apple wanting to prevent loading such content by untrusted apps.
STATUS: Although the OP's VideoID's are restricted for viewing on YouTube website only, this is the method to watch YouTube Videos on iOS Devices.
You will need to link using YouTube's Embed URL Method for iOS.
Example links:
http://www.youtube.com/embed/VIDEOID
http://www.youtube.com/embed/VIDEOID?autoplay=1
NOTE: The second links is formatted with YouTube autoplay Option.
Status Update: See this SO Answer that may prove useful. As those answer comments state, iOS will swap out the HTML tags automatically.

Resources