spotify player plays old track for some moments in iphone - iphone

I implemented Spotify in my application using CocoaLibSpotify library. I am playing spotify songs from different pages of my app. The problem is, after I played a song then when I try to play another song from another page, it plays the old song for some moments.
This is some code sample.
self.playbackManager = [[SPPlaybackManager alloc] initWithPlaybackSession:[SPSession sharedSession]];
self.playbackManager.playbackSession.playbackDelegate = (id)self;
[self.playbackManager playTrack:track callback:^(NSError *error)
{
if ((error || ([track availability] != SP_TRACK_AVAILABILITY_AVAILABLE)))
{
}
else
{
}
}];
Please help me to fix the issue.

It sounds like you're creating a new playback manager for each page of your app. Don't do this, or they'll overlap one another.
Have only a single playback manager in your whole app - that way, when you play a new track using playTrack:callback:, the new track will replace the old one instantly. Note that you do not set the currentTrack property directly - it's read-only.

Related

Unity VideoPlayer and WebGLMovieTexture cant play two videos in a row

I'm trying to play videos in Unity WebGL in the browser, but having lots of problems.
I tried two different video players and none of them work fully.
The WebGLMovieTexture player works like this
public WebGLStreamingVideoPlugin _videoPlugin = new WebGLStreamingVideoPlugin("http://www.example.net/video.mp4");
_videoPlugin.Play();
Basically when you want to play a video you create a new instance and give it the URL like above, and it plays great!!
The problem is when you want to stop that video, and play a different video, it seems impossible because there is no way dispose of the first video because there is only a Stop() in the API, which stops the playback but it continues to stream the video data from the internet in the background.
There is no way to delete the instance because Destroy() cant be called since that WebGLMovieTexture is not derived from monodevelop, and C# does not seem to give a way to delete an object (how silly). Even setting it to null doesnt do it, it continues to stream the video in the background.
So if you create a new instance in order to play a different video, you end up with TWO video streams, and if you do it again to play a third, you end up with THREE, and so on, so quickly you can see how bad that will end up.
My question is how to dispose of or destroy the first WebGLMovieTexture player, or maybe tell it to stop streaming the first video and start playing a different one?
The second player I tried is the VideoPlayer for WebGL in Unity Version 5.6.0b4, with this one I can only get it to play a video if I hardcode the URL in the inspector, if I put the URL in code it doesn't play it in the browser.
vPlayer = gameObject.GetComponent<UnityEngine.Video.VideoPlayer>();
if (vPlayer.isPlaying) {
Debug.Log("STOPPING PLAY");
vPlayer.Stop();
}
vPlayer.url = url;
vPlayer.isLooping = true;
vPlayer.frame = 0;
vPlayer.targetCameraAlpha = 1F;
vPlayer.Prepare();
vPlayer.Play();
And to get it to play a second video I suspect I will have the same problems as the other one.
Anybody have any ideas that can help me?

MPMoviePlayerController background audio issue in iOS5

I have an App that does the pretty standard operation:
It plays audio (streamed or in filesystem) when the app is in 1) Foreground mode, 2) Screen locked state 3)Background mode.
This was working fine in all iOS prior to iOS5.
I have been using MPMoviePlayerController (Because it can play streamed and local file system audio)
I have the following setup:
info.plist has Background Mode set to "Audio"
I have Audiosession setup as shown at http://developer.apple.com/library/ios/#qa/qa1668/_index.html
NSError *activationError = nil;
AVAudioSession *mySession = [AVAudioSession sharedInstance];
[mySession setCategory: AVAudioSessionCategoryPlayback error: &activationError];
if (activationError) { /* handle the error condition */ }
[mySession setActive: YES error: &activationError];
if (activationError) { /* handle the error condition */ }
I have background timer enabled that gets stopped at the end of audio playback
UIBackgroundTaskIdentifier newId = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:NULL];
I have the Moveplayer's useApplicationAudioSession = NO
I have subscribed to the following events to detect and handle various playback state and to start a new audio file at the end of current file.
MPMoviePlayerLoadStateDidChangeNotification
MPMoviePlayerPlaybackDidFinishNotification
MPMoviePlayerPlaybackStateDidChangeNotification
MPMoviePlayerNowPlayingMovieDidChangeNotification
Problem:
With this the audio starts to play and when the application is put to background state or if the phone is locked, the audio continues to play. But, after when I start another audio file,
I start getting PlaybackDidFinishNotification immediately with the state set to Playback ended (But the file was never played)
The same code plays audio files in foreground mode (After the current audio file ends, the next file is started without any problem)
Is there anything new in iOS5 I should be doing to get this to work? I read through the MPMoviePlayerController class reference and I couldn't see anything specific for iOS5.
Thanks in advance.
Finally figured out the issue. This is solved in this post in apple dev forums (needs login to see). That post was applicable to AVPlayer but also fixes the problem with MPMoviePlayerController as well.
Basically, this is an excerpt from that post:
your app must support remote control events! These are the audio
controller interface prex/nex/play/pause on the left of the multitask
switcher taskbar (not sure about the proper name of the thing). You
to this ensuring your view becomes First Controller and then calling
> [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
in viewDidLoad. Once you do this, your Player will no longer return
NO!!
My situation was different and I'm only answering here (and in the other SO question) to help future searchers on this error message. This does not answer the original question.
My app plays a sound OR a song but when I first coded it could play both. And in testing I always tested with a song. I played the song in the usual way:
self.musicQuery = [MPMediaQuery songsQuery];
[_musicQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:selectedSongID forProperty:MPMediaItemPropertyPersistentID comparisonType:MPMediaPredicateComparisonEqualTo]];
[_musicQuery setGroupingType:MPMediaGroupingTitle];
[_myPlayer setQueueWithQuery:_musicQuery];
[_myPlayer play];
Weeks passed and I started testing with the sound, played with AVAudioPlayer. My app started freezing for 5 seconds and I'd get the MediaPlayer: Message playbackState timed out message in the Console.
It turns out that passing a query that was empty was causing the freeze and the message. Changing my app's logic to only play a song when there was a song to play fixed it.

Play a movie in another app then return to my app

I wanted to be able to play a divx or mkv file from my app. So I'm using url schema method, calling a player like Buzz Player via
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString ourPath = #"http://buzzplayer:http://www.mylocalserver.com/file.avi";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
And that works great, it opens the file in the video player fine. But when I press Done in that player, it goes to the Buzz Player menu not back to my app.
It makes sense I guess as I did send the URL request to the player and I'm assuming this isn't Buzz Player specific but something any app would do.
Is there any way I can get an external player to work as the Apple built in video player works; when I press done it jumps back to my app? Or maybe a better solution to this?
I didn't want to reinvent the wheel and build a video player into my app when there are so many great apps that do this out there.
There is no way to get iOS to bring your app back to the front after the other app is done. In your case it seems you'll need to build a movie player into your app. Which isn't that hard to do. Have a look at the MediaPlayer class in the Foundation framework.

Continuous background audio with MPMoviePlayerController?

I am using MPMoviePlayerController to play streaming audio. I'm trying to get background audio working correctly. Right now, audio continues to play when you exit the app - the lock-screen and multi-tasking bar controls even work.
When a song finishes, the app is supposed to advance to the next track and play it. It works when the app is open but not when it is in the background (a song finishes but does not advance to the next track). If a song finishes and you re-open the app, however, the next song will start up immediately.
I am currently using NSNotificationCenter to keep track of when tracks end to advance to the next track (in my app delegate). Again, it works like a charm when the app is open. Is there a better way to do this to keep audio playing after a song is done?
I had this issue lately. Hope the answer helps other people.
If you have a playlist for example and want to play the next song while in background mode or lock mode add this line of code on your viewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
This makes the app supports remote control events.

Big problems when adding the same MPMediaItem into an iPod queue - how can I track which one is playing?

I've had a few complaints about my app, InstanTunes, when people are adding the same song twice to the queue, which is queued in the iPod app.
[[MPMusicPlayerController iPodMusicPlayer] setQueueWithItemCollection:collection];
Basically, the problem is that when the user leaves my app and continues to listen to their queue via the iPod app, InstanTunes loses all control and understanding of what is going on. On return to InstanTunes, I'm faced with the problem of the now playing song, given by [musicPlayer nowPlayingItem]; is in my NSMutableArray of MPMediaItems twice but I have no idea what instance of it is the now playing song.
Here's a simple diagram to illustrate:
Is song A at index 0 playing? Or song A at index 2 playing?
Can anyone think of a way for me to be able to tell which instance of 'Song A' is playing when the user returns to my app? As far as I know, I can't tag the MPMediaItems to be played in the iPod app.
This is really annoying, since I don't want to remove the ability to add the same song twice, but it is causing countless problems.
Any help, insight or suggestions would be greatly appreciated.
You can get the index of the nowPlayingItem