iOS controls not reappearing when touching screen - ios

I have a basic iOS AVPlayer configured as follows:
let player = AVPlayer(url: URL(string: <some video file URL>)!)
let controller = AVPlayerViewController()
controller.player = player
self.present(controller, animated: true, completion: { player.play() })
When this code is called with a video URL, a full-screen player opens with visible controls where the video appears and auto-plays. After a few seconds the controls auto-hide, and reappear when the screen is touched. (Tested with XCode 10.0)
Now I want to play audios in the same way, i.e. in full-screen and with the same controls. I saw in the documentation that AVPlayer also accepts mp3 files. Indeed, when I just pass an audio URL to the code above, it displays the control buttons and plays the audio. (NB: I first want things to work just with black screen and will later add a poster image.)
However: unlike with videos, when the controls disappear (for some reason they don't auto-hide but they can still disappear, e.g. maybe after locking/unlocking the screen), they do not reappear upon touching the screen. The user then has no way to pause or exit the player and is forced to kill the app.
How can I make sure that the controls show up when touching the screen?

Related

AVPlayer fast forwards unexpectedly

I'm playing a video using AVPlayer in a modal. When I swipe down the view, video play continues like Youtube app and I can hear the sound. But the problem is when I bring the view back, player plays the part of video that was not displayed very quickly until it catches up to current cursor.
For example, if I hid the view at 1:00 and revives it at 1:30, player shows the part from 1:00 to 1:30 in very high rate. I want video to be just playing at 1:30 cursor smoothly.
When hiding the view, you need to pause() the player, i.e.
player.pause()
Now, when you need to unhide the view, call play() method on the player, i.e.
player.play()

Implementing media player using AVPLayer to play audio from remote url in popup view in modal view controller in Swift 4

Sorry I made title too long as I'm trying to accomplish a lot of things in an attempt to play audio file from an url using AVPlayer in a modal view controller. I've used Apple's foundation example - AVFoundationSimplePlayer, tweaked it and added few things. Player works fine but I've few challenges to solve as described below:
Here is the complete source code Source Code of my project
1) Refer to screenshot below, I want the background of modal vc to be transparent so that I can see the view which launched this modal view.
2) There is delay after loading the modal vc and before the popup view appears with controls highlighted. I'm not sure if this is normal to have delay? If it's normal and we can't do anything about it then I want to add activity control during this loading period. I'm not sure when to stop this activity control.
3) There are errors shown before player loads the control. Not sure what they are?
2018-09-05 13:32:56.519014+0100
AVFoundationSimplePlayer-Swift[44042:2403222] Task
.<2> finished with error - code:
-999 2018-09-05 13:32:57.607560+0100 AVFoundationSimplePlayer-Swift[44042:2403221] Task
.<3> finished with error - code:
-999
I just checked your attached sample code. You are using play function before assigning any AVPlayerItem to your player. You're loading your asset asynchronously and before downloading the asset player can not play. So what you should do
asset = AVURLAsset(url: movieURL, options: nil)
/// Just after making an asset and remove the code written in asset's setter
let item = AVPlayerItem(asset: asset!)
player = AVPlayer(playerItem: item)
/// This extra line is to playing the live url. It will play what it downloads in chunks.
if #available(iOS 10.0, *) {
player.automaticallyWaitsToMinimizeStalling = false
} else {
// Fallback on earlier versions
}
/// Now your player is ready to play
player.play()
EDIT 1
If you're still struggling with above piece of code so I have made changes in PlayerViewController.m file to fix your issues. Here is the gist link of that file. Search /// TheTiger Change to find what change I made. Now I'm able to play audio file with proper time and slider value.
EDIT 2
I thought you will be able to see the difference but no problem I will explain in more detail.
#3. Regarding your error message this is just a Xcode debug message and you can disable it. See this answer for more detail.
#2. There is a delay in presenting the modal because you have all the code in your viewWillAppear: method just move that code in videDidAppear: and let the view appear first before doing anything. It will remove the delay.
#1. Background of modal vc to be transparent. So it is possible to make it transparent See this answer.
This Sample Code just works fine.

Transition avplayer to fullscreen from embedded view

I am currently displaying a video embedded in a view. On tap of this video I need to present it in full screen just like how App Store does it.
Right now I am using AVPlayer to embed in the view with a AVPlayerLayer. When I detect a tap on this embedded player, I am creating a AVPlayerViewController and setting its player object with the player currently embedded, but still I can notice a delay of 3~4 seconds until the player in fullscreen starts playing. How can I make this transition smooth ?

AVPlayer blank video: playing sound but no video

This question has the same problem, but the solutions didn't work.
The AVPlayer sometimes plays a blank video: there is sound but no video.
While blank videos were played, we printed the frame and status of the player. The frame was non-zero and was correctly set. The status was also readyToPlay. The play function is also invoked on the main thread.
In other words, the frame for the player layer is valid, and the player is also ready to play. Yet no video appears, even though sound does.
The issue seems to be a timing one. If we wait 5-10 seconds before playing the video, the video works fine each time.
This issue appears on iOS 10, not iOS 8 or 9.
This thread on the Apple forums suggests it might be a bug related to
AVVideoCompositionCoreAnimationTool, which we also use.
Any solutions?
This happens more often on iPhone 7 devices than iPhone 5s devices.
Code:
fileprivate func playVideo(_ videoURL: String, prompt: String) {
// Show <playerView>
playerView.isHidden = false
// Use new video in player
playerItem = AVPlayerItem(url: URL(fileURLWithPath: videoURL))
print("STATUS: \(player.status == AVPlayerStatus.readyToPlay). FRAME: \(playerLayer.frame). MAIN THREAD: \(Thread.isMainThread)")
player.replaceCurrentItem(with: playerItem)
// Start playing video
player.seek(to: kCMTimeZero)
player.actionAtItemEnd = .none
player.play()
}
I think you need to play with a AVPlayerViewController or AVPlayerLayer.
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
//set player layer frame and attach it to our view
playerLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:playerLayer];
//play the video
[player play];
From Apple doc:
AVPlayer and AVPlayerItem are nonvisual objects meaning that on their
own are unable to present an asset’s video on screen. You have two
primary approaches you can use to present your video content on
screen:
AVKit: The best way to present your video content is by using the
AVKit framework’s AVPlayerViewController class in iOS and tvOS or the
AVPlayerView class in macOS. These classes present the video content,
along with playback controls and other media features giving you a
full-featured playback experience.
AVPlayerLayer: If you are building a custom interface for your player,
you use a Core Animation CALayer subclass provided by AVFoundation
called AVPlayerLayer. The player layer can be set as a view’s backing
layer or can be added directly to the layer hierarchy. Unlike
AVPlayerView and AVPlayerViewController, a player layer doesn’t
present any playback controls, but simply presents the visual content
on screen. It is up to you to build the playback transport controls to
play, pause, and seek through the media.
In case anyone encounters this issue, the problem seems related to an iOS bug. The problem doesn't occur on iOS 8/9 devices, only on new iOS 10 devices. Starting with iOS 10.2, the problem vanishes.
Unfortunately, still no programmatic solution for 10.0.x or 10.1.x.

How can I force a video played in a UIWebView to be full screen on an iPad?

I'm playing a YouTube video using a UIWebView, like so:
self.webView.mediaPlaybackRequiresUserAction = NO;
self.webView.allowsInlineMediaPlayback = NO;
[self.webView loadHTMLString:videoEmbedCode baseURL:nil];
I want to force the video to play in full screen without the user tapping the button at the bottom right. This works fine on an iPhone (tap the player to play and it automatically enters full screen), but doesn't work on an iPad (the normal inline player is shown).
I assumed that setting allowsInlineMediaPlayback to NO would do the trick, but apparently not.
Is there a way to force HTML5 videos to play in full screen on an iPad?
If you are trying to play a youtube video in fullscreen directly, then Use a webview of size fullScreen and then handle its rotation accordingly and change the player size of Rotation too. If you need more help then i can help you with the code too for how to change the player size on rotation too.

Resources