iphone - 如何在 iPhone 中播放 URL 中的视频

标签 iphone objective-c iphone-sdk-3.0

我在 url 中有一个视频,在单击按钮时我必须播放视频,为此我使用 UIWebView 来播放该视频,为此我使用了以下代码:

-(void)viewDidLoad
{
   NSString *string = @"http://www.boxmusiq.com/ThiruvasakamVideo/VTS_01_2_converted.mp4";
   youtubeview = [[YouTubeView alloc] initWithStringAsURL:string frame:CGRectMake(0, 0, 320, 480)];
   NSLog(@"the url is: %@",string);
   [self.view addSubview:youtubeview];
}

以及 YouTubeView.m 中 YouTubeView 是 UIWebView 的子类;

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
 if (self = [super init]) 
 {
               // Create webview with requested frame size
   self = [[UIWebView alloc] initWithFrame:frame];

               // HTML to embed YouTube video
   NSString *youTubeVideoHTML = @"<html><head>\
                         <body style=\"margin:0\">\
                         <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
                         width=\"%0.0f\" height=\"%0.0f\"></embed>\
                         </body></html>";

   // Populate HTML with the URL and requested frame size
   NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];

   // Load the html into the webview
   [self loadHTMLString:html baseURL:nil];
       }

 return self;  

}

最佳答案

试试下面的代码

MPMoviePlayerController *theMoviPlayer;
NSURL *urlString=[NSURL URLWithString:@"http://www.boxmusiq.com/ThiruvasakamVideo/VTS_01_2_converted.mp4"];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
theMoviPlayer.scalingMode = MPMovieScalingModeFill;
theMoviPlayer.view.frame = CGRectMake(0, 60, 320, 350);
[self.view addSubview:theMoviPlayer.view];
[self.theMoviPlayer play];

注意:添加 Mediaplayer FrameKit 并将其导入到您的类中

关于iphone - 如何在 iPhone 中播放 URL 中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401531/

相关文章:

iphone - NSArray 内存泄漏!无法获取为什么?

iOS:Autolayout 在 iPhone 中未显示正确的尺寸

ios - 即使对于有效的 indexPath,在 reloadRowsAtIndexPaths 之后并不总是调用 cellForRowAtIndexPath

iphone - 我想跟踪手指移动的速度(以像素每秒为单位)有可能吗?

ios - 在 Swift 中设置呈现模态的自定义大小失败——占用全屏

ios - UIScrollView 跨设备 UI 兼容性

iphone - FB连接 : Facebook authent screen not redirecting to my app

ios - 在 iOS 7 中使用 applicationDidEnterBackground : 应用程序进入后台时无法更新 View

iphone - iOS : UITableView reloadData not working

objective-c - NSHTTPURLResponse statusCode 在应该为 401 时返回零