Question

Im making a radio streaming application, my app is working perfectly. I have two buttons in my screen one for playing and the other for pausing, also I have a label that indicates the state of my player. I have no problem making this label show the state "Playing" or "Paused" my problem is that when I press the play button there is a time where the buffer is gathering information and I can't figure out a way to show a "Buffering..." label before the player start to stream the audio.

This is the code Im using for streaming the radio station.

 NSString *url = [NSString stringWithFormat:@"http://66.7.218:8816"];

        player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
        player.movieSourceType = MPMovieSourceTypeStreaming;
        player.view.hidden = YES;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance]setActive:YES error:nil];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [player prepareToPlay];

        [player play];

Im usin a function I created called changeStatus and this function is called each second to identify the status of the player.

-(void)changeStatus
{   

    if (player.loadState == MPMovieLoadStateUnknown) {
        status.text = @"Buffering...";
    }
    if(player.playbackState == MPMoviePlaybackStatePlaying)
        {
            status.text = @"Playing.";
        }
   if(player.playbackState == MPMoviePlaybackStatePaused)
        {
            status.text = @"Paused";
        }

}

I really need to solve this problem and Ive tried all I can to solve it. Hope you can help me! Thanks in advance.

Was it helpful?

Solution

I achieved to solve the problem. The part that was missing is the if clause which gives the signal to start playing only when the player is prepared to play. Without that the player starts to play before the buffer is completely loaded so it plays no audio. And that was the reason my Playing label showed immediately instead of the buffering one.

NSString *url = [NSString stringWithFormat:@"http://66.7.218:8816"];

        player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
        player.movieSourceType = MPMovieSourceTypeStreaming;
        player.view.hidden = YES;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance]setActive:YES error:nil];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

        [player prepareToPlay];

        if(player.isPreparedToPlay)
        {
        [player play];
        }

Adittionally, the MPMoviePlaybackStateInterrupted refers to the buffering process while doing an stream. So if you want to make something happens in the buffering process refer to this method.

-(void)changeStatus

    {   

        if(player.playbackState == MPMoviePlaybackStatePlaying)
            {
                status.text = @"Playing.";
            }
       if(player.playbackState == MPMoviePlaybackStateInterrupted)
            {
                status.text = @"Buffering...";
            }

    }

Thanks a lot to the user That Guy who helped me solving this.

OTHER TIPS

Actually there's a notification called

MPMediaPlaybackIsPreparedToPlayDidChangeNotification

The observer will get notified after it is ready to play. It is more or less the same effect but with different mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top