MPMusicPlayerController won't seek to given playbackTime - ios

I want MPMusicPlayerController.applicationMusicPlayer() instance to start playing from a specific starting time:
applicationMusicPlayer.setQueueWithStoreIDs([String(track.id)])
applicationMusicPlayer.currentPlaybackTime = 10.0
print(applicationMusicPlayer.currentPlaybackTime) // will print 10.0
But as soon as the player starts playing the item, it will reset its currentPlaybackTime to zero and will start from the beginning:
applicationMusicPlayer.play()
print(applicationMusicPlayer.currentPlaybackTime) // will print 0.0
I thought maybe it's because I set the playback to the player that has just been created and is not ready yet, but neither .prepareToPlay() or .isPreparedToPlay() help me with that situation.
I even tried to wait for a few seconds and only then start the playback from the position that I've set. No success at all, extremely frustrating.
Maybe it is somehow connected to the fact that I'm playing songs from Apple Music directly? I can't switch to AVPlayer 'cause I have to play music from Apple Music.
Would appreciate any thoughts and help!
UPDATE: I found the method beginSeekingBackward at the MPMediaPlayback and it says that if the content is streamed, it won't have any effect:
https://developer.apple.com/documentation/mediaplayer/mpmediaplayback/1616248-beginseekingbackward?language=objc
Seems like only built-in Music app has a control over streaming music playback?

I had just run into this exact problem. I managed to get around it with the follow code.
It creates a background thread that continuously checks the currentPlaybackTime of the player. As soon as currentPlaybackTime is not the time I set it to be I set currentPlaybackTime back to what I wanted.
It feels like a terrible hack but it's working for me so far.
MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];
player.currentPlaybackTime = _startTime;
[player play];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
while(true) {
if (player.currentPlaybackTime != _startTime) {
player.currentPlaybackTime = _startTime;
break;
}
}
});

Related

How to play a normal video in Super Slow Motion Mode in iPad

I have created video player using AVPlayer. Video is playing very well. Now i am trying to play video in Super Slow Motion Mode. So i am changing frameRate value to 0.0001. But it seems AVPlayer unable to play in very slow mode. Kindly any expert suggest me or any other alternative.
This is play button clicked function.
- (void)playButtonPressed:(id)sender {
[video1ControlView.player prepareToPlayVideo]; }
-(void)prepareToPlayVideo
{
if(self.outputFileInternal != nil)
{
AVPlayer *currentPlayer = [self player];
currentPlayer.rate = self.frameRate; // here frameRate = 1 means, video play in normal play. if i will pass 0.25 then video will play slow mode but i want to play in **SUPER SLOW MOTION MODE**
_playerStatus = PlayerStatusPlaying;
}
}

Problems with currentPlaybackTime and MPMusicPlayerController and iOS 7.1

Having problems setting currentPlaybackTime with MPMusicPlayerController in iOS 7.1.
I used to be able to simply do the following:
MPMusicPlayerController *iPodController =
[MPMusicPlayerController applicationMusicPlayer];
iPodController.currentPlaybackTime = 30.0;
[iPodController play];
And the music player would seek to 30 seconds in and play.
As of iOS 7.1 this is not the case.
If I do the following:
[iPodController play];
iPodController.currentPlaybackTime = 30.0;
Then it "may" jump 30 secs in or not. Very inconsistent.
This used to work for all previous iOS versions. Is there a way to fix this?
I find that I can't set currentPlaybackTime before playing a given song.
Using your first snippet:
iPodController.currentPlaybackTime = 30.0;
[iPodController play];
setting the currentPlaybackTime property does nothing and I can't seek to the desired playback time. But making the calls the other way around have worked consistently for me with iOS 7.1 so far:
[iPodController play];
iPodController.currentPlaybackTime = 30.0;

UISlider and playing sound in iOS

I'm trying to use a UISlider to play sound in a fashion which emulates how a violin bow would play on a string. At current, I can get sound playing from the slider however the functionality isn't correct.
-(IBAction) slider
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"im_on" ofType:#"mp3" ];
if (song) [song release];
song = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[song play];
}
This is code and there is not much else to it. I have linked this method in IB to a slider which is triggered on the event Value Changed. When set to continuous = YES, when the slider begins to slide, the sound plays. However if it continues to slide the sound will stop until the slider stops where it then play again. When set to continuous = NO, the sound does not play when the value changes but when the touch is lifted. the sound continues to play while the value is hanging after that.
Is there a way to have the sound continue to play until the slider stops/changes direction/touch is released? i.e override all but the first value changed sent event until x condition is met.

How do I loop a video with AVFoundation without unwanted pauses at the end?

I am trying to play a video clip that loops indefinitely. I am doing this the way Apple recommends; by setting up a notification that gets triggered by AVPlayerItemDidPlayToEndTimeNotification:
#property(nonatomic) AVPlayer *videoPlayer;
#property(nonatomic) AVPlayerItem *videoPlayerItem;
-(void)loadVideo
{
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSString *tracksKey = #"tracks";
[asset loadValuesAsynchronouslyForKeys:#[tracksKey] completionHandler:
^{
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded)
{
[self setVideoPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];
[videoPlayerItem addObserver:self forKeyPath:#"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector (videoPlayerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:videoPlayerItem];
[self setVideoPlayer:[AVPlayer playerWithPlayerItem:videoPlayerItem]];
[scenePlayerView setVideoPlayer:videoPlayer];
}
});
}];
}
When triggered, this calls my method to effectively rewind and play the clip again:
-(void)videoPlayerItemDidReachEnd:(NSNotification *)notification
{
[videoPlayerItem seekToTime:kCMTimeZero];
[videoPlayer play];
}
The problem is, there is a brief but visible pause in the video playback every time it hits this method and loops back to the beginning.
The video clips are H.264 and have been tested in other players to ensure that they have no visible "skips" in their content. This is all happening under iOS6 both in the Simulator and on an iPad2 and iPad3.
What am I doing wrong?
You have two possible approaches to get a really professional looking loop with no glitches (AVPlayer by itself will not work). First, you could take your original video and decode it to a series of RGB frames (images). Then encode a new h.264 video and feed the frames into a longer h.264 video that is much longer than the first. For example, you might encode a 10 second looping clips 6 times to make a clip that last one minute or 6 * 5 to make a clip that would play for 5 minutes without glitching when the loop restarts. Then, open the longer clip and play it with AVPlayer and it will not glitch for a period of time.
The second approach would be to use an existing library that already handles seamless looping. You can take a look at my library, see my answer to basically the same question iphone-smooth-transition-from-one-video-to-another. The character in the example at the linked answer uses a repeating "loop" of one video clip that loops over and over when no buttons have been pressed.
Unfortunately it appears this actually is not currently possible using AVPlayer as it stands- there is an unavoidable hiccup upon beginning playback in an AVPlayer in every case. So "messier" hacks appear to be the only way to solve this, using multiple AVPlayers, etc.
I managed to get it working with two AVPlayer, two AVPlayerLayer added to two different views (I've tried add to the same view and the upper one get stuck once in a while), and switch alpha value between these two views.

iOS AVAsset.duration is zero for HTTP live streaming but works for progressive

I have an iOS app that plays video from a HTTP Live stream "playlist.m3u8" and I have a custom player created using AVPlayer. To handle normal user interactions such as scrubbing i need to get the duration of the video, but for some reason on iOS 4.3 using xcode 4.0 when I call the following code I get a CMTime that when converted to seconds gives a NaN -- I know what it's doing because CMTimeValue = 0 and CMTimeScale = 0 which gives the NaN and the CMTimeFlags = 17 which is even more strange.
Here's the code I uses which isn't complex at all:
AVPlayerItem *pItem = mPlayer.currentItem;
AVAsset* asset = pItem.asset;
CMTime d = asset.duration;
double duration = CMTimeGetSeconds(asset.duration);
I should also mention that I do monitor the status of the loading playlist to make sure it's ready before i start playing/scrubbing:
[mPlayer addObserver:self forKeyPath:#"currentItem.status" options:0 context:VideoPlaybackViewDelegateStatusContext];
Thanks for any help on this issues anyone could provide.
https://developer.apple.com/library/ios/releasenotes/AudioVideo/RN-AVFoundation-Old/#//apple_ref/doc/uid/TP40011199-CH1-SW4
The docs above mention that duration should now be obtained from the AVPlayerItem instance, rather than its corresponding AVAsset. To get the duration from the current player item via key-value observing, I use the following method (originally pulled from NGMoviePlayer which was written for iOS 4.0):
- (void)loadPlayerWithItem:(AVPlayerItem *)playerItem {
self.player = [AVPlayer playerWithPlayerItem:playerItem];
...
// changed this from previous value currentItem.asset.duration
[self.player addObserver:self forKeyPath:#"currentItem.duration"
options:0
context:nil];
...
}
I implemented the above change in my player and the duration is working now! This change in AVFoundation was the root cause of the issue. CMTimeFlags = 17 indicates kCMTimeFlags_Indefinite & kCMTimeFlags_Valid, and the docs specify:
In particular, the duration reported by the URL asset for streaming-based media is typically kCMTimeIndefinite, while the duration of a corresponding AVPlayerItem may be different and may change while it plays.

Resources