search

Home  >  Q&A  >  body text

AVPlayer cannot play local video file

As mentioned, AVPlayer refuses to play local video files (videos are absolutely fine), but network loading links can be played. Could you please tell me what’s going on?

The code is as follows:

NSURL *url = [NSURL fileURLWithPath:@"/Users/tangkunyin/Desktop/mp4ForDownloadTest.mp4"];
AVAsset *movieAsset = [AVURLAsset assetWithURL:url];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];

AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = CGRectMake(0, 0, KDeviceWidth, 300);
playerLayer.videoGravity=AVLayerVideoGravityResize;

[self.view.layer addSublayer:playerLayer];

[player play];
習慣沉默習慣沉默2772 days ago934

reply all(3)I'll reply

  • 世界只因有你

    世界只因有你2017-05-02 09:27:44

    Hello, have you solved it? My problem is exactly the same as yours, the network can work but the local video cannot be played.

    reply
    0
  • 为情所困

    为情所困2017-05-02 09:27:44

    The URL of your local video is wrong. How is it possible to get the address on Mac in iOS?

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:27:44

    Local URL needs to start with: file:///, such as file:///var/mobile/Containers/Data/Application/6272EE22-EF19-48E5-85BA-4A986B22AC39/Documents/Movie4.mov.
    But I see that yours already starts with file, so you can pay attention to how the AVPlayer is created and try not to create it temporarily

    reply
    0
  • Cancelreply