Picture-in-picture, which was already introduced on the iPad, is now compatible with iOS. I will describe the flow of introduction to the application from now on.
By introducing picture-in-picture, video playback will be possible on other apps.
In addition, the application supports picture-in-picture, If autoplay is enabled, it will start when you press the home button.
If your application supports picture-in-picture There is a behavior setting in the settings app when you press the home button while watching.
I don't think the average iPhone user is aware of it. It is added to the place where you dive about 2 layers of the setting application.
item | iOS14 | iPadOS13 |
---|---|---|
hierarchy | Settings → General → Picture in Picture | Settings → Home screen and Dock → Multitasking |
item name | Play picture-in-picture automatically | Picture in picture |
Explanatory text | When turned on, the overlay will continue to play the video even when you press the home button. | the same |
Next, I investigated how the behavior changes depending on whether the picture-in-picture autoplay setting is On / Off. I also checked if there was any other wire that would start picture-in-picture.
Behavior of TV application (genuine apple) </ b> https://apps.apple.com/jp/app/apple-tv/id1174078549
operation | Autoplay ON | Autoplay OFF |
---|---|---|
Press the home button | Transition to the home screen and start picture-in-picture | Transitions to the home screen, playback stops, and audio stops * 1 |
Press the picture-in-picture button | Transition to the home screen and start picture-in-picture | Transition to the home screen and start picture-in-picture |
Swipe down on the playback screen | Viewing stops | Viewing stops |
Twitch behavior </ b>
operation | Autoplay ON | Autoplay OFF |
---|---|---|
Press the home button | Transition to the home screen and start picture-in-picture | Transitions to the home screen, playback stops, and audio stops * 1 |
Press the picture-in-picture button | Transition to the home screen and start picture-in-picture | Transition to the home screen and start picture-in-picture |
Swipe down on the playback screen | Proprietary mini player starts | Proprietary mini player starts |
To get the user to start picture-in-picture Operate with the home button and prepare a picture-in-picture button.
The behavior when the home button is pressed follows OS settings </ b>. When you press the picture-in-picture button, start picture-in-picture </ b> as usual.
There are AVPlayerView
, AVPlayerViewController
, and AVPlayerLayer
to implement the player.
However, this time we will describe the implementation using AVPlayerLayer
.
Check it because it may not be supported by the OS such as iOS12.
Inform the OS that the picture-in-picture will be played and prepare.
Wait for the OS to be ready for picture-in-picture playback.
The start command is executed only when starting with the picture-in-picture button.
https://developer.apple.com/documentation/avkit/adopting_picture_in_picture_in_a_custom_player
SamplePlayer.swift
func setupPictureInPicture() {
// Ensure PiP is supported by current device.
if AVPictureInPictureController.isPictureInPictureSupported() {
// Create a new controller, passing the reference to the AVPlayerLayer.
pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
pictureInPictureController.delegate = self
pipPossibleObservation = pictureInPictureController.observe(\AVPictureInPictureController.isPictureInPicturePossible,
options: [.initial, .new]) { [weak self] _, change in
// Update the PiP button's enabled state.
self?.pictureInPictureButton.isEnabled = change.newValue ?? false
}
} else {
// PiP isn't supported by the current device. Disable the PiP button.
pictureInPictureButton.isEnabled = false
}
}
@IBAction func togglePictureInPictureMode(_ sender: UIButton) {
if pictureInPictureController.isPictureInPictureActive {
pictureInPictureController.stopPictureInPicture()
} else {
pictureInPictureController.startPictureInPicture()
}
}
AVPictureInPictureController.isPictureInPictureSupported()
Corresponds to 3-2-1.
Here, it is determined whether the target device supports picture-in-picture.
AVPictureInPictureController(playerLayer: playerLayer)
Corresponds to 3-2-2.
I am creating an AVPictureInPictureController.
By creating this AVPictureInPictureController, it will be automatically pressed when the home button is pressed.
Picture-in-picture playback will begin.
AVPictureInPictureController.isPictureInPicturePossible
Corresponds to 3-2-3.
There is a several seconds delay </ b> between the generation of AVPictureInPictureController and its activation.
Since it changes dynamically, it seems that the sample application uses observe.
If you are using Rx, I think it is possible to monitor the parameters with Rx.
pictureInPictureController.startPictureInPicture()
Corresponds to 3-2-4.
Requests a start when the picture-in-picture button is pressed after it is ready.
It is not necessary when the home button is pressed to start automatic playback.
As described in the previous section, there is a delay from the generation of AVPictureInPictureController
to its activation.
Therefore, if the process is started after the button is pressed, the response will be poor.
Currently under investigation </ b>
When AVPictureInPictureController
is generated, it becomes true
for a moment, but immediately returns to false
.
It may be a phenomenon that occurs only in my environment, but I will describe it just in case.
The above is a rough outline of picture-in-picture. It can be introduced in more than a dozen lines, so why not consider it?
Recommended Posts