Mohammad
Mohammad

Reputation: 46

how to display .mkv with subtitles using better player or chewie in flutter?

Currently, I have managed to display movies with the .mkv extension, but these movies have several languages ​​or subtitles, in other video players there is the ability to switch between audio and subtitles, but nothing is displayed in my video player. It should be noted that I can add audio or subtitles to the video, but I want to use the sound and subtitles attached to the .mkv video file. What do you think is the solution to this problem?

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

 
var src =
    'https://dl5.freeserver.top/www2/film/animation2/The.Croods.2013.1080p.BluRay.SoftSub.DigiMoviez.mkv';

class SubtitlesPage extends StatefulWidget {
  @override
  _SubtitlesPageState createState() => _SubtitlesPageState();
}

final videoPlayerController = VideoPlayerController.network(src);

class _SubtitlesPageState extends State<SubtitlesPage> {
  late BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    BetterPlayerConfiguration betterPlayerConfiguration =
        const BetterPlayerConfiguration(
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
      subtitlesConfiguration: BetterPlayerSubtitlesConfiguration(
        backgroundColor: Colors.green,
        fontColor: Colors.white,
        outlineColor: Colors.black,
        fontSize: 20,
      ),
    );

    _betterPlayerController = BetterPlayerController(betterPlayerConfiguration);
    _betterPlayerController.addEventsListener((event) {
      if (event.betterPlayerEventType == BetterPlayerEventType.progress) {
        log("Current subtitle line: ${_betterPlayerController.renderedSubtitle}");
      }
    });
    _setupDataSource();
    super.initState();
  }

  void _setupDataSource() async {
    BetterPlayerDataSource dataSource = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
      src,
      subtitles: BetterPlayerSubtitlesSource.single(
        type: BetterPlayerSubtitlesSourceType.file,
        url: src,
        name: "My subtitles",
        selectedByDefault: true,
      ),
    );
    _betterPlayerController.setupDataSource(dataSource);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Subtitles"),
      ),
      body: Column(children: [
        const SizedBox(height: 8),
        AspectRatio(
          aspectRatio: 16 / 9,
          child: BetterPlayer.network(src),
        )
      ]),
    );
  }
}

Upvotes: 2

Views: 1369

Answers (0)

Related Questions