0

我有这个函数可以将 NSData 写入文档字典 -

- (void)writeData
{
    NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
    NSLog(@"%@",path);
    NSLog(@"About to Write Data");
    NSData* data0 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
    [data0 writeToFile:path atomically:YES];
    NSLog(@"Writting  Data Completed");
}

后来我阅读了这些数据并尝试使用 MPMoviePlayerController 播放视频,但不幸的是这不会播放 -

- (void)readData
{
    NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
    NSURL* url = [[NSURL alloc] initFileURLWithPath:path];    
    NSLog(@"url:%@",url);

    NSError *error = nil;
    BOOL isReacahble = [url checkResourceIsReachableAndReturnError:&error];
    NSLog(@"isReachable :%i,%@",isReacahble,error);

    BOOL fileExist =  [[NSFileManager defaultManager] fileExistsAtPath:path];

    if(fileExist)
    {
        NSLog(@"File Exist in path");
    }
    else
    {
        NSLog(@"NO File Exist");
    }

    MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
    [moviePlayer prepareToPlay];
    NSLog(@"movie player:%@",moviePlayer);
    [moviePlayer setControlStyle: MPMovieControlStyleFullscreen];
    [moviePlayer.view setFrame:CGRectMake(0, 0,320, 480)];
    [self.view addSubview:moviePlayer.view];
    moviePlayer.shouldAutoplay = YES;   
    [moviePlayer play];
}

当我尝试播放时,它显示以下错误-

2013-01-17 11:21:07.067 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-17 11:21:07.068 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay
2013-01-17 11:21:07.090 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.091 VideoCheck[338:907] movie player:<MPMoviePlayerController: 0x21078250>
2013-01-17 11:21:07.093 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.102 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.113 VideoCheck[338:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-17 11:21:07.114 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-17 11:21:07.115 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.121 VideoCheck[338:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-17 11:21:07.149 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay

有人可以运行代码并提供一些关于这里发生的事情的信息吗?请有人帮助我。我正在使用 iOS 6。

4

1 回答 1

0

在您的wrieData方法中,不要将URL名称作为文件提供,而是仅提供video. 像

NSString *path = [documentsDirectory stringByAppendingPathComponent:[str lastPathComponent]];

并在readData方法上做同样的事情。

于 2013-01-17T06:12:32.763 回答