Play video from url in lens c with buffering

Hi everyone, I am working on an application in which I have a video URL and I need to play the video from that URL. I did this work from this code

- (IBAction)btnPlayVideo:(id)sender
{
    NSString *fileName = @"Server Address/Vdieo.flv";

    NSURL *streamURL = [NSURL URLWithString:fileName];

    mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];


    [self.view addSubview:mPlayerVC.view];

    //play movie

    MPMoviePlayerController *player = [mPlayerVC moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    player.view.frame = self.view.frame;
    [player setFullscreen:YES animated:YES];
    [self.view addSubview:player.view];
    [player prepareToPlay];
    [player play];

}

// ============ Other methods =====================

- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");
    [mPlayerVC.view removeFromSuperview];
    mPlayerVC = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            NSLog(@"exitedFullscreen");

            [[NSNotificationCenter defaultCenter] removeObserver:self];
            break;
        default:
            break;
    }

    [mPlayerVC.view removeFromSuperview];
    mPlayerVC = nil;

}

My problem is that when this code starts the video player and starts loading, it takes too much time to start the video. Can someone teach me how to quickly play videos from the internet?

+4
source share
1 answer

, , - , , . UE - , , .

, :

// hang on to the movie player
@property(nonatomic,retain) MPMoviePlayerController *mp;

// call this as soon as its possible to know the user might want to see the video
- (void)primeVideo {
    NSString *fileName = @"Server Address/Vdieo.flv";
    NSURL *streamURL = [NSURL URLWithString:fileName];

    MPMoviePlayerController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];

    // do this also in dealloc
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    mp.shouldAutoplay = NO;
    [mp prepareToPlay];
    self.mp = mp;
} 

. , ...

- (IBAction)btnPlayVideo:(id)sender {

    // if there any way that the user can request playback before
    // you've called primeVideo, check for that here.  But hopefully you
    // can call primeVideo before user even sees the play button
    if (!self.mp) [self primeVideo];

    self.mp.view.frame = self.view.frame;
    [self.mp setFullscreen:YES animated:YES];
    [self.view addSubview:self.mp.view];

    MPMovieLoadState state = [self.mp loadState];
    if (state & MPMovieLoadStatePlaythroughOK) [self.mp play];
    else self.mp.shouldAutoplay = YES;
}
0

All Articles