我正在使用“just_audio”依赖项处理Flutter,在尝试播放本地文件时出现错误。我已经通过添加资产对pubspec进行了更改,验证了路径“assets/music/song1.mp3”,但音乐播放不起作用。我得到的错误是播放器找不到文件。
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
class AudioPlayerScreen extends StatefulWidget {
const AudioPlayerScreen({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_AudioPlayerScreenState createState() => _AudioPlayerScreenState();
}
class _AudioPlayerScreenState extends State<AudioPlayerScreen> {
late AudioPlayer audioPlayer;
bool isPlaying = false;
@override
void initState() {
audioPlayer.setAsset('assets/music/song1.mp3');
super.initState();
audioPlayer = AudioPlayer();
}
@override
void dispose() {
audioPlayer.dispose();
super.dispose();
}
void playMusic() async {
audioPlayer.play();
setState(() {
isPlaying = true;
});
}
void pauseMusic() {
audioPlayer.pause();
setState(() {
isPlaying = false;
});
}
void stopMusic() {
audioPlayer.stop();
setState(() {
isPlaying = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Reproductor de audio'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
isPlaying
? ElevatedButton(
onPressed: pauseMusic,
child: const Icon(Icons.pause_circle),
)
: ElevatedButton(
onPressed: playMusic,
child: const Icon(Icons.play_circle),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: stopMusic,
child: const Icon(Icons.stop_circle),
),
],
),
),
);
}
}
name: playlist_app
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: '>=2.19.6 <3.0.0'
dependencies:
cupertino_icons: ^1.0.2
flutter:
sdk: flutter
just_audio: ^0.9.33
dev_dependencies:
flutter_lints: ^2.0.0
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/music/
我想要一种简单的东西,当我按下按钮时,它就会播放音乐,当我暂停它时,它就会停止。就这样
1条答案
按热度按时间lndjwyie1#
我刚改了这个