【问题标题】:iphone trying to play video from a file but only get a black screeniphone试图从文件中播放视频但只得到黑屏
【发布时间】:2011-12-27 09:17:38
【问题描述】:

我正在尝试从服务器下载 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];

}   

【问题讨论】:

    标签: iphone


    【解决方案1】:

    这是你的固定代码:

    - (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];
            }
        }
    }
    

    1)

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

    2)

    文件扩展名 (.mp4) 提示电影播放器​​正在播放哪种文件。 “.dat”太笼统了。

    3)

    说到写入本地文件,不要放在主目录下。查找缓存目录或其他更合适的位置。

    【讨论】:

    • 您好,感谢您的帮助,我花了几天的时间试图让它工作。
    猜你喜欢
    • 1970-01-01
    • 2014-04-03
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多