本地视频播放-ios

// 视频播放

-(void)playMovie

{

    self.navigationItem.rightBarButtonItem.enabled = YES;

    

    self.movieName = @"1305178253912757X4VMU00207002";

    self.folderName = @"1-11";

    //在VideoFolder文件夹下建立子文件夹

    NSFileManager *fm = [[NSFileManager alloc]init];

    NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

    NSString *folderPath = [docs stringByAppendingPathComponent:[NSString stringWithFormat:@"VideoFolder/%@/%@.mp4",self.folderName,self.movieName]];

    BOOL exists = [fm fileExistsAtPath:folderPath];

    if (exists) {

        NSURL *urls = [NSURL fileURLWithPath:folderPath];

        movie = [[MPMoviePlayerController alloc]initWithContentURL:urls];

        movie.controlStyle = MPMovieControlStyleDefault;

        [movie  prepareToPlay];

        [movie.view setFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height-60)];

        movie.movieSourceType = MPMovieSourceTypeFile;

        movie.initialPlaybackTime = -1;

        [self.view addSubview:movie.view];

        //注册一个播放结束的通知

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie];

        [movie play];

    }

    [fm release];

    

}

//当视频播放结束的时候关闭视频

-(void)movieFinishedCallback:(NSNotification*)notify

{

    //视频播放对象

    MPMoviePlayerController* theMovie = [notify object];

    //销毁播放通知

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:MPMoviePlayerPlaybackDidFinishNotification

                                                  object:theMovie];

    [theMovie.view removeFromSuperview];

    [movie.view removeFromSuperview];

    // 释放视频对象

    //    [movie release];

    [theMovie release];

}

//手动关闭视频

-(void)closeMovie

{

    self.navigationItem.rightBarButtonItem.enabled = NO;

    

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:MPMoviePlayerPlaybackDidFinishNotification

                                                  object:movie];

    [movie.view removeFromSuperview];

}

 

你可能感兴趣的:(ios)