UIWebView reloads video when closing its parent view - ios

I am trying to play a video from my UIWebView and play its audio in the background. This works fine, however when I lock my screen / close out of the view their is a slight delay in sound. The audio goes dead for a split second every time. Why is this happening? Is there a way to access avaudiosession of a UIWebview so I can get more information? Thank you.
Ive tried
adding all info.plist recommendations
enabling background mode
arbitrary loads = true
avaudiosession.shared().setactive = true
laying out views if needed
webView.mediaPlaybackRequiresUserAction = false
webView.allowsInlineMediaPlayback = true
webView.loadHTMLString(htmlString, baseURL: URL(string: "https://www.youtube.com"))

UIWebview is deprecated in swift 4 so for that you need to use WKWebview in same way. read user guide to use class of WKWebview.

Related

How to disable iOS Webview going full screen on video play

I am trying to play inline video embedded inside iOS native app.
Whatever I do, the player keeps entering full screen automatically once the video is played.
I was trying all mentioned suggestions such as playsinline , allowfullscreen="false", controls="false" etc.
Is there a way to prevent iOS webview from entering full screen?
You can enable inline playback in an iOS web view via configuration in your code, using WKWebViewConfiguration and allowsInlineMediaPlayback:
https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback
Set this property to true to play videos inline, or false to use the native full-screen controller. When adding a video element to an HTML document on iPhone, you must also include the playsinline attribute.
The default value of this property is false for iPhone and true for iPad.
You can see an example in this answer: https://stackoverflow.com/a/59834721/334402
Swift 5.5🚀
You just need allowsInlineMediaPlayback = true
class WebViewConfigurator {
private var _webViewConfig = WKWebViewConfiguration()
var webKitConfig : WKWebViewConfiguration {
_webViewConfig
}
init() {
_webViewConfig.allowsInlineMediaPlayback = true
}
}

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.

Disabling auto-play in full screen on iOS

The only problem i'm having is that i'm unable to disable playing videos on full screen according to apple documentation this is enabled by default and needs to be set as follows:
webView.configuration.allowsInlineMediaPlayback = true
Which is based on my understanding how it's supposed to be. However this doesn't work and even after configuration right after you press play on video it opens up in native full screen player. I'm using WKWebView.
Apple's Documentation for this
I faced with this problem using video tag in web view.
I have three videos in the page, and all videos successively played full screen.
I solved this problem by
1.Check Inline Playback
2.Add playsinline attribute to video tag
<video src="your_videosrc" autoplay playsinline></video>
Glad someones help :)
Playing a video inline requires two prerequisites:
Setting the configuration
Providing a correctly formatted link
1. Setting the configuration
The configuration of the WKWebView has needs to be set while initialising. Modifying allowsInlineMediaPlayback at a later point will not work:
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), configuration: configuration)
view.addSubview(webView)
2. Providing a correctly formatted link
In order for the web view to know that the video should start playing inline the appropriate URL parameter needs to be set.
For this you simply have to append ?playsinline=1.
Example:
webView.load(URLRequest(url: URL(string: "https://www.youtube.com/watch?v=OYbXaqQ3uuo?playsinline=1")!))
From Storyboad you can check/uncheck Inline playback to stop/allow full screen.
I'm having a similar issue, however it's a slight variation. We have a WkWebView that loads a mobile friendly web application. At various points there are links to videos which work great in that they do open the default iOS video player so the user can go full screen. That's all great. However we have a section where we have some podcasts. I'm trying to use the streaming provider's javascript player for these audio streams but when I press play the native iOS video player takes over. We don't want this because the provider's player has some features we need in it. I've tried adding the ?playsinline=1 but didn't have any affect on the playback. Is it possible to do this without affecting he video playback in the other areas of our app?

Is there a way to prevent AVPlayerViewController from updating the lock screen via MPNowPlayingInfoCenter?

here is my problem:
I've got an app playing audio files, updating the lockscreen info via MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo, and this part is working fine.
But in an other view, i'm playing a video with AVPlayerViewController and AVPlayer, and when the video starts playing, it's updating the lock screen automatically, with nothing except the video duration.
I didn't find anything about this behaviour in Apple's documentation, I can't find a way to disable it.
So far, I've tried calling UIApplication.sharedApplication().endReceivingRemoteControlEvents() before the video starts playing, and beginReceivingRemoteControlEvents() after. It doesn't work.
Does anyone know a way to prevent this?
Starting with iOS 10 there is a BOOL property in AVPlayerViewController called updatesNowPlayingInfoCenter, that has the default value: YES. Just change it to NO:
//playerController is an instance of AVPlayerViewController
if ([self.playerController respondsToSelector:#selector(setUpdatesNowPlayingInfoCenter:)])
{
self.playerController.updatesNowPlayingInfoCenter = NO;
}

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