Reputation: 1695
I have a music player application that allow user to stream song from server. I've been able to stream song from url using AVPlayer. I want to achieve something like below:
I've tried to convert top answer from this How to get file size and current file size from NSURL for AVPlayer iOS4.0 post to swift, but I can't find the "kLoadedTimeRanges".
And here is my code:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if object as? NSObject == songPlayer && keyPath == "status" {
if songPlayer?.status == AVPlayerStatus.Failed {
println("AVPlayerStatusReadyToFailed")
}else if songPlayer?.status == AVPlayerStatus.ReadyToPlay {
println("AVPlayerStatusReadyToPlay")
self.songPlayer!.play()
}else if songPlayer?.status == AVPlayerStatus.Unknown {
println("AVPlayerStatusReadyToUnknown")
}
}
}
@IBAction func playAction(sender: UIButton) {
var songUrl = "http://a575.phobos.apple.com/us/r1000/119/Music/v4/67/9d/b0/679db0ba-a7f4-35ea-3bf1-e817e7bcf11f/mzaf_3227008615944938675.aac.m4a"
var songPlayerItem = self.songPlayer?.currentItem
self.songPlayer = AVPlayer(URL: NSURL(string: songUrl))
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.songPlayer?.currentItem)
self.songPlayer?.addObserver(self, forKeyPath: "status", options: nil, context: nil)
//NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateProgress:", userInfo: nil, repeats: true)
}
I'll appreciate any suggestion, doesn't matter using objective c or swift
Upvotes: 5
Views: 6889
Reputation: 5939
Objective C:
Note: I made a custom UISlider (Buffer Slider) where I added one UIProgressView to show the buffer progress.setBufferValue
method shown in the code below is a custom method which will set the UIProgressView
value.
static void *PlayerViewControllerLoadedTimeRangesObservationContext = &PlayerViewControllerLoadedTimeRangesObservationContext;
[self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:PlayerViewControllerLoadedTimeRangesObservationContext];
- (void)observeValueForKeyPath:(NSString*) path
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if (context == PlayerViewControllerLoadedTimeRangesObservationContext) {
NSArray *timeRanges = (NSArray*)[change objectForKey:NSKeyValueChangeNewKey];
if (timeRanges && [timeRanges count]) {
CMTimeRange timerange=[[timeRanges objectAtIndex:0]CMTimeRangeValue];
float bufferTimeValue=CMTimeGetSeconds(timerange.duration)/CMTimeGetSeconds(_player.currentItem.duration);
[self.playerSlider setBufferValue:bufferTimeValue>.95?1:bufferTimeValue animated:YES];
}
}
}
Upvotes: 2
Reputation: 1592
just use
[avPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
kLoadedTimeRanges is probably just defined as
static NSString *kLoadedTimeRanges = @"loadedTimeRanges";
Upvotes: 0