gpt4 book ai didi

url - iOS 如何通过 URL 播放视频

转载 作者:行者123 更新时间:2023-11-28 21:39:45 26 4
gpt4 key购买 nike

我想通过 URL 播放视频。我看到了一些示例,代码如下:

NSString *movieFile= [[NSBundle mainBundle] pathForResource:@"android" ofType:@"mp4"];
videoURL=[[NSURL alloc] initFileURLWithPath:movieFile];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];

它只播放本地资源。我写了一些代码:

NSString* strurl =@"https://s3.amazonaws.com/adplayer/colgate.mp4";
videoURL=[NSURL fileURLWithPath:strurl];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];

但是什么都没有...为什么...以及如何通过 url 播放视频?

最佳答案

在下面的代码中,我通过 Internet 播放位于 Web 服务器上的电影文件中的视频。不要忘记添加 MediaPlayer 框架并在 ViewController.h 文件中包含“MediaPlayer/MediaPlayer.h”。

点击按钮使用以下代码:

    -(IBAction) playVideo:(id)sender
{

NSURL *url=[[NSURL alloc] initWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}

通知方式:

    - (void) moviePlayBackDidFinish:(NSNotification*)notification 
{

MPMoviePlayerController *player = [notification object];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:player];

if ([player respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}

关于url - iOS 如何通过 URL 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com