2

I want to play mp3 files from my Server, i do the following:

NSURL *url = [NSURL URLWithString:item.audioUrl];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

self.player = [AVPlayer playerWithPlayerItem:playerItem];

self.player addObserver:self forKeyPath:@"status" options:0 context:nil];

self.hud = [[MBProgressHUD alloc]initWithFrame:CGRectMake(150, 5, 50, 50)];     
self.hud.mode = MBProgressHUDModeIndeterminate;
[self.hud show:YES];
[self.containerView addSubview:self.hud];

Here is my observeValueForKeyPath:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if (object == self.player && [keyPath isEqualToString:@"status"])
{
    if (self.player.status == AVPlayerStatusReadyToPlay)
    {
        [self.hud hide:YES];
        [self.player play];
.
.
.

Now the Problem is that even when the Status of the AVPlayer have changed to "readyToPlay" it still takes about 2-3 seconds before it starts playing and of course my loading Spinner gets hidden before the music starts....any Ideas?

Davis
  • 1,253
  • 4
  • 17
  • 38
  • Have you checked that the track doesn't start with those seconds silenced? If so, I wonder that the problem could be related with GCD. Try to put the [self.player play] inside this: dispatch_async(dispatch_get_main_queue(), ^{[self.player play]}); – jomafer Aug 13 '14 at 14:26
  • Thanks for your quick response, i checked the track and it starts immediately, i also added your piece of code, but sadly the delay is still there... – Davis Aug 13 '14 at 14:34
  • 1
    @Davis Did you ever solve this? – Raymond26 Jun 27 '16 at 21:19

2 Answers2

1

try instead the method [self.player addObserver:self forKeyPath:@"status" options:0 context:nil]; by [self.playerItem addObserver:self forKeyPath:@"status" options:0 context:nil]

DuncanLee
  • 11
  • 1
0
self.player.automaticallyWaitsToMinimizeStalling = NO;

Set to NO to play audio immediately after AVPlayerStatusReadyToPlay status!

Tà Truhoada
  • 584
  • 7
  • 23