微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – Flutter App与背景音乐(外部)

在后台运行的应用程序(即Google Play音乐,Apple Music,Spotify等)上播放音乐时,如果您在颤动应用中播放任何音频,则背景音乐会停止/暂停,并且在音频完成后不会重新启动播放.

我已经尝试使用iOS上的TTS库和AudioPlayer库.

关于如何在我的音频完成后恢复播放背景音频的任何提示(这实际上只是一个2/3秒的短语)?

尚不确定这是否会影响Android.

注意:在此示例中使用TTS,但它发生在我尝试过的每个库中.

Pubspec.yaml

tts: "^1.0.1"

码:

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

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.red,
      ),
      home: new AudioExample(),
    );
  }
}

class AudioExample extends StatefulWidget {
  @override
  _AudioExampleState createState() => _AudioExampleState();
}

class _AudioExampleState extends State<AudioExample> {

  speak() async {
    Tts.speak('Hello World');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('Audio example app'),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          color: Colors.grey[300],
        ),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.play_arrow),
              onPressed: speak,
              color: Colors.red,
              iconSize: 100.0,
            ),
            new Padding(
              padding: const EdgeInsets.all(20.0),
              child: Text(
                  'Will say hello world once clicked and pause background audio on iOS - how to restart background audio after playing has finished?',
                textAlign: TextAlign.center,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

解决方法:

你可以在Java文件中添加音乐而不是颤动并添加循环转到android / app / src / main / java / com / example / ProjectName / MainActivity.java

并添加

  mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.MusicName);
  mediaPlayer.setLooping(true);
  mediaPlayer.start();
  mediaPlayer.setLooping(true);

在onCreate方法中,转到名为res的文件夹,在其中创建一个名为raw的文件夹,并在其中输入您的音乐

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐