Flutter底部导航栏播放器插件bottom_navbar_player的使用

发布于 1周前 作者 eggper 来自 Flutter

Flutter底部导航栏播放器插件bottom_navbar_player的使用

bottom_navbar_player 是一个用于在 Flutter 应用中实现底部导航栏或底部弹出层中的音频和视频播放功能的插件。它支持从 URL、资产文件和本地文件进行播放,并且可以在 Android、iOS 和 Web 平台上运行。

预览

Preview

快速入门

1. 添加依赖

首先,在 pubspec.yaml 文件中添加插件依赖:

dependencies:
  flutter:
    sdk: flutter
  bottom_navbar_player: ^x.x.x # 替换为最新版本号

然后运行 flutter pub get 来安装依赖。

2. 导入包

在 Dart 文件中导入 bottom_navbar_player 包:

import 'package:bottom_navbar_player/bottom_navbar_player.dart';

3. 创建实例并设置播放器

创建 BottomNavBarPlayer 的实例,并将其设置为 ScaffoldbottomSheet 或者 bottomNavigationBar

final bottomNavBarPlayer = BottomNavBarPlayer();

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Bottom NavBar Player'),
    ),
    body: Center(child: ...),
    bottomSheet: bottomNavBarPlayer.view(),
  );
}

4. 播放视频和音频

视频播放

以下是从不同来源播放视频的示例:

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
    sourceType: SourceType.url,
    playerSize: PlayerSize.max,
    mediaType: MediaType.video,
  ),
  child: const Text('from URL'),
),

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    'assets/bee.mp4',
    sourceType: SourceType.asset,
    playerSize: PlayerSize.max,
    mediaType: MediaType.video,
  ),
  child: const Text('from Asset'),
),

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    '/storage/sdcard/Download/bee.mp4',
    sourceType: SourceType.file,
    playerSize: PlayerSize.max,
    mediaType: MediaType.video,
  ),
  child: const Text('from File'),
),

音频播放

以下是从不同来源播放音频的示例:

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    'https://download.samplelib.com/mp3/sample-9s.mp3',
    sourceType: SourceType.url,
    playerSize: PlayerSize.max,
    mediaType: MediaType.audio,
  ),
  child: const Text('from URL'),
),

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    'assets/audio.mp3',
    sourceType: SourceType.asset,
    playerSize: PlayerSize.max,
    mediaType: MediaType.audio,
  ),
  child: const Text('from Asset'),
),

MaterialButton(
  onPressed: () => bottomNavBarPlayer.play(
    '/storage/sdcard/Download/audio_file.mp3',
    sourceType: SourceType.file,
    playerSize: PlayerSize.max,
    mediaType: MediaType.audio,
  ),
  child: const Text('from File'),
),

5. 完整示例代码

以下是一个完整的示例,展示了如何在应用中集成 bottom_navbar_player 插件:

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

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final bottomNavBarPlayer = BottomNavBarPlayer();

  const titleStyle = TextStyle(
      color: Colors.black, fontWeight: FontWeight.bold, fontSize: 17);
  final boxDecoration = BoxDecoration(
      color: Colors.grey.shade100, borderRadius: BorderRadius.circular(12));

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Bottom NavBar Player'),
        ),
        body: Center(
            child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              padding: const EdgeInsets.all(25),
              decoration: boxDecoration,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text(
                    'Video Player',
                    style: titleStyle,
                  ),
                  const SizedBox(height: 20),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play(
                        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
                        sourceType: SourceType.url,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.video),
                    child: const Text('from URL'),
                  ),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play('assets/bee.mp4',
                        sourceType: SourceType.asset,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.video),
                    child: const Text('from Asset'),
                  ),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play(
                        '/storage/sdcard/Download/bee.mp4',
                        sourceType: SourceType.file,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.video),
                    child: const Text('from File'),
                  ),
                  MaterialButton(
                    color: Colors.orange.shade200,
                    onPressed: () => bottomNavBarPlayer.play('assets/bee.mp4',
                        sourceType: SourceType.asset,
                        playerSize: PlayerSize.min,
                        mediaType: MediaType.video),
                    child: const Text('Min Size'),
                  ),
                ],
              ),
            ),
            Container(
              padding: const EdgeInsets.all(25),
              decoration: boxDecoration,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text(
                    'Audio Player',
                    style: titleStyle,
                  ),
                  const SizedBox(height: 20),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play(
                        'https://download.samplelib.com/mp3/sample-9s.mp3',
                        sourceType: SourceType.url,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.audio),
                    child: const Text('from URL'),
                  ),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play('assets/audio.mp3',
                        sourceType: SourceType.asset,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.audio),
                    child: const Text('from Asset'),
                  ),
                  MaterialButton(
                    color: Colors.white,
                    onPressed: () => bottomNavBarPlayer.play(
                        '/storage/sdcard/Download/audio_file.mp3',
                        sourceType: SourceType.file,
                        playerSize: PlayerSize.max,
                        mediaType: MediaType.audio),
                    child: const Text('from File'),
                  ),
                  MaterialButton(
                    color: Colors.orange.shade200,
                    onPressed: () => bottomNavBarPlayer.play('assets/audio.mp3',
                        sourceType: SourceType.asset,
                        playerSize: PlayerSize.min,
                        mediaType: MediaType.audio),
                    child: const Text('Min Size'),
                  ),
                ],
              ),
            ),
          ],
        )),
        bottomSheet: bottomNavBarPlayer.view(),
      ),
    );
  }
}

权限和配置

Android

如果你需要访问网络资源,请在 AndroidManifest.xml 中添加以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

如果你需要连接到非 HTTPS 的 URL,请在 <application> 标签中添加:

<application ... android:usesCleartextTraffic="true">

iOS

如果你需要连接到非 HTTPS 的 URL,请在 Info.plist 中添加:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

更多关于Flutter底部导航栏播放器插件bottom_navbar_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部导航栏播放器插件bottom_navbar_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中集成和使用bottom_navbar_player插件的代码示例。这个插件假设用于在底部导航栏中实现一个音频或视频播放器功能。需要注意的是,bottom_navbar_player并不是一个官方或广泛认可的Flutter插件,因此以下示例将基于一个假设的API结构。如果bottom_navbar_player插件真实存在,代码可能需要根据插件的实际API进行调整。

首先,确保你的pubspec.yaml文件中已经添加了bottom_navbar_player依赖项(如果它存在的话):

dependencies:
  flutter:
    sdk: flutter
  bottom_navbar_player: ^x.y.z  # 替换为实际版本号

然后,运行flutter pub get来获取依赖项。

接下来,你可以在你的Flutter应用中实现底部导航栏播放器。以下是一个基本的示例:

import 'package:flutter/material.dart';
import 'package:bottom_navbar_player/bottom_navbar_player.dart'; // 假设的包导入路径

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

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

class BottomNavbarPlayerDemo extends StatefulWidget {
  @override
  _BottomNavbarPlayerDemoState createState() => _BottomNavbarPlayerDemoState();
}

class _BottomNavbarPlayerDemoState extends State<BottomNavbarPlayerDemo> {
  int _selectedIndex = 0;
  final List<Widget> _widgetOptions = [
    HomeScreen(),
    SearchScreen(),
    LibraryScreen(),
    ProfileScreen(),
  ];

  // 假设的播放器控制器
  BottomNavbarPlayerController _playerController;

  @override
  void initState() {
    super.initState();
    // 初始化播放器控制器
    _playerController = BottomNavbarPlayerController();
    // 加载音频或视频资源(假设的方法)
    _playerController.loadMedia('https://example.com/audio.mp3');
  }

  @override
  void dispose() {
    // 释放播放器控制器资源
    _playerController.dispose();
    super.dispose();
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bottom Navbar Player Demo'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search),
            label: 'Search',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.library_books),
            label: 'Library',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          ),
          // 添加一个播放控制项(假设)
          BottomNavigationBarItem(
            icon: Icon(_playerController.isPlaying ? Icons.pause : Icons.play_arrow),
            label: 'Player',
            onPressed: () {
              setState(() {
                _playerController.togglePlayPause();
              });
            },
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        // 假设的底部导航栏播放器
        bottomNavigationBarPlayer: BottomNavbarPlayer(
          controller: _playerController,
          showControls: true, // 是否显示播放控制(假设的属性)
        ),
      ),
    );
  }
}

// 假设的屏幕组件
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Home Screen'));
  }
}

class SearchScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Search Screen'));
  }
}

class LibraryScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Library Screen'));
  }
}

class ProfileScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Profile Screen'));
  }
}

请注意,上述代码包含了一些假设的API调用和组件,例如BottomNavbarPlayerControllerBottomNavbarPlayer。这些组件和方法可能并不存在于实际的bottom_navbar_player插件中(如果这个插件存在的话)。你需要根据插件的实际文档和API来调整代码。

如果bottom_navbar_player插件不存在,你可能需要自己实现底部导航栏播放器的功能,或者寻找一个类似的第三方插件。在实现自定义功能时,可以考虑使用bottom_navigation_bar和媒体播放控制组件(如chewievideo_player)的组合。

回到顶部