几乎花了一天的时间来思考如何做到这一点,我需要你的帮助。
我创建了一个应用程序,该应用程序的状态为audioplayers和 using的实例flutter_bloc。
MusicPlayer不会使用以下命令重建小部件BlocBuildercurrentPosition我duration正在播放的音乐并使用包LinearPercentIndicator中的内容显示它linear_percent_indicator,但似乎找不到解决方案,因为重建不起作用。这是我到目前为止所拥有的:
class AudioPlayerBloc extends Bloc<AudioPlayerEvents, MusicPlayerState> {
@override
MusicPlayerState get initialState => MusicPlayerState(
player: AudioPlayer(),
episode: Episode(),
);
@override
Stream<MusicPlayerState> mapEventToState(AudioPlayerEvents event) async* {
if (event is InitializePlayer) {
this.currentState.episode = event.episode;
this.dispatch(PlayPlayer());
yield this.currentState;
}
if (event is PlayPlayer) {
this.play(this.currentState.episode.source);
}
if (event is PlayRemote) {
this.currentState.player.stop();
this.currentState.player.play(event.remoteURL);
yield this.currentState;
}
if (event is ShowPlayer) …Run Code Online (Sandbox Code Playgroud) 我正在使用 audioplayers: 1.1.0插件从 url 播放音频。玩得很好。但在player.onPositionChanged特定秒数之后,它会重新启动,计时器从 0 开始。这样我的滑块又从 0 开始。
player.onPositionChanged.listen((newPosition) {
prints(newPosition);
});
Run Code Online (Sandbox Code Playgroud)
输出:
0:00:00.000000
0:00:00.002000
0:00:00.203000
0:00:00.405000
0:00:00.607000
0:00:00.810000
0:00:01.013000
0:00:01.215000
0:00:02.027000
0:00:02.232000
0:00:03.863000
0:00:04.882000
0:00:00.236000 // after some seconds again returns from 0
0:00:00.438000
0:00:00.641000
0:00:01.052000
0:00:01.662000
0:00:01.864000
Run Code Online (Sandbox Code Playgroud)
当这个函数再次从 0 返回时,Slider 也会重新启动,如下所示
这是整个代码
音频播放器:1.1.0
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'dart:async';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData( …Run Code Online (Sandbox Code Playgroud) 我正在创建一个 Flutter 插件。目前,代码:
当我在 Android 中运行该应用程序时,它完美运行。然而,当我在 iOS 上运行它时,它却没有(假设涉及权限/限制)
await flutterTts
.synthesizeToFile(
"This is my first audio synthesizer in Flutter", audioFileName)
.then((success) => {
if (success == 1)
{
print('Successfully synthesized!!'),
// Gets the path to the generated file depending on the platform
pathFile =
Platform.isAndroid ? pathToFileAndroid : pathToFileIos,
pathFile
.then((pathToAudioFile) => {
print('Path to file: ' + pathToAudioFile),
// Speakers/earpiece
// int result = await player.earpieceOrSpeakersToggle();
// Sets the path to the file
audioPlayer.setUrl(pathToAudioFile),
audioPlayer.setVolume(1.0), …Run Code Online (Sandbox Code Playgroud)