playableDuration returns 0 in iOS5

2019-08-02 08:18发布

问题:

Has anyone else noticed that playableDuration property of MPMoviePlayerController class always returns 0 in iOS 5. This used to work fine in previous versions of iOS. I use it to set the value of a progress bar.

Here is piece of code that used to work under 4.x SDK just fine (i.e., the playableDuration attribute returned the correct non-zero value while buffering the stream), but under SDK 5.x it always returns zero.

- (void) updateMeter {
NSLog(@"playableDuration = %f", streamPlayer.playableDuration);
}

- (void)viewDidLoad
{

[super viewDidLoad];
streamPlayer = [[MPMoviePlayerController alloc] 
initWithContentURL:[NSURL    URLWithString:@"http://99.198.118.250:8158/"]];    

NSTimer *updateBarTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 
                          target:self selector:@selector(updateMeter)    
                          userInfo:nil repeats:YES];

streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
[streamPlayer play];

 }

回答1:

Use your exact code but replace the url with: http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8

For me your code failed unless I pointed the player to a segmented .m3u8 file.

I did some tests with a .mp4 movie and an .mp3 audio file locally on my computer and both worked fine as well.

I'm speculating here but I believe it's probable that while streaming media, the MPMoviePlayerController is using the .m3u8 file to deduce player item data on the fly? That's my guess anyway. What's curious is that if this is the case, why does it work at all for your url? Which leads me to my next comment...

You would probably have better results using AVFoundation rather than the MediaPlayer framework. I switched to it in my own work as well. It's less "prepackaged" but simply provides much more control.