【问题标题】:Flutter audio_service play audio in background when mobile screen is lockedFlutter audio_service 在移动屏幕锁定时在后台播放音频
【发布时间】:2021-01-23 02:14:04
【问题描述】:

我用下面的代码制作了一个广播播放器应用程序。打开手机屏幕后一切正常。但是当我关闭手机屏幕时,收音机会在大约 5-8 分钟后停止播放。我得到了一些关于使用颤振音频服务的提示。 (https://pub.dev/packages/audio_service) 但我很困惑我应该从哪里开始。我应该重新编码还是可以修改此代码。有人请帮助我。这将是一种恩典。提前谢谢你。

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

class Radio1 extends StatefulWidget {
  @override
  _Radio1State createState() => _Radio1State();
}

class _Radio1State extends State<Radio1> {
  AudioPlayer audioPlayer = AudioPlayer();

  @override
  void initState() {
    super.initState();
    AudioPlayer.logEnabled = true;
  }

  bool _isPlaying = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              //new
              SizedBox(
                height: 50,
              ),

              //Icon(
              // Icons.arrow_drop_down,
              //size: 40,
              //),

              //new
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20, vertical: 50),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: Color(0x46000000),
                      offset: Offset(0, 20),
                      spreadRadius: 0,
                      blurRadius: 30,
                    ),
                    BoxShadow(
                      color: Color(0x11000000),
                      offset: Offset(0, 10),
                      spreadRadius: 0,
                      blurRadius: 30,
                    ),
                  ],
                ),
//new
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(20),
                  child: Image(
                    image: AssetImage("assets/radiologo.jpg"),
                    width: MediaQuery.of(context).size.width * 0.7,
                    height: MediaQuery.of(context).size.width * 0.7,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              Text(
                "sample text",
                style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500),
              ),
              Text(
                "(sample text)",
                style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
              ),
              /* Slider(
                value: 10,
                onChanged: (v) {},
                max: 170,
                min: 0,
                activeColor: Color(0xFF5E35B1),
              ), */
              Text(
                "sample text.",
                style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
              ),

              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    icon: _isPlaying == false
                        ? Icon(Icons.play_circle_outline)
                        : Icon(Icons.pause_circle_outline),
                    iconSize: 60.0,
                    onPressed: () {
                      getAudio();
                    },
                  ),
                  IconButton(
                    icon: Icon(Icons.stop),
                    iconSize: 40,
                    onPressed: () {
                      stopAudio();
                    },
                  ),
                  //new line
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void getAudio() async {
    var url = "http://ia802708.us.archive.org/3/items/count_monte_cristo_0711_librivox/count_of_monte_cristo_001_dumas.mp3";
    if (_isPlaying) {
      var res = await audioPlayer.pause();
      if (res == 1) {
        setState(() {
          _isPlaying = false;
        });
      }
    } else {
      var res = await audioPlayer.play(url);
      if (res == 1) {
        setState(() {
          _isPlaying = true;
        });
      }
    }
  }

  void stopAudio() async {
    int res = await audioPlayer.stop();

    if (res == 1) {
      setState(() {
        _isPlaying = false;
      });
    }
  }

  void releaseAUdio() async {
    await audioPlayer.stop();
    await audioPlayer.release();
  }

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

【问题讨论】:

    标签: flutter audio radio


    【解决方案1】:

    因此,当您使用audioplayers 包时,您需要实现audio_service 来实现您想要的(在后台播放音频)。确实,audioplayers 包只负责播放音频文件,并不处理后台行为。

    audio_service 旨在成为您应用程序中唯一的真实来源。所以你需要重新架构你的代码以适应。

    但不要删除您的代码,您可能不需要对音频进行太多更改。 包装被切割成多个部分。例如,一个用于后台任务,一个用于 UI 告诉后台任务您想要做什么(播放、暂停、seekTo、...),因此您可能需要在代码中做的唯一更改是调用这部分,称为AudioService(查看API参考以获取更多信息:https://pub.dev/documentation/audio_service/latest/audio_service/AudioService-class.html)。

    一旦你这样做了,你当然必须实现你的后台任务来满足你的需求。

    总结:

    1. 您的代码很好,但没有处理后台行为。
    2. 您可能需要实现 audio_service 包(或类似的包)来处理后台行为。
    3. 请查看audio_session 包,以处理您的应用程序之间的交互和手机上不同的音频交互。 (例如,处理收到的通知并相应降低应用的音量)。

    希望这个答案对你有帮助,祝你好运:)

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多