Flutter音频播放插件pax_player的使用

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

Flutter音频播放插件pax_player的使用

重要提示

此插件仅在Android上运行!

安装

要将 pax_player 插件添加到您的项目中,请在 pubspec.yaml 文件中执行以下命令:

flutter pub add pax_player

设置minify和shrink

在您的 build.gradle 文件中添加以下行:

buildTypes {
    release {
        .
        .
        .
        minifyEnabled false
        shrinkResources false
    }
}

在AndroidManifest.xml中设置

在您的 AndroidManifest.xml 文件中添加以下行:

<application
...
android:name="pax.plugin.pki.parhamkish.vakili.pax_player.PaxApp"
...
</application>

此插件的功能

  • [✅] 获取设备信息 => 使用 getDeviceInfo
  • [✅] 获取卡号信息 => 使用 getCardNumber
  • [✅] 获取蜂鸣器设备 => 使用 getBeep
  • [✅] 启用设备导航栏 => 使用 setEnableNavBar 并设置为 true
  • [✅] 获取条形码信息 => 使用 getScanBarcode
  • ❌ 获取所有设备信息
  • ❌ 调用打印机功能
  • ❌ 设备设置
  • ❌ 调用所有扫描方法

如果您有Pax终端打印机并需要帮助集成过程,请联系我

测试设备

  • PAX A920

如果您喜欢此插件,请支持我

如果您想支持此插件,您可以 捐赠

示例代码

以下是使用 pax_player 插件的基本示例代码:

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pax_player/pax_player.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Pax Player Demo',
      debugShowCheckedModeBanner: false,
      home: MainPax(),
    );
  }
}

class MainPax extends StatelessWidget {
  const MainPax({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pax Player'),
      ),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const SignBarcode()));
                },
                child: const Text('获取条形码')),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const SignCard()));
                },
                child: const Text('获取卡号')),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const ScanCard()));
                },
                child: const Text('扫描卡号')),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const ScanBarcode()));
                },
                child: const Text('扫描条形码')),
          ],
        ),
      ),
    );
  }
}

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

  [@override](/user/override)
  State<SignBarcode> createState() => _SignBarcodeState();
}

class _SignBarcodeState extends State<SignBarcode> {
  printQrCode() async {
    await PaxPlayer().getScanBarcode();
    await PaxPlayer().getBeep(100);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('获取条形码'),
      ),
      body: Column(
        children: [
          const Text('请输入信息:'),
          ElevatedButton(
              onPressed: () => printQrCode(), child: const Text('获取条形码')),
        ],
      ),
    );
  }
}

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

  [@override](/user/override)
  State<SignCard> createState() => _SignCardState();
}

class _SignCardState extends State<SignCard> {
  String _cardNumber = '请插入卡片';

  Future<void> _getCardNumber() async {
    String? result = await PaxPlayer().getCardNumber();
    if (result != null) {
      setState(() {
        _cardNumber = '操作成功';
      });
    } else {
      setState(() {
        _cardNumber = '操作失败';
      });
    }
    await PaxPlayer().getBeep(100);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('获取卡号'),
      ),
      body: Column(
        children: [
          const Text('请输入信息:'),
          ElevatedButton(
              onPressed: () {
                _getCardNumber();
              },
              child: const Text('获取卡号')),
          const SizedBox(height: 30),
          Text(_cardNumber),
        ],
      ),
    );
  }
}

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

  [@override](/user/override)
  State<ScanCard> createState() => _ScanCardState();
}

class _ScanCardState extends State<ScanCard> {
  String _cardNumber = '请插入卡片';

  Future<void> _getCardNumber() async {
    String? result = await PaxPlayer().getCardNumber();
    if (result != null) {
      setState(() {
        _cardNumber = '操作成功';
      });
    } else {
      setState(() {
        _cardNumber = '操作失败';
      });
    }
    await PaxPlayer().getBeep(100);
    Future.delayed(const Duration(milliseconds: 500), _getCardNumber);
  }

  [@override](/user/override)
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      _getCardNumber();
    });
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('扫描卡片'),
      ),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(_cardNumber),
          ],
        ),
      ),
    );
  }
}

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

  [@override](/user/override)
  State<ScanBarcode> createState() => _ScanBarcodeState();
}

class _ScanBarcodeState extends State<ScanBarcode> {
  String _barcodeData = '请扫描条形码';

  Future<void> _getBarcode() async {
    String? result = await PaxPlayer().getScanBarcode();
    if (result != null) {
      setState(() {
        _barcodeData = '操作成功';
      });
    } else {
      setState(() {
        _barcodeData = '操作失败';
      });
    }
    await PaxPlayer().getBeep(100);
  }

  [@override](/user/override)
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      _getBarcode();
    });
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () => _getBarcode(),
        child: const Icon(Icons.qr_code_scanner_outlined),
      ),
      appBar: AppBar(
        title: const Text('扫描条形码'),
      ),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(_barcodeData),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter音频播放插件pax_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter音频播放插件pax_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用pax_player插件进行音频播放的示例代码。pax_player是一个强大的音频播放插件,支持多种音频格式和播放控制功能。

1. 添加依赖

首先,你需要在pubspec.yaml文件中添加pax_player依赖:

dependencies:
  flutter:
    sdk: flutter
  pax_player: ^最新版本号  # 请替换为实际最新版本号

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

2. 导入插件

在你的Dart文件中导入pax_player插件:

import 'package:pax_player/pax_player.dart';

3. 初始化和使用PaxPlayer

以下是一个完整的示例,展示如何初始化PaxPlayer,加载音频文件,并进行播放控制:

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

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

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

class AudioPlayerScreen extends StatefulWidget {
  @override
  _AudioPlayerScreenState createState() => _AudioPlayerScreenState();
}

class _AudioPlayerScreenState extends State<AudioPlayerScreen> {
  PaxPlayer? _player;

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

  void _initPlayer() {
    _player = PaxPlayer();
    _player!.init();
  }

  void _playAudio(String url) async {
    if (_player!.isPlaying) {
      _player!.pause();
    } else {
      await _player!.play(url);
    }
  }

  void _stopAudio() {
    _player!.stop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pax Player Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                _playAudio('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3');
              },
              child: Text('Play Audio'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                _stopAudio();
              },
              child: Text('Stop Audio'),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _player?.dispose();
    super.dispose();
  }
}

4. 注意事项

  • 确保音频URL是有效的,并且服务器允许跨域访问(CORS)。
  • 在实际项目中,可能需要添加更多的错误处理和状态管理。
  • PaxPlayer提供了丰富的API,如获取播放状态、当前播放时间、音量控制等,你可以根据需求进一步扩展功能。

这个示例展示了基本的音频播放和停止功能,你可以根据PaxPlayer的文档探索更多高级功能。

回到顶部