如何在 Flutter 中播放音频

问题描述 投票:0回答:1

在我的扑动应用程序中,我想制作一个音频播放器,这是我的代码,但问题是,当我单击图标按钮时,没有任何反应,并且音频无法启动,我该如何解决这个问题

import 'dart:async';
import 'dart:ui';
import 'package: flutter/cupertino.dart';
import 'package: flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:google_fonts/google_fonts.dart';

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

  @override
  State<CalmForest> createState() => _CalmForestState();
}

class _CalmForestState extends State<CalmForest> {
  //const CalmForest({super.key});
  final AudioPlayer player = AudioPlayer();
  void playAudio() async {
    final audioPlayer = AudioPlayer();
    final audioSource = AssetSource('assets/audio/Forest.mp3');
    await audioPlayer.play(audioSource);
  }

音频来自资产

我正在使用音频播放器:^6.0.0 来播放音频


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/forest.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 8, sigmaY: 8),
          child: ClipRect(
            child: Container(
              color: Colors.black.withOpacity(0.5),
              child: Stack(
                children: [
                  Center(
                    child: Container(
                      width: 250,
                      height: 300,
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              image: AssetImage('assets/forest.png'))),
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Icon(Icons.graphic_eq,
                                size: 30, color: Colors.white),
                            SizedBox(
                              width: 25,
                            ),
                            Text(
                              "Forest",
                              style: GoogleFonts.merriweather(
                                color: Colors.white,
                                fontSize: 25,
                              ),
                            ),
                            SizedBox(
                              width: 25,
                            ),
                            Icon(Icons.graphic_eq,
                                size: 30, color: Colors.white),
                          ],
                        ),
                        IconButton(
                            onPressed: () {
                              playAudio();
                            },
                            icon: Icon(
                              Icons.play_circle,
                              size: 60,
                              color: Colors.white,
                            )),
                        SizedBox(
                          height: 120,
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

我怎样才能播放声音,你能帮我吗?这就是整个问题

flutter dart flutter-dependencies audio-player
1个回答
0
投票

https://pub.dev/packages/audio_waveforms

您可以使用此包来播放音频:

此博客将指导您如何使用此包播放音频

https://blog.logrocket.com/audio-waveforms-flutter-app/#playing-audio

© www.soinside.com 2019 - 2024. All rights reserved.