即使 UIWebView 关闭,视频/音频流也不会停止 - iPad
我仅在 iPad 上看到此问题。 iPhone 上的功能与预期相同。
我正在 UIWebView 中从我的应用程序打开 URL。如果 URL 是普通网页,则它可以正常工作。但如果 URL 是远程视频/音频文件的 URL,UIWebView 会打开默认播放器,这又很好。
现在,当我关闭 UIWebView (通过单击播放器上的“完成”按钮)时,流媒体不会停止,并且音频/视频继续在后台播放(我看不到它,但它确实继续在后台播放,可以听到它)。创建 webview 的 UIViewController 也被释放(我在 dealloc 方法中放入了一条日志语句),但流并没有停止。
有人可以帮我解释为什么会发生这种情况吗?当 UIWebView 关闭时如何停止音频/视频流?
谢谢。
I see this issue only on the iPad. The same things works as expected on the iPhone.
I am opening the URL from my application in a UIWebView. If the URL is a normal web page, it works fine as expected. But if the URL is that of a remote video/audio file, the UIWebView opens the default player which is again good.
Now when I dismiss the UIWebView (by clicking on the Done button on the player), the streaming doesn't stop and the audio/video keeps playing in the background (I cannot see it but it does keep playing in the background, can hear it). The UIViewController in which the webview was created is also dealloced (I put in a log statement in the dealloc method) but the streaming doesn't stop.
Can someone please help me out on why this could be happening? And how can I stop the audio/video streaming when the UIWebView is closed?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我遇到了与所述相同的问题,但在这种情况下,不会停止播放的视频是使用 object/embed 方法嵌入的 Youtube 视频。
我花了很长时间试图弄清楚如何让视频停止播放,我找到的唯一解决方案是告诉 UIWebView 在关闭视图之前加载空白页面:
编辑(2015-05-12):正如所提到的下面的 @chibimai,alloc_iNit 的这个答案的工作原理相同,但因为我的答案是 5 年前 - 而他的只有 4——链接的答案可能更适用。我不再从事 iPhone 开发工作,所以我无法确定哪种方式更好。
I have the same issue as stated but in this case the video that won't stop playing is a Youtube video embeded using the object/embed method.
I spent a long time trying to figure out how to get the video to stop playing and the only solution I found was to tell the UIWebView to load a blank page before dismissing the view:
Edit(2015-05-12): As mentioned by @chibimai below, this answer by alloc_iNit works along the same lines but since my answer is from 5 years ago -- and his only 4 -- the linked answer may be more applicable. I no longer do iPhone dev work so I cannot determine which is better either way.
我还发现这种行为只是因为网络视图尚未发布。
I also found this behaviour simply because the web view hadn't been released.
我知道这是相当老的线程,但为了像我这样的新开发人员。
我的情况是:我使用拆分控制器,在主控器和播放器上的媒体列表位于详细控制器部分,
我遇到了同样的问题并尝试了各种解决方法。
我终于发现问题很简单,我在我的master中保存了详细控制器的引用,并且没有释放早期的详细控制器。在母版中的正确位置进行简单的发布就解决了这个问题。
I know this is pretty old thread, but for the sake of new developers like me.
My scenario was: I was using split controller, with media list on master and player in the detail controller section
I faced the same issue and tried all sorts of workaround.
I finally figured it out that the problem was simple, I was holding the reference of the detail controller in my master, and was not releasing the earlier detail controller. A simple release in the master at the right place solved the issue.
我正在解决同样的问题。我发现,如果您在脚本标记中定义类似的内容:
然后在 UIViewController 中添加:
它似乎尝试暂停/停止视频几秒钟,但随后您会听到音频继续播放!
更新!
这是媒体播放器的常见错误。你必须将播放时间设置为-1才能使其真正停止。
I'm working on the same problem. I've found that if you define something like this in your script tag:
Then in your UIViewController, add in:
It seems to try to pause/stop the video for several seconds, but then you hear the audio continue to play!
Update!
This is a general bug with the media player. You have to set the playback time to -1 in order to make it really stop.
我刚刚遇到了从网页的媒体播放器中自动打开 mp4 文件而无法关闭它的问题 - “完成”按钮仅在视频全屏模式下可见并且刚刚关闭全屏,因此我无法返回无需在导航栏添加“后退”按钮即可访问网页。 (在 iPhone 上,视频在单独的视图中打开,并通过“完成”按钮返回到源页面的 Web 视图)对
我有帮助的解决方法是在单独的媒体播放器中打开视频文件。
打开 MP4 文件
,
在模型视图中的媒体播放器中打开带有 URL 的视频,
不要忘记将 MediaPlayer.framework 添加到项目中,并导入
要构建的项目
PS。非常感谢 Viktor Gubrienko 提供的解决方案
I've just had an issues with an mp4 file opening automatically in media player from a web-page without possibility to close it - "Done" button was only seen in video fullscreen mode and just closed fullscreen, so I had no way to return to the webpage without adding a "Back" button to the navigation bar. (on iPhone the video was opening in a separate view with "Done" button taking back to the WebView with the source page)
The workaround that helped me is to open the video file in a separate media player.
catch opening an MP4 file
where
opens video with URL in a media player in a model view
don't forget to add MediaPlayer.framework to the project and import
for the project to be built
PS. many thanks to Viktor Gubrienko for the solution
事实上,我更喜欢使用空链接来解决问题。
像这样:
In fact, I prefer to use the null link to solve the problem.
like this:
感谢您在这个问题上的帖子,我能够找到一个完全解决该问题的解决方案,并且我在我的 iPad 上测试了它,而不仅仅是模拟器。这也解决了音频播放的问题。这不是永久的解决方案,而是一种有效的解决方法。
方法概述:
基本上所需要做的就是向 webView 发送一个简短的音频文件。我制作了 IMac Submarin.m4v 文件的副本,将其命名为 ping.m4v 并将其添加到资源文件夹中的 xcode 项目中。
然后,当我希望视频/音频停止时,我执行以下步骤:
现在您还必须处理错误“204”。错误 204 似乎只是一个警告,表示 webView 将处理此音频文件。为了处理该错误,我将这一行(参见>>)添加到
didFailLoadWithError
最后,在显示下一个 Audio/ 之前,我将
webView.hidden = FALSE
视频。这样,声音文件的小播放栏就不会显示在显示屏上。只有轻微的 ping 声...任何音频文件都可以...我希望这有帮助...
thanks to your thread on this issue I was able to figure out a solution that resolves the issue completely, and I tested it on my IPad, not just the simulator. This also resolves the issue with the audio playing. This is not the permanent resolution but an effective work around.
Overview of the approach:
Basically all that is needed is to send a short audio file to the webView. I made a copy of the IMac submarine.m4v file, I called ping.m4v and added it to my xcode project in the resource folder.
Then at the point when I want the video / audio to stop I do the following steps:
Now you also have to handle error "204". Error 204 seems to be just a warning saying that webView is going to handle this audio file. To handle the error I added this single line (see >>>) to
didFailLoadWithError
Finally I turn
webView.hidden = FALSE
right before I display my next Audio/Video. This way the little play bar for the sound file does not show on the display. There is just a soft ping sound... any audio file will do...I hope this helps...
这就是我用的。它确实会记录此错误“停用正在运行 I/O 的音频会话。在停用音频会话之前应停止或暂停所有 I/O。”但会停止音频文件的播放(淡出)并记住播放位置。
This is what I use. It does log this error "Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session." but does stop the audio file from playing (fading out) and remembers the play position.
由于网页视图尚未发布,视频在后台继续在网页视图上播放。
因此,您只需使用下面的代码即可释放网络视图,这对我来说效果很好。
我希望这有帮助...
As web view hadn't been released, the video keeps playing on the web view in background.
So you need to release the web view simply by using below code which works fine for me.
I hope this helps...
在我的例子中,使用 loadHTMLString 而不是使用 @"about:blank" 的 loadRequest,解决 macOS 上的问题
in my case using loadHTMLString and not loadRequest with @"about:blank", fix the problem on macOS
我在表格视图中遇到了同样的问题,其中表格视图单元格有一个加载音频文件的网络视图。一旦您播放并返回到另一个屏幕,音频仍然会播放。
解决方案:在表视图控制器的 viewWillDisappear(_:) 中重新加载表视图,一旦导航到另一个屏幕,音频将停止播放。
I had the same issue in Table view where the table view cell has a webview which loads an audio file . Once you play and go back to another screen the audio still plays .
Solution : reload the table view in viewWillDisappear(_:) of the table view controller and the audio will stop playing once you navigate to another screen .