【问题标题】:Flutter playing music stored in assets folderFlutter 播放存储在 assets 文件夹中的音乐
【发布时间】:2020-05-23 07:10:24
【问题描述】:

我想播放一首存储在我的资产文件夹中的歌曲,但据我研究,只有一种方法,那就是临时文件夹方法。还有其他方法吗? 以下是我尝试使用 audioplayer 包的内容,虽然它使用了 URL,但它仍然无法正常工作。

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
class songPage extends StatefulWidget {
  @override
  _songPageState createState() => _songPageState();
}

AudioPlayer audioPlayer = AudioPlayer();

class _songPageState extends State<songPage> {

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: new BoxDecoration(color: Colors.blue),
               child: Column(
           mainAxisAlignment: MainAxisAlignment.center,

           children: <Widget>[
            Text(
               'Page 2',
               style: TextStyle(
                 fontFamily: 'FlamanteRoma' ,
                 fontSize: 50,
                 color: Colors.white,

               ),
             ),
             RaisedButton(
               child: Icon(Icons.play_arrow),
               onPressed: (){
                  playLocal() async {
                      int result = await audioPlayer.play('assets\audio\lovesong.mp3', isLocal: true);
                    }
                  print('It runs');
               })


           ],
         ),
    );
  }
}

这是我得到的输出:

I/flutter (30875): It runs

但它不播放,可能是应用内音量?(我的设备音量设置为最大)

【问题讨论】:

    标签: flutter avaudioplayer


    【解决方案1】:

    根据docs

    如果您收到 MissingPluginException,请尝试在 Android 上flutter build apk,或flutter build ios

    【讨论】:

    • 试过了还是不行,用了新包还是不行
    【解决方案2】:

    您是否将资产包含在 pubspec.yaml 中?

    flutter:
      assets:
        - assets/
    

    【讨论】:

    • 是的,还是什么都没有
    • 你用的是哪个插件?
    • 音频播放器和音频播放器
    【解决方案3】:

    为了只播放一首歌曲,我设法使用音频缓存,只需在实现后声明音频缓存,然后声明路径,最后在 onpressee 按钮中说 audiocache.play

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多