forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…flutter#6908) Part of flutter/flutter#148417. I'm working on re-landing flutter#6456, this time without using the `ActivityAware` interface (see flutter/flutter#148417). As part of that work, I'll need to better control the `ExoPlayer` lifecycle and save/restore internal state. These are some proposed refactors to limit how much work `VideoPlayer` is doing, so I can better understand what needs to be reset (or not) internally. Specifically, `VideoPlayer` no longer knows what an `EventChannel` or `EventSink` is, and does not need to manage the lifecycle (it stores a `private final VideoPlayerCallbacks` instead), and instead there is a `VideoPlayerCallbacks` interface that does all that. I'm totally open to: - Landing this as-is (+/- nits) and making minor improvements in follow-up PRs - Making more significant changes to this PR and then landing it - Not landing this PR at all because it doesn't follow the approach the folks who maintain the plugin prefer Also happy to chat in VC/person about any of the changes.
- Loading branch information
1 parent
9840922
commit 9ac4571
Showing
5 changed files
with
256 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...er_android/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.videoplayer; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Callbacks representing events invoked by {@link VideoPlayer}. | ||
* | ||
* <p>In the actual plugin, this will always be {@link VideoPlayerEventCallbacks}, which creates the | ||
* expected events to send back through the plugin channel. In tests methods can be overridden in | ||
* order to assert results. | ||
* | ||
* <p>See {@link androidx.media3.common.Player.Listener} for details. | ||
*/ | ||
interface VideoPlayerCallbacks { | ||
void onInitialized(int width, int height, long durationInMs, int rotationCorrectionInDegrees); | ||
|
||
void onBufferingStart(); | ||
|
||
void onBufferingUpdate(long bufferedPosition); | ||
|
||
void onBufferingEnd(); | ||
|
||
void onCompleted(); | ||
|
||
void onError(@NonNull String code, @Nullable String message, @Nullable Object details); | ||
|
||
void onIsPlayingStateUpdate(boolean isPlaying); | ||
} |
98 changes: 98 additions & 0 deletions
98
...droid/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerEventCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.videoplayer; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
import io.flutter.plugin.common.EventChannel; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
final class VideoPlayerEventCallbacks implements VideoPlayerCallbacks { | ||
private final EventChannel.EventSink eventSink; | ||
|
||
static VideoPlayerEventCallbacks bindTo(EventChannel eventChannel) { | ||
QueuingEventSink eventSink = new QueuingEventSink(); | ||
eventChannel.setStreamHandler( | ||
new EventChannel.StreamHandler() { | ||
@Override | ||
public void onListen(Object arguments, EventChannel.EventSink events) { | ||
eventSink.setDelegate(events); | ||
} | ||
|
||
@Override | ||
public void onCancel(Object arguments) { | ||
eventSink.setDelegate(null); | ||
} | ||
}); | ||
return VideoPlayerEventCallbacks.withSink(eventSink); | ||
} | ||
|
||
@VisibleForTesting | ||
static VideoPlayerEventCallbacks withSink(EventChannel.EventSink eventSink) { | ||
return new VideoPlayerEventCallbacks(eventSink); | ||
} | ||
|
||
private VideoPlayerEventCallbacks(EventChannel.EventSink eventSink) { | ||
this.eventSink = eventSink; | ||
} | ||
|
||
@Override | ||
public void onInitialized( | ||
int width, int height, long durationInMs, int rotationCorrectionInDegrees) { | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("event", "initialized"); | ||
event.put("width", width); | ||
event.put("height", height); | ||
event.put("duration", durationInMs); | ||
if (rotationCorrectionInDegrees != 0) { | ||
event.put("rotationCorrection", rotationCorrectionInDegrees); | ||
} | ||
eventSink.success(event); | ||
} | ||
|
||
@Override | ||
public void onBufferingStart() { | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("event", "bufferingStart"); | ||
eventSink.success(event); | ||
} | ||
|
||
@Override | ||
public void onBufferingUpdate(long bufferedPosition) { | ||
// iOS supports a list of buffered ranges, so we send as a list with a single range. | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("values", Collections.singletonList(bufferedPosition)); | ||
eventSink.success(event); | ||
} | ||
|
||
@Override | ||
public void onBufferingEnd() { | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("event", "bufferingEnd"); | ||
eventSink.success(event); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("event", "completed"); | ||
eventSink.success(event); | ||
} | ||
|
||
@Override | ||
public void onError(@NonNull String code, @Nullable String message, @Nullable Object details) { | ||
eventSink.error(code, message, details); | ||
} | ||
|
||
@Override | ||
public void onIsPlayingStateUpdate(boolean isPlaying) { | ||
Map<String, Object> event = new HashMap<>(); | ||
event.put("isPlaying", isPlaying); | ||
eventSink.success(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.