How do I make MPMusicPlayerController register changes in playlist? - iphone

I'm trying to build a music player app in xcode, but I'm having trouble when changing the playlist (a.k.a. the music player queue). Any time I change something in my playlist (i.e. when addind songs or changing song order, or when pressing the "next" button, etc.) I always seem to have to execute these tow lines of code:
[self.musicplayer play];
[self.musicplayer pause];
apparently, the music player only really registers changes when i send it the "play" command; soon after, i have to send it "pause", so that the song won't actually start playing.
without these lines of code, i would have the following problem (among others):
I would have, say, 6 songs, named "1" through "6". song 1 was playing, but i paused it. I then pressed the "next button" 3 times, which executed the following code (3 times):
[self.musicPlayer skipToNextItem];
therefore, by my logic, the music player should have song number 4 as its current playing song; but when I pressed play, song number 1 would start playing, as if it hadn't noticed I pressed "next".
I was only able to solve the problem by telling it to play and pause, as described above, but that was a very ugly solution, to say the least. also, in some cases, the ipod would play the first seconds of the song before processing it had to pause right away, and that was pretty bad :p
Does anyone know the correct way I should be doing this?
I am using Xcode 4.2.5 preview, and testing it on an iphone 4s with ios 6.

Related

Flutter Implementing pause and play..... Streaming Music

I've tried multiple flutter plug-ins to stream and play and pause music but as of currently, I haven't yet figured out a simple way to implement play and pause, whilst having the ability to avoid music overlapping, I want so that when the current song is playing, if I were to press play on new music track, the current song playing will stop and play the newly pressed song. I've tried flutter_sound, audio player, audio file player, and flute music player but I can't seem to figure out how to do this.
If it is possible either using one of these or a new plug-in, is there a way to implement what I have just described.
The library you seek is more of a multi-track player than a player. Which does not currently exists AFAIK.
The simplest way is to act according to the status of the tracks (which should be given by the player).
Roughly, what I suggest: have something that keeps the status (e.g. paused, playing, completed) of each track. (e.g. List of Tracks) When you press play on track 2 or track 1 finishes playing, you set to 'completed' (or as you wish) the track 1 and set to 'playing' the track 2.
Have a listener on the status of the current track and voila (for autoplay). When the track completes, notify the listener to call a function to start the next one on your list. When you press another track, set to 'completed' the current track and to 'playing' the track you pressed, then notify listeners.
Any library letting you know the status of a track is good enough for your purpose.

AirPlay flickering when switching between movies

I have an app that shows list of movies in a table view. When I play them one after another on device it works just great. But when I switch to Apple TV over AirPlay it doesn't work anymore. It play's the first video on ATV ok but after a switch to the next video the screen on ATV start blinking/flickering and after a few seconds it falls back playing on a device. I am using MPMoviePlayerController for playing stream videos.
I also found out that if previous video finished playing over AirPlay it tries to start the next one over AirPlay also. Is this intended behaviour?
Is this kind a related with property allowsAirPlay?
I think I've found a solution. Before you switch playing another video you should stop the previous one:
[self.moviePlayer stop];
This is not needed if you are not playing via AirPlay cos the next video will automatically stop previous one by nature - you can not play two videos at once.
But If you do play video over AirPlay you need to stop previous one first and than play the next one.
This solution works for me.

Prevent MPMusicPlayerController from playing next song in que

I am creating an app that has a playlist and uses the MediaPlayer library. When a song ends I don't want the next song to start. I tried to use the MPMusicPlayerControllerNowPlayingItemDidChangeNotification to pause the music player when the song changes, but the song starts to play before I get the chance to stop it. I also tried to use KVO to track the changes to musicPlayer.currentPlaybackTime, but I only receive messages when i manually change the playback time. I can't find any information about this problem anywhere.
In my app I have shuffle and repeat off, and create a queue with only one song in it, then send that to the music player. When the song finishes, there aren't any other songs in the queue, so it stops playing.
That being said, the latest iOS update has stopped reporting the correct playback state when the song finishes and is reporting it as paused when it used to be stopped. Not sure if that's a side-effect of the above method or not.
Are you calling:
player.beginGeneratingPlaybackNotifications()
before adding the observers?

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.

Marking an MPMediaItem (podcast) as listened to

Is there a way to update the 'listened to' status of an MPMediaItem from the iPod library?
I've tried using MPMusicPlayerController in iPodMusicPlayer mode and it does not mark the podcast as listened to once it finishes playing.
As far as I can see it's not possible to change media attributes from MPMusicPlayer controller.
The only thing you could try play back the podcast and jump to the last second, then play back the last second (and turn the volume to 0 for that second).
But this is no good way as well because, you have play back something this causes a stop of every other iPod playback.