Detect which music from another app is playing in background - swift

I am trying to detect which music from another app is playing in background.
I know there is a way to detect if a music is played in background but I would like to detect which song and the time if I can. How do I do that? In Swift or Objective-c

You can use the MediaPlayer framework to get the current Music playing in the Music app:
import MediaPlayer
let player = MPMusicPlayerController.systemMusicPlayer()
let currentSongTitle = player.nowPlayingItem?.title
let currentPlaybackTime = player.currentPlaybackTime
Note that you need to have the NSAppleMusicUsageDescription (aka Privacy - Media Library Usage Description) set to the String you want to present to the user when asking for permission.

Related

Casting music and video from the same iOS application with different background option doesn't work

I have an iOS application that can play music and video. You can cast music or video from the app using the chromecast sdk. I've been reading the documentation about playing in background. When setting this code:
let criteria = GCKDiscoveryCriteria(applicationID: chromeCastReceiverAppID)
let options = GCKCastOptions(discoveryCriteria: criteria)
options.physicalVolumeButtonsWillControlDeviceVolume = true
options.suspendSessionsWhenBackgrounded = false
GCKCastContext.setSharedInstanceWith(options)
I can keep controlling my music while casting and the app is in background this is great. When casting a video the session is lost while in background and when the app resume it doesn't resume automatically as stated in the documentation. it does work if I set the suspendSessionsWhenBackgrounded = true but then I get suspended when casting music and can't switch to the next song.
Is there a way to switch that option based on what type of content I'm casting?

Swift skip current song playing on iPhone

I would like to display info and skip the current song being played by the iPhone. I understand MPMusicPlayerController can do this for apple music. However, I would like to display and skip the current song if it is being played by a third-party app like Spotify or Audible.
let audioPlayer = MPMusicPlayerController.systemMusicPlayer
next song
audioPlayer.skipToNextItem()
previous song
audioPlayer.skipToPreviousItem()

Missing sound on Apple Watch after resume from dock

I am using the following sounds (all 32K AAC as mentioned in Apple document but I use mono instead of stereo: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/AudioandVideo.html) for my watchOS 3 games.
let hitSound = SKAction.playSoundFileNamed("hit.m4a", waitForCompletion:false)
let explosionSound = SKAction.playSoundFileNamed("explosion.m4a", waitForCompletion:false)
let music = SKAudioNode(fileNamed: "delta.m4a")
I use hitSound and explosionSound for short sound effect, and play music on the background, they works fine until the game is quit and resume from dock.
The music is resume and the sound effects are no longer heard. Is there any problem with using SKAction.playSoundFileNamed to play sound?

playing ipod library songs in audio queues , not with mpmusicplayer controller

i need to play songs from my music library in the background , while playing video..
Both audio and video will play at the same time... first i tried using mpmusicplayercontroller to play music , but when i play video , its stops playing song in background. after that i tried to play music using audiotoolbox."Audio Queues". now i can play audio and video at the same time ,but the song i am playing is static. i want to play song from ipod library and i need to provide the file path there, so is there any way to get the library song path? or any other idea? please suggest..!
While it may be possible, Apple doesn't want you to know how. They don't want people to be able to programmatically access the song data. It can lead to music sharing, which Apple tries not to make easy.
Maybe the way to do this is to have your videos be the thing that is static and get your songs through MPMusicPlayer. Of course, you'll have to play them with a different mechanism than MPMediaPlayer.

Using AVAudioPlayer and MPMoviePlayerController simultaneously

I need to use AVAudioPlayer and MPMoviePlayerController simultaneously i.e play a movie while the background loop is playing is it possible.
My bg music loop stops working when a movie starts playing , so I tried to stop the bg music loop and start the movie and when movie stops, start playing the bg loop again but this is also is not working.
In fact it is achievable from 3.x onwards. I had once done this in one of my applications and its got nothing to do with any backgrounding or any other new iOS features. You just need to configure the audio sessions properly. If you make sure that both your audio player as well as the video player are using the ambient audio session it is possible.
Prior to starting your AVAudioPlayer to play, set the category of audio session to AVAudioSessionCategoryAmbient. This is the system wide common audio session. This allows other sounds to play along such as the sound from the iPod application.
In case you are using the MPMoviePlayerController from iOS 3.2 or above make sure you set the useApplicationAudioSession to NO. This ensures other sounds to play along. It is the default in earlier versions of iOS.
There is a limitation in the current iPhone SDK (2.x and 3.x) that does not allow any background audio playing while a movie is playing in the MPMoviePlayerController API. Unfortunately, it is not possible to do what you are asking at this time within the iPhone SDK.
I would suggest logging an enhancement request to Apple to offer this in a future version of the SDK.