MPMusicPlayerController play not working with Apple Music - ios

I am using the new Apple Music API with MPMusicPlayerController but play method doesn't work in the first time called, but after the second or third time.
My code is the following:
MPMusicPlayerController *appleMusicPlayer = /*access instantiated player*/;
[appleMusicPlayer setQueueWithStoreIDs:#[url]];
[appleMusicPlayer play];
The player is instantiated as follows:
_appleMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
_appleMusicPlayer.repeatMode = MPMusicRepeatModeNone;
_appleMusicPlayer.shuffleMode = MPMusicShuffleModeOff;
[_appleMusicPlayer beginGeneratingPlaybackNotifications];
The url passed in setQueueWithStoreIDs method is a NSString with a country-specific valid iTunesID. I provide you with a screenshot of NSLog output of url after above play method is called.
Any help would be much appreciated.

Make sure url is a string of the store ID!
You can get that here: https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/

Related

YTPlayerView not playing video with Youtube URL

I am using YTPlayerView for playing embedded videos in my app. Everything works perfect with ID but video doesn't play with URL. Here is my code
-(IBAction)playVideo:(id)sender{
self.playerView.delegate = self;
self.playerView.hidden = false;
[self.playerView loadVideoByURL:#"https://www.youtube.com/watch?v=KWTULSf29Ho" startSeconds:0 suggestedQuality:kYTPlaybackQualitySmall];
}
The URL has to be in the format youtube.com/v/VIDEO_ID?version=3, your url was wrong.
if you want to achieve the specific format, change your method which for responding user input url, appending ?version=3 with user input url.
check this,i think you forgot load the video into the player

MPMediaItemPropertyAssetURL returning nil

I am working on a project where I need to play songs from iTunes Library in AVPlayer. For that, I am taking URL "ipod-library://item/item.mp3?id=1577682869916034242" of selected songs from iTunes Library and playing same in AVPlayer. Almost all songs get play, but for few songs MPMediaItemPropertyAssetURL returns nil URL. Also receive following error
-[AVAssetReader initWithAsset:error:] invalid parameter not satisfying: asset != ((void *)0)
Any suggestion on this? and why I am getting nil value from
MPMediaItemPropertyAssetURL
Also any idea how to stream or convert DRM Protected Media track into NSData?.
Please advice.
MPMediaItemPropertyAssetURL can returns null for two possible reason.
The music is not downloaded to your device but added in music
library only.
The music is loaded but its DRM-protected.
DRM-protected asset is not possible to play using AVPlayer, its only able to play using MPMusicPlayer. So you must need to check two things before proceed with AVPlayer.
MPMediaItemPropertyAssetURL is nil ?
MPMediaItem is protected ?
Please see the code below….
MPMediaItem *theChosenSong = [[mediaItemCollection items] firstObject];
NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL];
if(assetURL) {
BOOL bIsProtected = theChosenSong.protectedAsset;
if(!bIsProtected) {
// Do whatever you want to do
NSLog(#"Its not protected");
}
else {
NSLog(#"Its DRM protected");
}
}
else {
NSLog(#"DRM protected or not downloaded locally");
}
I found out that the problem was the song I was trying to get the MPMediaItemPropertyAssetURL property for was actually not on my device. It was listed in the media library, but was actually still in iCloud. Once I downloaded the song to my device then the problem was solved.
Leaving this answer for other people like me.
Even though the music is on downloaded on the device, if it is DRM protected, MPMediaItem.value(forProperty: MPMediaItemPropertyAssetURL) will return nil as mentioned in the comment.
My app kept crashing and I confirmed this with my beta tester.
It seems like MPMusicPlayerController still supports playback so according to this answer.

MPMoviePlayerController not resuming while streaming through url

I have MPMoviePlayerController and it start with url but it doesn't resume after pausing, I don't know how to resume after successful buffer of video.
Please help me.
Thanks in advance.
I tried with this
self.moviePlayer = MPMoviePlayerViewController(contentURL: url)
self.moviePlayer?.moviePlayer.prepareToPlay()
self.moviePlayer?.moviePlayer?.movieSourceType = MPMovieSourceType.Unknown
self.moviePlayer?.moviePlayer.play()
self.presentMoviePlayerViewControllerAnimated(self.moviePlayer)
You can use MPMoviePlayerPlaybackStateDidChangeNotification to get notified for state change. Then check MPMoviePlaybackState for desired state and resume your video. For more reference you can refer to apple document for MPMoviePlayerController. Also set its shouldAutoplay property to YES.
Also checkout this answer in case your self.moviePlayer is not working properly because it is getting out of scope. Try using it as a member variable.

how to play mp3 streaming in objectives c

I have this stream url with mp3 type: http://www.slobodnyvysielac.sk/redata/other/play.php?file=informacna%20vojna%20-%202015-02-17%20financne%20skupiny.mp3
when I open this url with Safari or Chrome, it can play, but I can't play it with objectives C code (iOS).
Please tell me the solution!
Thanks all!
player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:#"http://archive.slobodnyvysielac.sk/informacna%20vojna%20-%202015-02-10%20hudo.mp3"]];
[player play];
Inside your view controller or whatever class you have define this, don't define it inside the event where you stream.
AVPlayer *player
EDIT:
There was a problem with your URL, if you open it in the browser, it opens a flash player so I inspected the flash object and got the original MP3 url which will stream, and you can compare how Google Chrome e.g. reacts to both URLs to notice the difference, now the url in my above code is the correct one

iOS: How do I detect if music is playing in any background music app?

I currently have my game correctly handling disabling its own BGM when music is playing in the built-in iPod app, but it does not detect when an app such as Pandora is playing music.
Currently, in my applicationDidBecomeActive method, I check [[MPMusicPlayerController iPodMusicPlayer] playbackState] to determine whether music is playing. What is the equivalent of this to check if an app like Pandora is playing audio in the background?
AudioSessionGetProperty (as mentioned in jake_hetfield's answer) is deprecated as of iOS 7.
Instead, try this one-liner that uses isOtherAudioPlaying:
BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
Works on iOS 6+.
Check out this question
Seems you can see if another audio is playing by checking the property kAudioSessionProperty_OtherAudioIsPlaying like this:
UInt32 propertySize, audioIsAlreadyPlaying=0;
propertySize = sizeof(UInt32);
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
A complement to this could be to ask the user if he/she wants to have the game music or the already playing sound/music.
As of iOS 8, the secondaryAudioShouldBeSilencedHint property should be used:
/* Will be true when another application with a non-mixable audio session is playing audio. Applications may use
this property as a hint to silence audio that is secondary to the functionality of the application. For example, a game app
using AVAudioSessionCategoryAmbient may use this property to decide to mute its soundtrack while leaving its sound effects unmuted.
Note: This property is closely related to AVAudioSessionSilenceSecondaryAudioHintNotification.
*/
#property(readonly) BOOL secondaryAudioShouldBeSilencedHint NS_AVAILABLE_IOS(8_0);
You may want to do something like this.....
Create A class to handle your audio settings say... "AudioManager"
Poll the Boolean "isOtherAudioPlaying"... maybe assign it to your own Boolean value.
import Foundation
import AVFoundation
class AudioManager {
static let successBingSoundID: SystemSoundID = <Your System Sound ID in Int>
static func playSystemSoundIfBackgroundSoundIsOff() {
guard !AVAudioSession.sharedInstance().isOtherAudioPlaying else {return}
AudioServicesPlaySystemSoundWithCompletion(successBingSoundID, nil)
}
}
usage:
AudioManager.playSystemSoundIfBackgroundSoundIsOff()

Resources