我试图在 iphone 上下载视频/音频并存储在 iPhone 系统捆绑包中,然后播放捆绑包中的视频/音频文件。
我已经使用 NSURLConnection 成功下载了视频/音频文件。我已将其存储为“sample.mov”
但我无法播放捆绑包中的视频。是因为该文件未在捆绑包中注册吗?!
这是代码:
下载完成后写入文件
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"sample.mov"];
[VideoData writeToFile:path atomically:YES];
}
然后我播放捆绑包中的视频
MoviePlayerAppDelegate *appDelegate = (MoviePlayerAppDelegate *)[[UIApplication sharedApplication] delegate];
// play the movie
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"a" ofType:@"mov"];
NSURL *movURL = [NSURL fileURLWithPath:moviePath];
[appDelegate initAndPlayMovie:movURL];
-(void)initAndPlayMovie:(NSURL *)movieURL
{
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[mp release];
// Apply the user specified settings to the movie player object
[self setMoviePlayerUserSettings];
// Play the movie!
[self.moviePlayer play];
}
}