Removing the QuickTime icon from AVPlayerViewController - ios

I am building a screen where users can play audio files using an AVPlayerViewController. The problem is that I can't get rid of the QuickTime logo in the player view, see screenshot:
My code:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
guard let url = URL(string: filePath!) else {
return
}
let player = AVPlayer(url: url)
let controller = AVPlayerViewController()
controller.modalPresentationStyle = .overFullScreen
controller.player = player
self.present(controller, animated: true) {
player.play()
}
} catch {
}
I've tried adding an UIImageView using controller.contentOverlayView?.addSubView(), but I can't center that properly. How do I customize the layout of the player without having to build my own interface from scratch?

What you tried is correct: just add a subview to the content overlay view. But you left out one step: you must give both the subview and the content overlay view constraints, to make them cover the player controller’s view completely.
Example (my av is your controller):
let iv = UIView()
iv.backgroundColor = .white
av.contentOverlayView!.addSubview(iv)
let v = av.contentOverlayView!
iv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
iv.bottomAnchor.constraint(equalTo:v.bottomAnchor),
iv.topAnchor.constraint(equalTo:v.topAnchor),
iv.leadingAnchor.constraint(equalTo:v.leadingAnchor),
iv.trailingAnchor.constraint(equalTo:v.trailingAnchor),
])
NSLayoutConstraint.activate([
v.bottomAnchor.constraint(equalTo:av.view.bottomAnchor),
v.topAnchor.constraint(equalTo:av.view.topAnchor),
v.leadingAnchor.constraint(equalTo:av.view.leadingAnchor),
v.trailingAnchor.constraint(equalTo:av.view.trailingAnchor),
])

If all you want to do is remove the "Q" logo without replacing it with anything, you can get rid of this way:
(avPlayerViewController?.view.subviews.first?.subviews.first as? UIImageView)?.image = nil
But, at least for me, the "Q" logo usually only appears about 2 seconds after I've set up the player view controller. So you might need to use a timer to get the above code to run about 2 seconds after creating the player.

Related

AVPlayerViewController ignores all touches after returning from fullscreen

I faced a very strange behaviour of AVPlayerViewController.
I use this code to add play video inside view:
class ViewShowTest:UIViewController
{
let view_for_media = UIView()
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = .red
self.view.addSubview(view_for_media)
view_for_media.snp.makeConstraints(
{ make in
make.top.equalToSuperview().offset(100)
make.left.right.equalToSuperview()
make.height.equalTo(300)
})
setVideo()
}
func setVideo()
{
let url = URL(string: "https://www.radiantmediaplayer.com/media/bbb-360p.mp4")!
let player = AVPlayer(url: url)
let player_vc = AVPlayerViewController(nibName: nil, bundle: nil)
player_vc.player = player
self.view_for_media.addSubview(player_vc.view)
self.addChild(player_vc)
player_vc.didMove(toParent: self)
player_vc.view.frame = view_for_media.bounds
}
}
Video is playing as expected inside view. But after going to fullScreen and returning back (no matter with swipe or close button) ViewShowTest ignores all touches. I still can drag default ios Menus from top and bottom but ViewShowTest view and player_vc.view does not reacts any touches. This bug appears only on ios 12.4 and below. How can i solve this problem?

Videos in welcome screen using AVPlayer causing memory leak?

In my app, there is a welcome screen consist of 3 screens. They all inherit from one super class. The different between them are just texts and video url:
|-SuperWelcomeScreenViewController
|----FirstWelcomeScreenViewController
|----SecondWelcomeScreenViewController
|----ThirdWelcomeScreenViewController
In the super class, I have a view to load view:
private final var player: AVPlayer = AVPlayer()
private final lazy var videoView: UIView = {
let v = UIView()
let videoString:String? = Bundle.main.path(forResource: self.videoPath, ofType: "mp4")
guard let unwrappedVideoPath = videoString else {return v}
let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)
let item = AVPlayerItem(url: videoUrl)
self.player.replaceCurrentItem(with: item)
let layer: AVPlayerLayer = AVPlayerLayer(player: player)
//Using the size of the video
layer.frame = CGRect(x: -125, y: 0, width: 250, height: 541)
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
v.layer.addSublayer(layer)
return v
}()
Here is the issue: I test the memory usage on Simulator, in the beginning it's about 250MB. Then I swiped to next welcome screen, it became 450MB. Then next for 550MB. My welcome screen has a infinite loop, but the usage stays at maximum after 3 viewcontrollers are all showed.
Then I went to the login screen, and then went back to welcome screen. The initial usage became 750MB. If I keep jumping from the login screen and the welcome screen, the usage will keep raising!
Here are what I want to do:
First, I want memory usage stay steady when I jumping from the login screen and the welcome screen.
Second, I want recycle memory between welcome screens. So that in every screen the usage would roughly be 250MB.
Here are what I have tried and failed:
Change player to static
Remove AVPlayerLayer from superLayer when view willDisappear
Set AVPlayer to nil
FYI I ran CFGetRetainCount in viewDidLoad and it's 5. I cannot find the other 4.
Actually doing things below together did the trick
Remove AVPlayerLayer from superLayer when view willDisappear
Set AVPlayer to nil

Embedding an AVPlayerViewController into a UIView

I am trying to play a video inside a UIView, but the video player always appears outside of the UIView which I am embedding the AVPlayerViewController into.
Below is what I have so far....
class ViewController: UIViewController {
let smallVideoPlayerViewController = AVPlayerViewController()
#IBOutlet weak var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let myFileManager = FileManager.default
let mainBundle = Bundle.main
let resourcesPath = mainBundle.resourcePath!
guard let allItemsInTheBundle = try? myFileManager.contentsOfDirectory(atPath: resourcesPath) else {
return
}
let videoName = "test"
let videoPath = Bundle.main.path(forResource: videoName, ofType: "mp4")
let videoUrl = URL(fileURLWithPath: videoPath!)
smallVideoPlayerViewController.showsPlaybackControls = false
smallVideoPlayerViewController.player = AVPlayer(url: videoUrl)
videoView.addSubview(smallVideoPlayerViewController.view)
smallVideoPlayerViewController.view.frame = videoView.frame
smallVideoPlayerViewController.player?.play()
}
...
}
The background colour of UIView is set to white. As seen from the screenshot, AVPlayer is outside of the UIView.
I tried manually setting the dimensions and position of the AVPlayer, but no luck with that either.
I am using Xcode 9.2. The project has not warnings regarding to any layout issues.
How can I perfectly align the AVPlayer, so that is would appear inside the UIView.
Thanks
Change this line:
smallVideoPlayerViewController.view.frame = videoView.frame
to this:
smallVideoPlayerViewController.view.frame = videoView.bounds
The reason is because videoView.frame includes the origin in its superview which you don't want. videoView.bounds is just the size with an origin of 0,0.
Of course you might want to go further set the auto-resizing mask to keep the video player frame the same like this:
smallVideoPlayerViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
or even to go full blown auto-layout.

Swift AVPlayerViewController Add AirPlay

I'm attempting to enable AirPlay with my AVPlayerViewController. In the document:
https://developer.apple.com/reference/avkit/avplayerviewcontroller
It states
AVPlayerViewController automatically supports AirPlay, but you need to perform some project and audio session configuration before it can be enabled in your application.
Under the Capabilities tab, I did enable the Background Mode for Audio, AirPlay, and Picture in Picture. I have created the AVPlayerViewController as follows:
// Create the view controller and player
let moviePlayerViewController: AVPlayerViewController = AVPlayerViewController()
let moviePlayer = AVPlayer(url: videoUrl!)
moviePlayer.allowsExternalPlayback = true
moviePlayer.usesExternalPlaybackWhileExternalScreenIsActive = true
// Initialize the AVPlayer
moviePlayerViewController.player = moviePlayer
// Present movie player and play when completion
self.present(moviePlayerViewController, animated: false, completion: {
moviePlayerViewController.player?.play()
})
I thought the two lines
moviePlayer.allowsExternalPlayback = true
moviePlayer.usesExternalPlaybackWhileExternalScreenIsActive = true
Would add the AirPlay support but I'm wrong. I have read that AirPlay can be used by adding MPVolumeView, but that's for a custom video controller, not the built in one. Any help would be greatly appreciated.
You should still be able to use MPVolumeView. Just use the following:
let volumeView = MPVolumeView()
self.view.addSubview(volumeView)
or if you just want the menu to show up, you can use:
let volumeView = MPVolumeView(frame: CGRect(x: -100, y: 0, width: 0, height: 0))
self.addSubview(volumeView)
for view: UIView in volumeView.subviews {
if let button = view as? UIButton {
button.sendActions(for: .touchUpInside)
volumeView.removeFromSuperview()
break
}
}
This will put it outside of the current view and then trigger the action that displays the air play menu.
Nowadays you should have no problem doing this.
(1) Under the Capabilities tab, enable Background Mode for Audio
(2) With your
var ezPlayer: AVPlayerViewController!
just
ezPlayer.showsPlaybackControls = true
ezPlayer.player = AVPlayer(url: ... )
ezPlayer.player!.allowsExternalPlayback = true
ezPlayer.player!.usesExternalPlaybackWhileExternalScreenIsActive =
and you should be fine. Build to a device. Assuming you are on a usable wifi network with an apple TV, apple laptops, the airplay icon will appear and work.
There's no need to use an MPVolumeView these days.

Set size for Video with AVPlayerViewController

I need to play a short video at the end of my game.
I created an AVPlayerViewController on my storyboard with a modal presentation using a segue from the previous View Controller.
However I want the video NOT to cover the whole screen. Let's say 50% (centered) of the screen size.
Here is the code I tried :
import AVKit
import AVFoundation
import UIKit
class VideoPlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
// Play video
player?.play()
}
override func viewDidLayoutSubviews() {
print(videoBounds)
self.view.bounds = videoBounds
}
}
The problem is that the video scales to the whole screen is very small (like something around 100*70px). I believe the default size is 100*100 at some point and it scales it to match my video ratio.
If I don't specify bounds, it scales to the whole screen (video gravity don't let me the choice).
If I reduce the size on my whole view in viewDidLayoutSubviews, it's still not okay because this method is called multiple times so I keep changing my view bounds.
I can't believe how hard it is to display a local video on part of the screen with AVPlayerViewController...
Any help ?
You can make the view controller any size you'd like. Like this, if you wanted to:
let videoURL = URL(fileURLWithPath: videoPath)
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = CGRect (x:100, y:100, width:200, height:100)
playerViewController.player = player
self.addChildViewController(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)
(obviously, the above size would be weird, but you can. if you want to).
Use the ViewController if you want automatically managed player controls. If you want to roll your own controls, the layer is probably better.
Just posting this to correct the answer that indicated you shouldn't use the VC except in full screen. Seems to work fine for me.
AVPlayerViewController is only meant to be used in a full screen manner, you are better off using AVPlayerLayer this allows you to size / place it in a view of your choosing.
let player = AVPlayer(url: myURL)
let layer = AVPlayerLayer(player: player)
layer.frame.size = CGSize(100,100)
layer.frame.origin = myView.center
player.play()

Resources