2

サーバーにビデオがあります。それらのビデオをiPhoneとiPadで再生したいと思います。

そのような場合に対処する方法を誰かが提案できますか?

を使用して同じビデオがプロジェクトにある場合、ビデオを再生する方法を知っていますMPMoviePlayerController

4

2 に答える 2

4

WebView を使用してビデオを再生できます。

.h ファイルに次のコードを追加します。

#import <MediaPlayer/MediaPlayer.h>
    @interface VideoViewController : UIViewController<UIWebViewDelegate>
    @property (nonatomic, strong) UIWebView *playerView;

そして.mファイルで:

@synthesize playerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         self.playerView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 05, 300, 200)];
        [self.view addSubview:self.playerView];

    }
    return self;
}

-(void)viewDidLoad
{
    playerView.delegate = self;
    playerView.scrollView.scrollEnabled = NO;
    playerView.layer.cornerRadius = 5.0;
    playerView.clipsToBounds = YES;
}

そして playBtn アクションに次のコードを入れます:

NSString *yourVideoLink = your video link;
NSString *yourlinkThumbnail = your video thumbnaillink;
[self playVideo: yourVideoLink withWebView:playerView andThumbnailLink: yourlinkThumbnail];


#pragma mark - Webview Delegates

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //[SVProgressHUD showWithStatus:@"Loading..."];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //[SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
   // [SVProgressHUD dismiss];
}

#pragma mark - MoviePlayer Methods
- (void)playVideo:(NSString *)urlString withWebView:(UIWebView*)videoView andThumbnailLink:(NSString*)thumbnailImageLink {
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    <script>\
    function load(){document.getElementById(\"yt\").play();}\
    </script>\
    </head><body onload=\"load()\"style=\"margin:0\">\
    <video id=\"yt\" src=\"%@\" \
    width=\"%0.0f\" height=\"%0.0f\" poster=\"%@\" autoplay controls></video>\
    </body></html>";
    videoView.backgroundColor   =  [UIColor redColor];
    NSString *html = [NSString stringWithFormat:embedHTML, urlString, videoView.frame.size.width, videoView.frame.size.height,thumbnailImageLink];
    [videoView loadHTMLString:html baseURL:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
}

-(void)videoPlayStarted:(NSNotification *)notification{
    //self.isInFullScreenMode = YES;
}

-(void)videoPlayFinished:(NSNotification *)notification{
    // your code here
   // self.isInFullScreenMode = NO;
}
于 2013-02-04T07:32:15.820 に答える
0

UIWebView に HTML を埋め込むことで動画を再生できます。

    let height = UIScreen.main.bounds.size.height - 100
    let width = UIScreen.main.bounds.size.width
    let url: NSURL = NSURL(string: webURl)!
    let embedHTML = "<html><head><style type=\"text/css\">body {background-color: transparent;color: black;}</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes\"/></head><body style=\"margin:0\"><div><iframe src=\"\(webURl!)\" width=\"\(width)\" height=\"\(height)\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div></body></html>"
    webView.loadHTMLString(embedHTML as String, baseURL:url as URL )
    webView.contentMode = UIViewContentMode.scaleAspectFit
于 2017-09-28T12:41:47.830 に答える