有没有办法在better_player - flutter 中播放多个音频?

问题描述 投票:0回答:1

这是我从内存中播放视频的代码,这是一个应用程序内视频。有什么方法可以像 netfix 或 youtube 一样在 better_player 中播放多个音频吗?视频保存在设备内存中并获取文件。

class MemoryPlayerPage extends StatefulWidget {
  final String? filePath;

  const MemoryPlayerPage({
    super.key,
    this.filePath,
  });

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

class MemoryPlayerPageState extends State<MemoryPlayerPage> {
  late BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    BetterPlayerConfiguration betterPlayerConfiguration =
        const BetterPlayerConfiguration(
      controlsConfiguration: BetterPlayerControlsConfiguration(
        enableAudioTracks: true,
      ),
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
    );

    _betterPlayerController = BetterPlayerController(betterPlayerConfiguration);
    _setupDataSource();
    super.initState();
  }

  void _setupDataSource() async {
    File file = File(widget.filePath!);
    List<int> bytes = file.readAsBytesSync().buffer.asUint8List();
  BetterPlayerDataSource dataSource = BetterPlayerDataSource.memory(bytes,
        videoExtension: "mp4", useAsmsSubtitles: false, useAsmsTracks: true);
    _betterPlayerController.setupDataSource(
      dataSource,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Memory player"),
      ),
      body: Column(
        children: [
          const SizedBox(height: 8),
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Text(
              "Memory player with plays video from bytes list. In this example"
              "file bytes are read to list and then used in player.",
              style: TextStyle(fontSize: 16),
            ),
          ),
          AspectRatio(
            aspectRatio: 16 / 9,
            child: BetterPlayer(controller: _betterPlayerController),
          ),
        ],
      ),
    );
  }
}

我尝试了在 better_player 中播放视频的详细代码,并且只需要在我的应用程序中播放它

flutter
1个回答
0
投票

要使用 Flutter 中的 better_player 包播放视频中的多个音轨,您需要确保您的视频文件包含多个音轨。然后,您可以使用“BetterPlayerDataSource”类来指定要播放的音轨。您可以通过以下方式修改代码来实现此目的:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:better_player/better_player.dart';

class MemoryPlayerPage extends StatefulWidget {
  final String? filePath;

  const MemoryPlayerPage({
    Key? key,
    this.filePath,
  }) : super(key: key);

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

class MemoryPlayerPageState extends State<MemoryPlayerPage> {
  late BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    BetterPlayerConfiguration betterPlayerConfiguration =
        const BetterPlayerConfiguration(
      controlsConfiguration: BetterPlayerControlsConfiguration(
        enableAudioTracks: true,
      ),
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
    );

    _betterPlayerController = BetterPlayerController(betterPlayerConfiguration);
    _setupDataSource();
    super.initState();
  }

  void _setupDataSource() async {
    File file = File(widget.filePath!);
    List<int> bytes = file.readAsBytesSync().buffer.asUint8List();
    BetterPlayerDataSource dataSource = BetterPlayerDataSource.memory(
      bytes,
      videoExtension: "mp4",
      useAsmsSubtitles: false,
      useAsmsTracks: true,
      audioTracks: [
        BetterPlayerAudioTrack(
          language: "English", // Language of the audio track
          trackUrl: "https://example.com/audio/english.mp3", // URL of the audio track
        ),
        BetterPlayerAudioTrack(
          language: "Spanish",
          trackUrl: "https://example.com/audio/spanish.mp3",
        ),
        // Add more audio tracks as needed
      ],
    );
    _betterPlayerController.setupDataSource(dataSource);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Memory player"),
      ),
      body: Column(
        children: [
          const SizedBox(height: 8),
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Text(
              "Memory player with plays video from bytes list. In this example"
              "file bytes are read to list and then used in player.",
              style: TextStyle(fontSize: 16),
            ),
          ),
          AspectRatio(
            aspectRatio: 16 / 9,
            child: BetterPlayer(controller: _betterPlayerController),
          ),
        ],
      ),
    );
  }
}

在_setupDataSource方法中,我将BetterPlayerAudioTrack对象添加到BetterPlayerDataSource的audioTracks参数中。每个 BetterPlayerAudioTrack 代表一个音轨及其语言和 URL。您可以根据需要添加任意数量的音轨,播放器将允许用户在播放过程中在它们之间切换。

请记住将占位符 URL(https://example.com/audio/english.mp3https://example.com/audio/spanish.mp3 等)替换为音频的实际 URL曲目。

© www.soinside.com 2019 - 2024. All rights reserved.