iOS - AVPlayer doesn't play mov file - ios

I have the following code which is not working with the mov file, but it works when i use an MP4 file.
The important thing about to mov file is because it has an alpha channel.
I'm open to find solutions that offers me an alpha channel as mov file does.
class MovViewController: UIViewController {
var avPlayer: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let filepath: String? = Bundle.main.path(forResource: "animacion_logo", ofType: "mov")
let fileURL = URL.init(fileURLWithPath: filepath!)
let player = AVPlayer(url: fileURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
// self.view.layer.addSublayer(playerLayer)
self.view.layer.insertSublayer(playerLayer, at: 0)
player.play()
}
}
This is the original mov file: https://ufile.io/jcbfn

AVPlayer do not support ProRes format with alpha:
Using AVPlayer to view transparent video
But there is some options out there. Take a look at this Medium post:
https://medium.com/#quentinfasquel/ios-transparent-video-with-coreimage-52cfb2544d54

You can play H.264 or H.265 (on newer iOS hardware) on iOS, but these formats do not support an alpha channel. There is no built in support for alpha channel, you will need to use a 3rd party library, the other option is a series of PNG images or decoding WebM, but both of these options will consume boat loads of CPU resources.

Related

Creating app that uses files in the files app on iOS

I am attempting to create an iOS application that takes various audio files from the Files app and plays them inside the app using the AudioKit framework. However, from what I have researched, AudioKit's file API appears to be incompatible with external URLs. Does anybody know any alternative music-based frameworks or methods, that can play audio files that are external URL based?
This is the another way. We can play audio with AVPlayer without download. No need to download the audio, with streaming we can play.
Check below code.
let url = URL(string:"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x:0, y:0, width:200, height:200)
self.view.layer.addSublayer(playerLayer)
player.volume = 1.0
player.play()

Playing HEVC(H.265) file using AVPlayer from a remote url

I am trying to play an HEVC(H.265) codec media url on AVPlayer. But the video is not showing. I didn't get any solution. How do I implement this in ios?
let videoURL = URL(string: "http://bitmovin-a.akamaihd.net/content/dataset/multi-codec/hevc/stream_fmp4.m3u8")
let avPlayer = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = avPlayer
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
Following your example I am able to play this content on a device (not simulator)
Pay attention also that I changed the URL from HTTP to HTTPS!
import UIKit
import AVFoundation
class ViewController: UIViewController {
let videoURL = URL(string: "https://bitmovin-a.akamaihd.net/content/dataset/multi-codec/hevc/stream_fmp4.m3u8")
let avPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let asset = AVAsset(url: videoURL!)
let playerItem = AVPlayerItem(asset: asset)
avPlayer.replaceCurrentItem(with: playerItem)
let avLayer = AVPlayerLayer(player: avPlayer)
avLayer.frame = view.bounds
view.layer.addSublayer(avLayer)
avPlayer.play()
}
}
NB:
The container format for HEVC video MUST be fMP4.
There are a few ways to deliver HEVC content to users:
HEVC in HLS using MPEG-2 Transport Stream chunks, which Apple doesn’t
support
HEVC in HLS using fMP4 segments, which is what Apple announced on
WWDC17 and our player supports
HEVC in MPEG-DASH using fMP4 segments
For reference:
WWDC17 – HEVC with HLS – Apple just announced a feature that we support out of the box
HLS Authoring Specification for Apple Devices

.avi file video not play in MPMoviePlayerController ios player

I'm using MPMoviePlayerController to play avi formatted video files.
The audio works, but the video doesn't. Why is that?
MPMoviePlayerViewController only supports .mov, .mp4,.mpv, and .3gp and is deprecated.
From Apple's documentation:
The MPMoviePlayerViewController class is formally deprecated in iOS 9.
(The MPMoviePlayerController class is also formally deprecated.) To
play video content in iOS 9 and later, instead use the
AVPictureInPictureController or AVPlayerViewController class from the
AVKit framework, or the WKWebView class from WebKit.
You should use AVPlayerViewController:
import AVFoundation
import AVKit
let videoPath = "https://video.avi"
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
playerViewController.player?.play()
}
or just add AVPlayer in as a subview:
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
view.layer.addSublayer(playerLayer)
player.play()
it supports the following audio/video formats:
.mpeg, .mpeg-2-video, .avi, .aifc-audio, .aac-audio, .mpeg-4, .au-audio, .aiff-audio, .mp2, .3gpp2, .ac3-audio, .mp3, .mpeg-2-transport-stream, .3gpp, .mpeg-4-audio
I think you cannot play .avi file using MPMoviePlayerController in ios.
As .avi format contents are not possible to decode the video on a iOS device.

Play several videos using AVPlayer in a view

I have an UIView which contains some some texts and views. in each view I need to play a video with AVPlayer , but my problem is only the first view shows the video :
Here is my code :
func playVideo(name:String , onView:UIView) {
let path = Bundle.main.path(forResource: name, ofType: "mp4")
let videoURL = URL(fileURLWithPath: path!)
let player = AVPlayer(url: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = onView.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
onView.layer.addSublayer(playerLayer)
player.play()
}
Using the function :
override func viewDidLoad() {
super.viewDidLoad()
//ScrollView
scrollView.contentSize = menuView.frame.size
scrollView.addSubview(menuView)
//Play videos
playVideo(name: "pizza", onView: videoView1)
playVideo(name: "salad", onView: videoView2)
playVideo(name: "fries", onView: videoView3)
}
when I run the app video only play in videoView1 , any suggestion why this happens ?
I also put my code in viewWillAppear , viewDidLayoutSubviews
According to Apple doc:
class AVPlayer
Note
AVPlayer is intended for playing a single media asset at a time.
I just found an alternative solution , I replaced UIView with UIContainerView and then link them with Embed to other view controller and play video in each view controller. Now it works perfectly as I expected.

Using local player in iOS to play a video from web

I am making a small website for video viewing as a project,
I need to be able to play a video using the local player of the phone, in Android i managed to find the solution:
intent://localhost/video.avi#Intent;action=android.intent.action.VIEW;scheme=http;type=video/mp4;end
This tells the Android OS to open this file using a media player, I need something like this for iOS.
I couldn't find anything on the internet about this, all other posts deal with making an actual application, I don't have access to an iPhone making this even harder to test and play around with it.
How can I do this for iPhone? if not possible are there alternatives?
Notice the code is a mere URL and needs absolutely no further implementations
There's URL Schemes provided by Apple to launch different native stuff via links.
try this: videos://"your video url here"
as an alternative: youtube://"youtube video url here"
About Apple iPhone URL schemes:
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899-CH1-SW1
Building an iOS application you can use AVFoundation framework, it has AVPlayer (samples are in Swift):
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
you can also use AVPlayerViewController class for it:
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let player = playerViewController.player {
player.play()
}
}
or you can use a UIWebView to play video from a player within a web page:
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: 240, height: 375))

Resources