Skip to content

[video_player_ios] Video player not rendering frame but video position keeps updating #64561

@akindone

Description

@akindone

Steps to Reproduce

  1. Run flutter create bug.
  2. Update the files as follows: ...
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: IosVideoDisappear());
  }
}

class IosVideoDisappear extends StatefulWidget {
  @override
  _IosVideoDisappearState createState() => _IosVideoDisappearState();
}

class _IosVideoDisappearState extends State<IosVideoDisappear> {
  VideoPlayerController controller1;
  VideoPlayerController controller2;

  @override
  void initState() {
    super.initState();
    initVideoPlayerController();
  }

  Future<void> initVideoPlayerController() async {
    VideoPlayerController initController(String url) {
      var controller = url.startsWith('http')
          ? VideoPlayerController.network(url)
          : VideoPlayerController.asset(url);
      controller.addListener(() {
        setState(() {});
      });
      controller.setLooping(false);
      controller.initialize().then((value) => setState(() {}));
      controller.play();
      return controller;
    }

    controller1 = initController(getRandomUrl());
    controller2 = initController(getRandomUrl());
  }

  Future<void> changeVideo() async {
    controller1.pause();
    controller2.pause();
    var lastController1 = controller1;
    var lastController2 = controller2;
    await initVideoPlayerController();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      lastController1.dispose();
      lastController2.dispose();
    });
  }

  String getRandomUrl() {
    return videoUrls[Random().nextInt(videoUrls.length)];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Stack(
            children: [
              SizedBox(
                width: 960,
                height: 600,
                child: _buildVideoView(controller1),
              ),
              SizedBox(
                width: 300,
                height: 200,
                child: _buildVideoView(controller2),
              ),
            ],
          ),
          Wrap(
            spacing: 10,
            children: [
              RaisedButton(
                onPressed: () {
                  controller1?.pause();
                },
                child: Text('VideoLarge pause'),
              ),
              RaisedButton(
                onPressed: () {
                  controller1?.seekTo(Duration(seconds: 0));
                  controller1?.play();
                },
                child: Text('VideoLarge play'),
              ),
              RaisedButton(
                onPressed: changeVideo,
                child: Text('Random change'),
              ),
              RaisedButton(
                onPressed: () {
                  controller2?.pause();
                },
                child: Text('VideoSmall pause'),
              ),
              RaisedButton(
                onPressed: () {
                  controller2?.seekTo(Duration(seconds: 0));
                  controller2?.play();
                },
                child: Text('VideoSmall play'),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildVideoView(VideoPlayerController controller) {
    return Column(
      children: [
        AspectRatio(
          aspectRatio: (controller?.value?.initialized ?? false)
              ? controller.value.aspectRatio
              : 16 / 9,
          child: (controller?.value?.initialized ?? false)
              ? VideoPlayer(controller)
              : Container(
                  color: Colors.black12,
                ),
        ),
        Text(
          _getPosition(controller),
        ),
      ],
    );
  }

  String _getPosition(VideoPlayerController controller) {
    return '${controller?.value?.position}/${controller?.value?.duration}';
  }
}

List<String> videoUrls = [
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/5.1.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/5.3.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/5.5.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/5.7.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/6.1.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/6.3.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/6.5.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/6.7.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/7.1.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/7.3.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/7.5.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/7.7.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/8.1.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/8.3.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/8.5.ppt.mp4',
  'https://focus-resource.oss-cn-beijing.aliyuncs.com/camp/video/test/8.7.ppt.mp4',
];

  1. ...

Expected results:every time press the "Random change" button will change the video, and the postion text can update

Actual results:about 2% chance will encounter the case: no video frame is rendered but the VideoPlayerController.value keep updating the position; press the pause /seek play button can controll the position but the frame of video is still not rendered
this is the demo video:
video_demo.zip

Logs
flutter analyze
Analyzing video_debug...                                                
No issues found! (ran in 2.2s)
 flutter doctor -v
[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.5 19F101, locale zh-Hans-CN)
    • Flutter version 1.20.2 at /Users/jike/software/flutter
    • Framework revision bbfbf1770c (12 days ago), 2020-08-13 08:33:09 -0700
    • Engine revision 9d5b21729f
    • Dart version 2.9.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: qualityA truly polished experiencefound in release: 1.22Found to occur in 1.22has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: video_playerThe Video Player pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: timeoutIssue is closed due to author not providing the requested details in time

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions