wang-bin / fvp

Flutter video player plugin for all desktop+mobile platforms. download prebuilt examples from github actions. https://pub.dev/packages/fvp
BSD 3-Clause "New" or "Revised" License
197 stars 31 forks source link

Video stutters after changing the audio/video track #47

Closed lKinderBueno closed 12 months ago

lKinderBueno commented 1 year ago

Describe the bug I have a video with multiple audio/video/subtitles tracks. When I call the method setActiveTracks, the player starts to stutter.

Code:

import 'package:flutter/material.dart';
import 'package:fvp/mdk.dart';
import 'package:logging/logging.dart';

void main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    print('${record.loggerName}: ${record.message}');
  });
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: VideoPlayerScreen(),
      ),
    );
  }
}

class VideoPlayerScreen extends StatefulWidget {
  VideoPlayerScreen();

  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  late final player = Player();
  double ratio = 16 / 9;

  void setAudioTrackByIndex(int index) {
    int trackLength = player.mediaInfo.audio?.length ?? 0;
    if (trackLength == 0) return;

    List<int> tracks = List<int>.generate(trackLength, (index) => 0); //[0,0,0]
    tracks[index] = 1; //[0,1,0]
    player.setActiveTracks(MediaType.audio, tracks);
  }

  void setVideoTrackByIndex(int index) {
    int trackLength = player.mediaInfo.video?.length ?? 0;
    if (trackLength == 0) return ;

    List<int> tracks = List<int>.generate(trackLength, (index) => 0);
    tracks[index] = 1;
    player.setActiveTracks(MediaType.video, tracks);
  }

  @override
  void initState() {
    super.initState();
    player.media = 'https://mirror.selfnet.de/CCC/congress/2019/h264-hd/36c3-11235-eng-deu-fra-36C3_Infrastructure_Review_hd.mp4';
    player.loop = -1;
    player.state = PlaybackState.playing;
    player.updateTexture();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ValueListenableBuilder<int?>(
            valueListenable: player.textureId,
            builder: (context, id, _) => id == null ? const SizedBox.shrink() : Texture(textureId: id),
          ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setAudioTrackByIndex(1);
          },
          child: Icon(
            Icons.transcribe_rounded,
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    player.dispose();
  }
}

Log These are the logs in the moment I try to change the audio track:

D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 3584 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
D/om.example.tes(23768): PlayerBase::stop() from IPlayer
D/AudioTrack(23768): stop(43): called with 256 frames delivered
....
sudhanshugairola commented 1 year ago

Same issue in HLS.

sudhanshugairola commented 1 year ago

player dead while seeking on mdk.

wang-bin commented 1 year ago

player dead while seeking on mdk.

log

wang-bin commented 1 year ago

To select the second audio track, the tracks list is [1] instead of [0, 1, 0]

 player.setActiveTracks(MediaType.audio, [1]);
sudhanshugairola commented 1 year ago

Right