video_player integration
Checklist
- [x] The question is not already in the FAQ.
- [x] The question is not too narrow or specific to a particular application.
Suggested Question
Hey:-)
first of all, thank you so much for this amazing package.
My question is about integration with video_player service. I have an app that plays videos, and I want to have background control when the user exits from the app. The problem is that the video_player plays the video as an audio stream in the background isolate, but doesn't play the video in the app (I tried to transfer the videoPlayerController to my main code using customAction, but it can't convert this object in the channel, and returns Unhandled Exception: PlatformException(error, Converting object to an encodable object failed: Instance of 'VideoPlayerController', null, null) ).
The only solution that I have been able to do is to use separate video & audio services (with just_audio) to implement this feature, but this solution using two players has performance issues.
I saw that you wrote here a year ago that:
The video_player plugin does not support background execution, although I am creating a fork of this project to support it. Once that is done, you should also be able to add background and lock screen controls with audio_service.
Is that still a problem? Do you have another idea to solve it?
This issue was automatically closed because it did not follow the issue template.
HI @maayanmag I had forgotten about that! I think there's still even a fork of the video_player plugin among my GitHub repos, although it doesn't actually contain any changes since the client who wanted it no longer needed the feature, so I never bothered to do it.
Thank you very much for answering quickly :-) Is there a chance this will happen one day?
I don't actually have a need for a video player so it will probably not happen of my own accord. I know how to build such a thing, but time is limited and so whenever I do have free time to spend, I tend to spend it on what I personally have a need for.
I know how to build such a thing, but time is limited
@ryanheise could you please point on what needs to be done to achieve that? Maybe someone will take the idea and develop it
Thanks!
Thanks for the awesome plugins btw 👍
@sck-v thanks, I really appreciate your comment!
Unfortunately explaining what to do would involve refreshing my memory of the video_player code, and doing that would get me half way there to actually implementing it, so asking one is almost equivalent to asking the other in terms of the effort and time involved. If you're lucky, another client may ask me to build it, or one day I may find that I do have a personal need for it.
I'm sorry I know that is not the answer you wanted to hear, but it is difficult for me to manage that right now on top of what I'm currently doing.
I still don't have time to create this plugin, but I managed to dig up my old experimental code from 2019, which was for the Android side. Since audio_service does all the heavy lifting for the background service, I did not need to change much in the video_player plugin itself. The key code that needs to be added is that when entering background mode, we need to release and detach the surface texture, and then when re-entering foreground mode we need to reattach a surface:
private void enterBackground() {
System.out.println("enterBackground. setVideoSurface: null");
exoPlayer.setVideoSurface(null);
textureEntry.release();
surface.release();
eventChannel.setStreamHandler(null);
eventSink.setDelegate(null);
}
private void enterForeground(EventChannel eventChannel, TextureRegistry.SurfaceTextureEntry textureEntry) {
this.textureEntry = textureEntry;
this.eventChannel = eventChannel;
surface = new Surface(textureEntry.surfaceTexture());
System.out.println("enterForeground. setVideoSurface again: " + surface);
exoPlayer.setVideoSurface(surface);
eventChannel.setStreamHandler(
new EventChannel.StreamHandler() {
@Override
public void onListen(Object o, EventChannel.EventSink sink) {
System.out.println("enterForeground. onListen. setDelegate: " + sink);
eventSink.setDelegate(sink);
}
@Override
public void onCancel(Object o) {
eventSink.setDelegate(null);
}
});
sendInitialized(true);
}
I hope this will help someone else to submit a proper pull request to the video_player project.
Note there is now an issue for this here: https://github.com/flutter/flutter/issues/62739