Skip to content

Commit a98cbf8

Browse files
skyburmravn-google
authored andcommitted
Add video player buffering status (flutter#583)
1 parent 9e2a744 commit a98cbf8

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

packages/video_player/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.3
2+
3+
* Added video buffering status.
4+
15
## 0.5.2
26

37
* Fixed a bug on iOS that could lead to missing initialization.

packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ public void onCompletion(MediaPlayer mediaPlayer) {
132132
}
133133
});
134134

135+
mediaPlayer.setOnInfoListener(
136+
new MediaPlayer.OnInfoListener() {
137+
@Override
138+
public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
139+
Map<String, Object> event = new HashMap<>();
140+
switch (what) {
141+
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
142+
{
143+
event.put("event", "bufferingStart");
144+
eventSink.success(event);
145+
return true;
146+
}
147+
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
148+
{
149+
event.put("event", "bufferingEnd");
150+
eventSink.success(event);
151+
return true;
152+
}
153+
}
154+
return false;
155+
}
156+
});
157+
135158
mediaPlayer.prepareAsync();
136159

137160
Map<String, Object> reply = new HashMap<>();

packages/video_player/example/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class _VideoPlayPauseState extends State<VideoPlayPause> {
8181
),
8282
),
8383
new Center(child: imageFadeAnim),
84+
new Center(
85+
child: controller.value.isBuffering
86+
? const CircularProgressIndicator()
87+
: null),
8488
];
8589

8690
return new Stack(

packages/video_player/ios/Classes/VideoPlayerPlugin.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ - (void)updatePlayingState;
4646
static void* timeRangeContext = &timeRangeContext;
4747
static void* statusContext = &statusContext;
4848
static void* playbackLikelyToKeepUpContext = &playbackLikelyToKeepUpContext;
49+
static void* playbackBufferEmptyContext = &playbackBufferEmptyContext;
50+
static void* playbackBufferFullContext = &playbackBufferFullContext;
4951

5052
@implementation FLTVideoPlayer
5153
- (instancetype)initWithAsset:(NSString*)asset frameUpdater:(FLTFrameUpdater*)frameUpdater {
@@ -73,6 +75,14 @@ - (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpda
7375
forKeyPath:@"playbackLikelyToKeepUp"
7476
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
7577
context:playbackLikelyToKeepUpContext];
78+
[item addObserver:self
79+
forKeyPath:@"playbackBufferEmpty"
80+
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
81+
context:playbackBufferEmptyContext];
82+
[item addObserver:self
83+
forKeyPath:@"playbackBufferFull"
84+
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
85+
context:playbackBufferFullContext];
7686

7787
_player = [AVPlayer playerWithPlayerItem:item];
7888
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
@@ -161,6 +171,17 @@ - (void)observeValueForKeyPath:(NSString*)path
161171
} else if (context == playbackLikelyToKeepUpContext) {
162172
if ([[_player currentItem] isPlaybackLikelyToKeepUp]) {
163173
[self updatePlayingState];
174+
if (_eventSink != nil) {
175+
_eventSink(@{@"event" : @"bufferingEnd"});
176+
}
177+
}
178+
} else if (context == playbackBufferEmptyContext) {
179+
if (_eventSink != nil) {
180+
_eventSink(@{@"event" : @"bufferingStart"});
181+
}
182+
} else if (context == playbackBufferFullContext) {
183+
if (_eventSink != nil) {
184+
_eventSink(@{@"event" : @"bufferingEnd"});
164185
}
165186
}
166187
}

packages/video_player/lib/video_player.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class VideoPlayerValue {
5252
/// True if the video is looping.
5353
final bool isLooping;
5454

55+
/// True if the video is currently buffering.
56+
final bool isBuffering;
57+
5558
/// The current volume of the playback.
5659
final double volume;
5760

@@ -72,6 +75,7 @@ class VideoPlayerValue {
7275
this.buffered: const <DurationRange>[],
7376
this.isPlaying: false,
7477
this.isLooping: false,
78+
this.isBuffering: false,
7579
this.volume: 1.0,
7680
this.errorDescription,
7781
});
@@ -92,6 +96,7 @@ class VideoPlayerValue {
9296
List<DurationRange> buffered,
9397
bool isPlaying,
9498
bool isLooping,
99+
bool isBuffering,
95100
double volume,
96101
String errorDescription,
97102
}) {
@@ -102,6 +107,7 @@ class VideoPlayerValue {
102107
buffered: buffered ?? this.buffered,
103108
isPlaying: isPlaying ?? this.isPlaying,
104109
isLooping: isLooping ?? this.isLooping,
110+
isBuffering: isBuffering ?? this.isBuffering,
105111
volume: volume ?? this.volume,
106112
errorDescription: errorDescription ?? this.errorDescription,
107113
);
@@ -116,6 +122,7 @@ class VideoPlayerValue {
116122
'buffered: [${buffered.join(', ')}], '
117123
'isPlaying: $isPlaying, '
118124
'isLooping: $isLooping, '
125+
'isBuffering: $isBuffering'
119126
'volume: $volume, '
120127
'errorDescription: $errorDescription)';
121128
}
@@ -230,6 +237,12 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
230237
buffered: values.map<DurationRange>(toDurationRange).toList(),
231238
);
232239
break;
240+
case 'bufferingStart':
241+
value = value.copyWith(isBuffering: true);
242+
break;
243+
case 'bufferingEnd':
244+
value = value.copyWith(isBuffering: false);
245+
break;
233246
}
234247
}
235248

packages/video_player/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: video_player
22
description: Flutter plugin for displaying inline video with other Flutter
33
widgets on Android and iOS.
44
author: Flutter Team <flutter-dev@googlegroups.com>
5-
version: 0.5.2
5+
version: 0.5.3
66
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player
77

88
flutter:

0 commit comments

Comments
 (0)