如何从文件管理器打开音频文件并使用 Flutter 应用程序播放该音频文件
How to open audio file from file manager and play that audio file using Flutter app
我是 flutter 开发的新手,我被困在一个程序中,我想通过从文件中选择它来播放音频文件 manager.i 我可以从文件管理器中打开音频文件,但我没有不知道如何播放该音频文件。
这个你能帮我吗。
我正在使用此代码 -
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class FilePickerDemo extends StatefulWidget {
@override
_FilePickerDemoState createState() => _FilePickerDemoState();
}
I am using this for file picking and save file I want to know how to play save file
class _FilePickerDemoState extends State<FilePickerDemo> {
void _pickFiles() async {
_resetState();
try {
_directoryPath = null;
_paths = (await FilePicker.platform.pickFiles(
type: _pickingType,
allowMultiple: _multiPick,
onFileLoading: (FilePickerStatus status) => print(status),
allowedExtensions: ['mp3'],
))
?.files;
} on PlatformException catch (e) {
_logException('Unsupported operation' + e.toString());
} catch (e) {
_logException(e.toString());
}
if (!mounted) return;
setState(() {
_isLoading = false;
_fileName =
_paths != null ? _paths!.map((e) => e.name).toString() : '...';
_userAborted = _paths == null;
});
}
您可以使用 https://pub.dev/packages/just_audio 包来播放音频。
import 'package:just_audio/just_audio.dart';
final _player = AudioPlayer();
playAudio() async {
try {
await _player.setFilePath(widget.audioPath);
} catch (e) {
log("Error loading audio source: $e");
}
_player.play();
}
另一个方便的软件包是 AudioPlayers,它也可以缓存本地音频文件。
这是一个非常基本的示例,其中包含用于播放、暂停和停止对传递到有状态小部件的本地音频的引用的按钮,以帮助您入门(主要基于这篇 Medium 文章)Medium Article
提醒将音频保存在本地文件中,如下所示
并在您的 yaml 文件中添加引用并获取依赖项
assets:
- assets/audio/
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
class AudioManagerExample extends StatefulWidget {
const AudioManagerExample({required this.audioName, Key? key}) : super(key: key);
final String audioName;
@override
_AudioManagerExampleState createState() => _AudioManagerExampleState();
}
class _AudioManagerExampleState extends State<AudioManagerExample> {
AudioPlayer audioPlayer = AudioPlayer();
late AudioCache audioCache;
@override
void initState() {
audioCache = AudioCache(fixedPlayer: audioPlayer);
audioCache.load('audio/${widget.audioName}.mp3');
super.initState();
}
@override
void dispose() {
audioCache.clearAll();
audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
audioCache.play('audio/${widget.audioName}.mp3');
});
},
child: Text('Play ${widget.audioName}'),
),
ElevatedButton(
onPressed: () {
setState(() {
audioPlayer.pause();
});
},
child: Text('Pause ${widget.audioName}'),
),
ElevatedButton(
onPressed: () {
setState(() {
audioPlayer.stop();
});
},
child: Text('Stop ${widget.audioName}'),
),
],
);
}
}
我是 flutter 开发的新手,我被困在一个程序中,我想通过从文件中选择它来播放音频文件 manager.i 我可以从文件管理器中打开音频文件,但我没有不知道如何播放该音频文件。 这个你能帮我吗。 我正在使用此代码 -
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class FilePickerDemo extends StatefulWidget {
@override
_FilePickerDemoState createState() => _FilePickerDemoState();
}
I am using this for file picking and save file I want to know how to play save file
class _FilePickerDemoState extends State<FilePickerDemo> {
void _pickFiles() async {
_resetState();
try {
_directoryPath = null;
_paths = (await FilePicker.platform.pickFiles(
type: _pickingType,
allowMultiple: _multiPick,
onFileLoading: (FilePickerStatus status) => print(status),
allowedExtensions: ['mp3'],
))
?.files;
} on PlatformException catch (e) {
_logException('Unsupported operation' + e.toString());
} catch (e) {
_logException(e.toString());
}
if (!mounted) return;
setState(() {
_isLoading = false;
_fileName =
_paths != null ? _paths!.map((e) => e.name).toString() : '...';
_userAborted = _paths == null;
});
}
您可以使用 https://pub.dev/packages/just_audio 包来播放音频。
import 'package:just_audio/just_audio.dart';
final _player = AudioPlayer();
playAudio() async {
try {
await _player.setFilePath(widget.audioPath);
} catch (e) {
log("Error loading audio source: $e");
}
_player.play();
}
另一个方便的软件包是 AudioPlayers,它也可以缓存本地音频文件。
这是一个非常基本的示例,其中包含用于播放、暂停和停止对传递到有状态小部件的本地音频的引用的按钮,以帮助您入门(主要基于这篇 Medium 文章)Medium Article
提醒将音频保存在本地文件中,如下所示
并在您的 yaml 文件中添加引用并获取依赖项
assets:
- assets/audio/
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
class AudioManagerExample extends StatefulWidget {
const AudioManagerExample({required this.audioName, Key? key}) : super(key: key);
final String audioName;
@override
_AudioManagerExampleState createState() => _AudioManagerExampleState();
}
class _AudioManagerExampleState extends State<AudioManagerExample> {
AudioPlayer audioPlayer = AudioPlayer();
late AudioCache audioCache;
@override
void initState() {
audioCache = AudioCache(fixedPlayer: audioPlayer);
audioCache.load('audio/${widget.audioName}.mp3');
super.initState();
}
@override
void dispose() {
audioCache.clearAll();
audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
audioCache.play('audio/${widget.audioName}.mp3');
});
},
child: Text('Play ${widget.audioName}'),
),
ElevatedButton(
onPressed: () {
setState(() {
audioPlayer.pause();
});
},
child: Text('Pause ${widget.audioName}'),
),
ElevatedButton(
onPressed: () {
setState(() {
audioPlayer.stop();
});
},
child: Text('Stop ${widget.audioName}'),
),
],
);
}
}