Closed Nesh108 closed 1 year ago
@Nesh108 we've been experiencing similar battery drain issues and have not been able to narrow it down yet. Our first thought was it was related to PiP features, your issue makes me think otherwise now and may be something else.
Did you enable background audio on your xcode project?
@EfosaSO Yes, it is enabled.
What I am noticing is that backgrounding the app causes the CPU to skyrocket to 100% and because of that, iOS decides to terminate the app when the screen is off.
Do we have any update on this issue, facing the same? Using the library to play Audios mostly it plays in the background and drains the battery highly.
For us, it turned out to be an issue with another library (rn-placeholder
), which was causing the issue.
Basically, to generalise the problem: anything that makes the app use too much CPU even when backgrounded will trigger iOS to flag it and kill it. So, profile your app and see if that's the case then start removing chunks of your app until the CPU spikes disappear and work your way back.
@freeboub This issue still exist in the latest beta version 6.0.0-beta.8 using RN 0.71.11, the energy consumption goes higher when the app sent to background and kills the app, I am playing Audio using the player. How to handle, many active users are affected.
Hello folks, in our internal testing we also noticed our iOS app causing a lot of battery drain while being in the background. We profiled our app using XCode instruments -> CPU Profiler and noticed some method calls related to AVFCore happening while the app was in the background. We made some comparisons internally and figured out that the Video
component was causing the battery drain, even after passing all the pause and background related props properly.
We landed on the following component as a basic solution :
interface IStateAwareVideoProps {
ref?: React.MutableRefObject<Video>;
props: VideoProperties;
}
export const StateAwareVideo = ({ ref, props }: IStateAwareVideoProps) => {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
useEffect(() => {
const subscription = AppState.addEventListener(
'change',
(nextAppState) => {
appState.current = nextAppState;
setAppStateVisible(appState.current);
},
);
return () => {
subscription.remove();
};
}, []);
return (
<>
{appStateVisible === 'active' ? (
<Video ref={ref} {...props} />
) : (
<View />
)}
</>
);
};
After testing, with this, our app was no longer using unnecessary battery in the background.
Bug
Platform
Which player are you experiencing the problem on:
Environment info
React native info output:
Library version: commit #8013b9a19763a326aa94b0967e9e8925ffd63693
Steps To Reproduce
Note: This high CPU doesn't happen if the audio/video has not been started.
Expected behaviour
Note
This is why I believe some people are having issues with background audio/video playing on iOS, so it might be important to look into it.
Component: