shiwani
shiwani

Reputation: 31

Unable to play the video in MPMoviePlayer?

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];

I am getting the below error while playing the url in MPMoviePlayer:

HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

2018-06-06 11:15:31.891375+0530 vedio[1866:137968] Task .<1> finished with error - code: -1022 2018-06-06 11:15:31.891439+0530 vedio[1866:138011] Task .<1> finished with error - code: -1022 2018-06-06 11:15:32.269777+0530 vedio[1866:137470] [Playback] Using to resolve error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-1022), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x600000449e70 {Error Domain=NSOSStatusErrorDomain Code=-1022 "(null)"}} 2018-06-06 11:15:32.271275+0530 vedio[1866:137470] [Playback] ❗️Resolution for item could not resolve error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-1022), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x600000449e70 {Error Domain=NSOSStatusErrorDomain Code=-1022 "(null)"}} with resolution error: (null) 2018-06-06 11:15:32.271423+0530 vedio[1866:137470] [Playback] ❗️Playback failed with error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-1022), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x600000449e70 {Error Domain=NSOSStatusErrorDomain Code=-1022 "(null)"}}, not resolving (canResolve: NO, allowsItemErrorResolution: NO) 2018-06-06 11:15:32.301954+0530 vedio[1866:137470] [Playback] ❗️Failed to queue any items.

Upvotes: 0

Views: 318

Answers (4)

Vinu Jacob
Vinu Jacob

Reputation: 381

I played a video from Youtube by using
pod "youtube-ios-player-helper", "~> 0.1.4" it works fine.

After you will instal this pod,

import "YTPlayerView.h"

  1. drag a UIView and change the custom class to "YTPlayerView"

  2. @property (weak, nonatomic) IBOutlet YTPlayerView *PlayerView; // create outlet

In viewDidload you can write the singlr line code for play the video

  1. [self.PlayerView loadWithVideoId:@"UQxxt6R5VtQ"]; // you can get the video_id frm the url.

Upvotes: 0

Mahendra
Mahendra

Reputation: 8904

You need to add the following in info.plist since you are using insecure connection(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>

or you can use lazy method.

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

enter image description here

And then try to play viedo.

If problem remains then try to play video with https:// instead of 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];

Upvotes: 0

Hardik Bar
Hardik Bar

Reputation: 1760

your video will not play in MPMoviePlayerController you need

one solution is using

pod 'YouTubePlayer'

using pod you need to give youtubeplayerview and thing but some owner disable to play in another app

so you need to solution like if youtube is there play in youtube else open in browser

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)
        }
    }

Upvotes: 0

Naresh
Naresh

Reputation: 17882

You are not enabled app transport security protocol. So simply this code is enough for you. Open info.plist file with source code and paste my code.

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

Upvotes: 0

Related Questions