OStack程序员社区-中国程序员成长平台

标题: ios - 如何播放服务器上的在线视频 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:49
标题: ios - 如何播放服务器上的在线视频

我的服务器上有视频。我想在 iPhone 和 iPad 上播放这些视频。

谁能建议如何处理这种情况?

我知道如何在我的项目中使用 MPMoviePlayerController 播放视频。



Best Answer-推荐答案


您可以使用 WebView 播放视频。

在 .h 文件中添加以下代码:

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

在 .m 文件中:

@synthesize playerView;

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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)webViewDidStartLoadUIWebView *)webView
{
    //[SVProgressHUD showWithStatus"Loading..."];
}
- (void)webViewDidFinishLoadUIWebView *)webView
{
    //[SVProgressHUD dismiss];
}
- (void)webViewUIWebView *)webView didFailLoadWithErrorNSError *)error
{
   // [SVProgressHUD dismiss];
}

#pragma mark - MoviePlayer Methods
- (void)playVideoNSString *)urlString withWebViewUIWebView*)videoView andThumbnailLinkNSString*)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 selectorselector(videoPlayStarted name"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selectorselector(videoPlayFinished name"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
}

-(void)videoPlayStartedNSNotification *)notification{
    //self.isInFullScreenMode = YES;
}

-(void)videoPlayFinished:(NSNotification *)notification{
    // your code here
   // self.isInFullScreenMode = NO;
}

关于ios - 如何播放服务器上的在线视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14681618/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4