如何在 iPhone 中播放视频

发布于 2024-10-12 12:16:12 字数 190 浏览 2 评论 0原文

MPMoviePlayerController 中的 initWithContentURL: 是否首先下载视频文件然后开始播放,还是会像 youtube 中那样流式传输文件?

我想通过流媒体播放位于服务器中的视频,以免浪费时间。

如何在 iPhone 中进行流媒体播放? 如果你知道的话,有人可以建议我吗?

提前致谢。

does initWithContentURL: in MPMoviePlayerController download the video file at first and then start playing or will it stream file like in youtube?

I want to play an video located in server through streaming in order not to waste of time.

How to perform streaming in iphone?
Can anyone please suggest me if u know.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-10-19 12:16:12
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {
    NSLog(@"Documents directory not found!");
}
NSString *appFile;
NSArray *myWords = [[NSString stringWithFormat:@"%@",[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"]] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];
NSFileManager *fileMgr=[NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:appFile]) {
    NSData *imageData;
    NSURL *imageURL = [[[NSURL alloc] initWithString:[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"] ] autorelease];
    if (imageURL) {
        imageData = [NSData dataWithContentsOfURL:imageURL];
    }

    [imageData writeToFile:appFile atomically:YES];
}
[pool release];


if (spinner) {
    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
    spinner = nil;
}

---------------------------以上方法用于将视频保存到iPhone的文件系统中
-------------------- 以下方法可以连续播放该电影

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {

    NSLog(@"Documents directory not found!");

}

NSArray *myWords = [videoString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];


NSString *appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

NSLog(@"%@",[myWords lastObject]);

NSURL *url = [NSURL fileURLWithPath:appFile];

self.videoMPPlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];

    self.videoMPPlayer.view.frame = CGRectMake(0, 0, 320, 480);

    self.videoMPPlayer.scalingMode = MPMovieScalingModeAspectFill;

    self.videoMPPlayer.controlStyle = MPMovieControlStyleNone;


    // Register for the playback finished notification

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(movieLoadStateChanges:) name:MPMoviePlayerLoadStateDidChangeNotification object:videoMPPlayer];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoMPPlayer];


// Movie playback is asynchronous, so this method returns immediately.
[self.videoMPPlayer play];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {
    NSLog(@"Documents directory not found!");
}
NSString *appFile;
NSArray *myWords = [[NSString stringWithFormat:@"%@",[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"]] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];
NSFileManager *fileMgr=[NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:appFile]) {
    NSData *imageData;
    NSURL *imageURL = [[[NSURL alloc] initWithString:[[videolistArray objectAtIndex:[number intValue]] valueForKey:@"video"] ] autorelease];
    if (imageURL) {
        imageData = [NSData dataWithContentsOfURL:imageURL];
    }

    [imageData writeToFile:appFile atomically:YES];
}
[pool release];


if (spinner) {
    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
    spinner = nil;
}

---------------------------Above methode is for save video in file system of iphone
-------------------- Below method to play that movie continuos

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {

    NSLog(@"Documents directory not found!");

}

NSArray *myWords = [videoString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];


NSString *appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

NSLog(@"%@",[myWords lastObject]);

NSURL *url = [NSURL fileURLWithPath:appFile];

self.videoMPPlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];

    self.videoMPPlayer.view.frame = CGRectMake(0, 0, 320, 480);

    self.videoMPPlayer.scalingMode = MPMovieScalingModeAspectFill;

    self.videoMPPlayer.controlStyle = MPMovieControlStyleNone;


    // Register for the playback finished notification

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(movieLoadStateChanges:) name:MPMoviePlayerLoadStateDidChangeNotification object:videoMPPlayer];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoMPPlayer];


// Movie playback is asynchronous, so this method returns immediately.
[self.videoMPPlayer play];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文