2011-08-02 16 views
2

私はUIWebViewコントロールでdailymotionとyoutubeの動画を再生したいです。私は次の埋め込みHTMLコードを使ってプレイしていますが、うまくいきます。しかし、デバイスが回転しているので、それに応じてリセットされません。次のコードを再度呼び出すと、最初からビデオを再生し始めます。埋め込みビデオを複数の方向で再生しますか?

NSString* embedHTML = @"<html><head><style>body {\"background-color:transparent;color:black;\"}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\"type=\"application/x-shockwave-flash\"width=\"%.0f\" height=\"%.0f\"></embed></body></html>"; 
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height]; 

デバイスの回転に従ってビューをリセットし、再起動せずに再生を続ける解決策はありますか?

編集: 再生モードでは、ビデオの向きが完全に変更されています。一度それを停止してビューを回転しようとすると、webviewのコンテンツは回転しません。そして、私はその解決策を探しています。

答えて

0

私はこのためのチュートリアルを作成していました。また、インターフェイスの変更にも対応しています。

ここに実装ファイルがあります。ちょうどあなたのコントローラーに貼り付ける、あなたのURLを入れて、

#import "TestVideoViewController.h" 
#import "UICommonUtils.h" 

@implementation TestVideoViewController 
@synthesize TestVideoView; 


- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    //sleep(10.0); 
    [busyLoadingView hide:YES]; 
    //bShow = NO; 
} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    //sleep(10.0); 
    [busyLoadingView hide:YES]; 
    //bShow = NO; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    //bShow = YES; 
    //[busyLoadingView showWhileExecuting:@selector(showCursor:) onTarget:self withObject:nil animated:YES]; 

    //[busyLoadingView show:YES]; 
    return YES; 
} 

- (void)webViewDidStartLoad:(UIWebView *)webView 
{ 
    [busyLoadingView show:YES]; 
} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [self embedVideo1:@"PASS HERE URL1" andVideo2:@"PASS HERE URL2" frame:self.view.frame]; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
     [super viewDidLoad]; 

    CGRect rectFrame = [UIScreen mainScreen].applicationFrame; 

    self.view.frame = rectFrame; 
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    self.view.autoresizesSubviews = YES; 
    //TestVideoView.scalesPageToFit = YES; 
//TestVideoView.backgroundColor = [UIColor clearColor]; 

    busyLoadingView = [[MBProgressHUD alloc] initWithView:self.view]; 


    // Add HUD to screen 
    [self.view addSubview:busyLoadingView]; 

    busyLoadingView.labelText = @"Loading..."; 

    [self embedVideo1:@"PASS HERE URL1" andVideo2:@"PASS HERE URL2" frame:self.view.frame]; 

    //self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    //self.view.autoresizesSubviews = YES; 

} 

- (void)embedVideo1:(NSString*)url1 andVideo2:(NSString*)url2 frame:(CGRect)frame { 
     float videoWidth = 748.0; 
    float videoHeight = 341.0; 
    //int marginLeft = 10; 
    //int marginTop = 10; 

    if ([UICommonUtils isiPad]){ 
     videoWidth = 748.0; 
     videoHeight = 341.0; 
     //marginLeft = (int)(frame.size.width - videoWidth) /2; 
     //marginTop = 10; 
    } else { 
     videoWidth = 300.0; 
     videoHeight = 160.0; 
     //marginLeft = 10; 
     //marginTop = 10; 
    } 

    NSString* embedHTML = @" \ 
     <html><head> \ 
    <style type=\"text/css\"> \ 
    .style11 { \ 
    font-family:Arial,Helvetica,sans-serif; \ 
    font-weight:bold; \ 
    font-size:16; \ 
    } \ 
    .style12 { \ 
    width: \"%0.0f\"; \ 
    height=\"%0.0f\" \ 
    align = center; \ 
    } \ 
    .style21 { \ 
    font-family:Arial,Helvetica,sans-serif; \ 
    font-weight:bold; \ 
    font-size:16; \ 
    } \ 
    .style22 { \ 
    width: \"%0.0f\"; \ 
    height=\"%0.0f\" \ 
    align = center; \ 
    } \ 
    body {\ 
    background-color: transparent; \ 
    color: black; \ 
    } \ 
    </style> \ 
    </head><body> \ 
    <table> \ 
    <tr><td><span class=\"style11\">1) Collecting and Editing Leads</span></td></tr> \ 
    <tr><td height=20></td></tr> \ 
    <tr><td class=\"style12\"> \ 
    <embed id=\"yt1\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"> \ 
     </embed> \ 
    </td></tr> \ 
    <tr height=20></tr> \ 
    <tr><td><span class=\"style21\">2) Accessing Leads Online</span></td></tr> \ 
    <tr><td height=20></td></tr> \ 
    <tr><td class=\"style22\"> \ 
    <embed id=\"yt2\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"> \ 
    </embed> \ 
    </td></tr> \ 
    </table> \ 
     </body></html>"; 
    NSString* html = [NSString stringWithFormat:embedHTML, videoWidth, videoHeight, videoWidth, videoHeight, url1, videoWidth, videoHeight, url2, \ 
        videoWidth, videoHeight]; 
    //NSString* html = [NSString stringWithFormat:embedHTML, videoWidth, videoHeight, videoWidth, videoHeight, url1, videoWidth-20, videoHeight, url2, \ 
    //    videoWidth-20, videoHeight]; 
    /* 
    if(videoView == nil) { 
     videoView = [[UIWebView alloc] initWithFrame:frame]; 
     [self.view addSubview:videoView]; 
    } */ 
    //[TestVideoView loadHTMLString:html baseURL:nil]; 
    [(UIWebView *)self.view loadHTMLString:html baseURL:nil]; 
} 


// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    //return (interfaceOrientation == UIInterfaceOrientationPortrait); 
//return YES; 
    if([UICommonUtils isiPad]) return YES; 
    else return NO; 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    //CGRect rectFrame = [UIScreen mainScreen].applicationFrame; 

    //self.view.frame = rectFrame; 
    //NSLog([NSString stringWithFormat:@"origin x: %f origin y: %f width: %f height: %f", rectFrame.origin.x, 
    //  rectFrame.origin.y, rectFrame.size.width, rectFrame.size.height]); 

    //TestVideoView.frame = CGRectMake(rectFrame.origin.x, rectFrame.origin.y, rectFrame.size.height, rectFrame.size.width);  
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

このコードを試してみてくださいコピーここでのViewController

#import <UIKit/UIKit.h> 
#import "MBProgressHUD.h" 


@interface TestVideoViewController : UIViewController<UIWebViewDelegate>{ 
    UIWebView *TestVideoView; 
    MBProgressHUD *busyLoadingView; 
    BOOL bShow; 
} 

@property (nonatomic, retain) IBOutlet UIWebView *TestVideoView; 

- (void)embedVideo1:(NSString*)url1 andVideo2:(NSString*)url2 frame:(CGRect)frame; 

@end 

のヘッダファイル・ホープこれはあなたを助けです。

さらなるヘルプが必要な場合は、教えてください。

EDIT: このコードはおそらくシミュレータとしてシミュレータ上で適切に仕事を文句を言わないshockwave-フラッシュプレーヤーを持っているdoesntの

+0

コンパイラが#importをするために、エラーを示してい"UICommonUtils.h"。 #import "UICommonUtils.h"が欠落しています。親切にクラスのリファレンスを教えてください。 –

+0

いいえデバイスがiphoneかipadかどうかを確認するのは私のカスタムクラスでした。あなたが持っている場合は、今のipadデバイスで再生することを検討することができますまたはその場合は、デバイスタイプを確認する必要はありませんので、iphoneデバイス。したがって、条件チェックを削除してUICommonUtilsクラスへの参照を取り除くことができます。 –

+0

私の答えの編集セクションもチェックしてください。あなたのUICommonUtilsクラスエラーとは何も関係ありませんが、そこに重要な点を追加しました。 –

関連する問題