首页 文章

无法在MPMoviePlayer中播放视频?

提问于
浏览
0
NSURL *url =[[NSURL alloc]initWithString:@"http://www.youtube.com/watch?v=Jeh40KFFS5Y"];
  MPMoviePlayerController *player1 = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [player1 setContentURL:url];
        [player1 setMovieSourceType:MPMovieSourceTypeFile];
        [[player1 view] setFrame:self.view.bounds];
        player1.scalingMode = MPMovieScalingModeNone;
        player1.repeatMode = MPMovieRepeatModeNone;
        [self.view addSubview: [player1 view]];
        [player1 play];

我在MPMoviePlayer中播放网址时收到以下错误:

HTTP(http://)资源加载,因为它不安全 . 可以通过应用程序的Info.plist文件配置临时例外 . 2018-06-06 11:15:31.891375 0530 vedio [1866:137968]任务 . <1>完成错误 - 代码:-1022 2018-06-06 11:15:31.891439 0530 vedio [1866:138011]任务 . < 1>完成错误 - 代码:-1022 2018-06-06 11:15:32.269777 0530 vedio [1866:137470] [回放]用于解决错误错误Domain = AVFoundationErrorDomain Code = -11800“操作无法完成” UserInfo = {NSLocalizedFailureReason =发生未知错误(-1022),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x600000449e70 {错误域= NSOSStatusErrorDomain代码= -1022“(null)”}} 2018-06-06 11:15 :32.271275 0530 vedio [1866:137470] [回放]❗️Resolutionfor item无法解决错误:错误Domain = AVFoundationErrorDomain Code = -11800“操作无法完成”UserInfo = {NSLocalizedFailureReason =发生未知错误(-1022) ,NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x600000449e70 {错误域= NSOSStatusErrorDomain代码= -1022“(n ull)“}}与分辨率错误:(null)2018-06-06 11:15:32.271423 0530 vedio [1866:137470] [回放]❗️Playback失败并显示错误:错误Domain = AVFoundationErrorDomain Code = -11800”操作可能未完成“UserInfo = {NSLocalizedFailureReason =发生未知错误(-1022),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x600000449e70 {错误域= NSOSStatusErrorDomain代码= -1022”(null)“}},未解析( canResolve:NO,allowsItemErrorResolution:NO)2018-06-06 11:15:32.301954 0530 vedio [1866:137470] [回放]❗️无法排队任何项目 .

4 回答

  • 0

    我用Youtube播放了一段视频
    pod "youtube-ios-player-helper","~> 0.1.4" it works fine.

    安装此pod后,

    import“YTPlayerView.h”

    • 拖动UIView并将自定义类更改为“YTPlayerView”

    • @property(弱,非原子)IBOutlet YTPlayerView * PlayerView; //创建出口

    在viewDidload中,您可以编写单行代码来播放视频

    • [self.PlayerView loadWithVideoId:@ "UQxxt6R5VtQ"]; //你可以通过url获取video_id frm .
  • 0

    您未启用应用传输安全协议 . 所以这个代码就足够了 . 打开带有源代码的info.plist文件并粘贴我的代码 .

    <key>NSAppTransportSecurity</key>
    <dict>
         <key>NSAllowsArbitraryLoads</key>
    <true/>
    </dict>
    
  • 0

    您的视频无法在您需要的MPMoviePlayerController中播放

    一种解决方案正在使用

    pod 'YouTubePlayer'
    

    使用pod你需要提供youtubeplayerview和东西,但一些所有者禁用在另一个应用程序中播放

    所以你需要解决方案,比如youtube是否在youtube中播放,否则在浏览器中打开

    func playInYoutube(youtubeURL: String) {
            if let youtubeURL = URL(string: youtubeURL),
                UIApplication.shared.canOpenURL(youtubeURL) {
                // redirect to app
                UIApplication.shared.open(youtubeURL, options: [:], completionHandler: nil)
            } else if let youtubeURL = URL(string: youtubeURL) {
                // redirect through safari
                UIApplication.shared.open(youtubeURL, options: [:], completionHandler: nil)
            }
        }
    
  • 0

    您需要在info.plist中添加以下内容,因为您使用的是不安全的连接(http://) .

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSExceptionDomains</key>
       <dict>
        <key>youtube.com</key>
        <dict>
          <!--Include to allow subdomains-->
          <key>NSIncludesSubdomains</key>
          <true/>
          <!--Include to allow HTTP requests-->
          <key>NSExceptionAllowsInsecureHTTPLoads</key>
          <true/>
        </dict>
      </dict>
    </dict>
    

    或者你可以使用懒惰的方法 .

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

    enter image description here

    然后尝试玩viedo .

    如果问题仍然存在,请尝试使用 https:// 而不是 http:// 播放视频 .

    Using AVPlayer

    NSURL *videoURL = [NSURL URLWithString:@"https://www.youtube.com/watch?v=Jeh40KFFS5Y"];
    AVAsset *avAsset = [AVAsset assetWithURL:videoURL];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:avAsset];
    AVPlayer * avPlayer = [AVPlayer playerWithPlayerItem:item];
    
    AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc] initWithLayer:avPlayer];
    playerLayer.frame = self.view.frame;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [self.view.layer addSublayer:playerLayer];
    

相关问题