iphone试图播放文件中的视频,但只得到一个黑屏

iphone试图播放文件中的视频,但只得到一个黑屏,iphone,Iphone,我正在尝试从服务器下载mp4文件,然后将其保存到文件中。保存到文件中后,我尝试播放它。程序运行时,在下载文件时暂停,然后在创建MPMoviePlayerController对象时屏幕变黑。 我做了以下几件事。 1.跟踪代码并查看NSDAta对象,以确保加载了正确数量的数据。 2.添加了if[mFile fileExistsAtPath:filename]==true以确保文件存在。 3.检查所有返回值是否为零。 我现在没有理想了 //设置文件名以将其保存在下并播放 NSFileManager*m

我正在尝试从服务器下载mp4文件,然后将其保存到文件中。保存到文件中后,我尝试播放它。程序运行时,在下载文件时暂停,然后在创建MPMoviePlayerController对象时屏幕变黑。 我做了以下几件事。 1.跟踪代码并查看NSDAta对象,以确保加载了正确数量的数据。 2.添加了if[mFile fileExistsAtPath:filename]==true以确保文件存在。 3.检查所有返回值是否为零。 我现在没有理想了

//设置文件名以将其保存在下并播放 NSFileManager*mFile=[NSFileManager defaultManager]; NSString*filename=[NSHomeDirectory stringByAppendingPathComponent:@ted10.dat]

// load video
NSURL *movieUrl=[[NSURL alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/ted.mp4"];
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];  
[mFile createFileAtPath:filename contents: data attributes:nil];

// makse sure file exist


if ( [mFile fileExistsAtPath:filename]==true )
{
// play it
NSURL *fileUrl=[ NSURL fileURLWithPath:filename];
mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl];
[mp.view setFrame: self.view.bounds];
[self.view addSubview: mp.view];

if ([mp respondsToSelector:@selector(loadState)]) 

{
    // Set movie player layout
    [mp setControlStyle:MPMovieControlStyleFullscreen];
    [mp setFullscreen:YES];

    // May help to reduce latency
    [mp prepareToPlay];

    // Register that the load state changed (movie is ready)

}//localhost/Dino/golflix2/Classes/videolist.h  
else
{
}


[mp play];

}   

这是您的固定代码:

- (IBAction) doMovie: (id) sender
{
    // set up file name to save it under and play it 
    NSFileManager *mFile= [NSFileManager defaultManager]; 
    NSString *filename=[ NSHomeDirectory() stringByAppendingPathComponent:@"ted10.mp4"];

    // load video
    NSURL *movieUrl=[[NSURL alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/ted.mp4"];
    NSError *error = NULL;
    NSData *data = [NSData dataWithContentsOfURL:movieUrl];
    if( [data writeToFile:filename options: NSDataWritingAtomic error: &error] != YES)
    {
        NSLog( @"error writing data to file %@", filename );
    } else {
        // make sure file exist
        if ( [mFile fileExistsAtPath:filename]==true )
        {
            // play it
            NSURL *fileUrl=[ NSURL fileURLWithPath:filename];
            MPMoviePlayerController * mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl];
            [mp.view setFrame: self.view.bounds];
            [self.view addSubview: mp.view];

            // Set movie player layout
            [mp setControlStyle:MPMovieControlStyleFullscreen];
            [mp setFullscreen:YES];

            // May help to reduce latency
            [mp prepareToPlay];

            [mp play];
        }
    }
}
一,

最重要的是,您最初试图将电影写回远程URL。我认为您真正想要做的是写入本地文件或缓存

二,

文件扩展名.mp4向电影播放器提示正在播放的文件类型。dat太通用了

三,


说到写本地文件,不要把它放在主目录中。找到缓存目录或其他更合适的地方。

嗨,谢谢你的帮助,我已经花了好几天的时间试图让它工作。