RenderHeads / UnityPlugin-AVProVideo

AVPro Video is a multi-platform Unity plugin for advanced video playback
https://www.renderheads.com/products/avpro-video/
238 stars 29 forks source link

[IOS] MediaPlayer is not resumed when app regain focus #1440

Closed KeeleyYu closed 1 year ago

KeeleyYu commented 1 year ago

Hi, When the app goes to the background while a video is playing, it pauses like it should. But when the app regains focus, the video does not resume playing on iOS only. The app is still running fine. The other functions of app are good as well. It doesn't happen all of the time but it's likely to happen if you go all the way back to the os, tap some other things. Also, it occasionally happens when airpods connected or disconnected. Yet, it works on android and windows.

I have checked the answer for issues #1261, #447 and #91, and I still have some questions as follows:

  1. MediaPlayer_AppFocus.cs is not work on IOS, because of this reason (https://github.com/RenderHeads/UnityPlugin-AVProVideo/issues/1261#issuecomment-1260534127). So I guess there is some ‘natural mechanism’ on IOS which seems not work at all. Even though I remove the UNITY_IOS macro from the #if defines, it still get stuck occasionally. Is it because that ‘Control.Pause()’ only works on non-ios platform? If so or not, how can I fix this problem ?

  2. Since I dynamically create the MediaPlayer component through code, I can't check the "Resume playback on audio session route change" checkbox directly through the editor. Instead, I set the 'm_mediaPlayer.PlatformOptionsIOS.flags' to 'ResumeMediaPlaybackAfterAudioSessionRouteChange' by code before video playback, but it doesn't seem to work (video will not resume when headphone connect/disconnect). How can I solve this ?

Your Setup (please complete the following information): Unity version: 2020.3.25f1 AVPro Video version (number and edition (trial/core/ultra/enterprise)): ultra Operating system version: v2.6.6 Device model: ios Video specs (resolution, frame-rate, codec, file size): none

Chris-RH commented 1 year ago

Could you update to the latest version of AVPro please Which device(s) have you tested? Which iOS version(s) have you tested? Have you tested it in a new scene using the mediaplayer component?

KeeleyYu commented 1 year ago
  1. As for update, I can't see any IOS related modifications though, I will have a try.
  2. I mostly test on iphone12 with version 14.1 , but other Iphone devices(e.g. Iphone8 with version 11 ) also have the bugs.
  3. I haven't test on other new scene. @Chris-RH
Chris-RH commented 1 year ago

Great, please let me know how it goes after you have done 1 and 3, thanks :)

KeeleyYu commented 1 year ago

I have tested both 1 and 3. Still got the bug :( And I found that if the interval between switching foreground and background is short enough, no matter whether the video got stuck, it will play again. Oppositely, it will get stuck. Is there any inspiration ?

KeeleyYu commented 1 year ago

image I have disabled the macro in the first line of MediaPlayer_AppFocus for testing. As seen from the log, when switching back to the app from background, it did run Control.Play(), yet without actually play. I haven't look deep into this but, is it related to the AppleMediaplayer + Native ?

MorrisRH commented 1 year ago
  1. Since I dynamically create the MediaPlayer component through code, I can't check the "Resume playback on audio session route change" checkbox directly through the editor. Instead, I set the 'm_mediaPlayer.PlatformOptionsIOS.flags' to 'ResumeMediaPlaybackAfterAudioSessionRouteChange' by code before video playback, but it doesn't seem to work (video will not resume when headphone connect/disconnect). How can I solve this ?

This part of the issue has been fixed and will make it into the next release.

MorrisRH commented 1 year ago

When the app goes to the background while a video is playing, it pauses like it should. But when the app regains focus, the video does not resume playing on iOS only.

Unfortunately a fix provided for pausing playback when the control centre was opened prevented playback from resuming when an application was returned from the background. This is now fixed and will make it into the next release.

KeeleyYu commented 1 year ago

@MorrisRH Awesome, that really helps a lot!! If the next release not that soon, can I implement the fix by myself in somewhere ?

MorrisRH commented 1 year ago

We're planning to get the next release out later this week. The fix is in the native part of the code so there is nothing you can do. A possible workaround would be to call play after a short delay when returning from the background.

KeeleyYu commented 1 year ago

Appreciate ! I'm looking forward to it :)

Chris-RH commented 1 year ago

The latest version has been released. Please let us know if it does not fix your issue.

KeeleyYu commented 1 year ago

Thanks for swift release! It seems that the bug has been fixed since I have done a couple of test. I will do more testing and developing based on it :) @Chris-RH

KeeleyYu commented 1 year ago

One more weird IOS bug here. When switching back from background, the video will more likely start from a certain frame if the interval is larger than around 1s, without continue from the frame that switching to background. Seems that the player record a wrong frame in an early time, leading that the video more likely revert to it Please have a check too :) @Chris-RH

MorrisRH commented 1 year ago

The player jumps back to the previous key-frame, afraid there is nothing we can do about this

KeeleyYu commented 1 year ago

The player jumps back to the previous key-frame, afraid there is nothing we can do about this

How can I get the key-frame? Or can I set it when monitoring application switching?

MorrisRH commented 1 year ago

Key-frames are complete frames in the movie file so do not need additional frames being stored in order to decode. When the application goes to the background the partial frames are discarded to reduce memory pressure on the system which is why when the app returns to the foreground it resumes from the last key-frame.

If you really want to try and work around this then you could store the current time when the app goes into the background, and seek to it when focus is restored. There will most likely be a jump though.

KeeleyYu commented 1 year ago

Yep I have already tried this way and fixed it at all. Thanks for your explanation as well 👍