AudioPlayers на Flutter: я не могу играть mp3 один за другим - PullRequest
0 голосов
/ 17 февраля 2020

Я застрял на несколько дней в поисках решения и не могу его найти. Мне нужно проигрывать mp3 файлы один за другим, используя AudioPlayers, и это невозможно для меня. Я использую единственный инструмент (насколько я знаю), который дает вам свободу делать подобные вещи, но я не могу найти, как это сделать. Спасибо за помощь.

playLocal(List list) async {

list.forEach((f) async {
  final file = new File('${(await getTemporaryDirectory()).path}/voice.mp3');
  await file.writeAsBytes((await loadAsset(f)).buffer.asUint8List());
  bool finish = false;

  if (player.state == null || player.state == AudioPlayerState.COMPLETED) {
    await player.play(file.path, isLocal:true);
  }

  if (player.state != null && player.state != AudioPlayerState.COMPLETED) {
    while(!finish){
      player.onPlayerCompletion.listen((event){
        finish = true;
      });
      if (finish) {await player.play(file.path, isLocal:true);}
    }
  }

});

}

1 Ответ

0 голосов
/ 18 февраля 2020

Вы можете использовать пакет https://pub.dev/packages/assets_audio_player
В assetsAudioPlayer.finished.listen вызов _next()

фрагмент кода

void _next() {
    if (_assetsAudioPlayer.playlist != null) {
      _assetsAudioPlayer.playlistNext();
    } else {
      _currentAssetPosition++;
      _open(_currentAssetPosition);
    }
  }   
@override
      void initState() {
        super.initState();
        _assetsAudioPlayer.finished.listen((finished) {
          print("paly next");
          _next();
        });
      }

рабочая демонстрация после song1.mp3 fini sh auto play song2.mp3

enter image description here

полный код

import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:assets_audio_player_example/asset_audio_player_icons.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final assets = <String>[
    "assets/audios/song1.mp3",
    "assets/audios/song2.mp3",
    "assets/audios/song3.mp3",
  ];
  final AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();

  var _currentAssetPosition = -1;

  void _open(int assetIndex) {
    _currentAssetPosition = assetIndex % assets.length;
    _assetsAudioPlayer.open(assets[_currentAssetPosition]);
  }

  void _playPause() {
    _assetsAudioPlayer.playOrPause();
  }

  void _next() {
    if (_assetsAudioPlayer.playlist != null) {
      _assetsAudioPlayer.playlistNext();
    } else {
      _currentAssetPosition++;
      _open(_currentAssetPosition);
    }
  }

  void _prev() {
    if (_assetsAudioPlayer.playlist != null) {
      _assetsAudioPlayer.playlistPrevious();
    } else {
      _currentAssetPosition--;
      _open(_currentAssetPosition);
    }
  }

  @override
  void initState() {
    super.initState();
    _assetsAudioPlayer.finished.listen((finished) {
      print("paly next");
      _next();
    });
  }

  @override
  void dispose() {
    _assetsAudioPlayer.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Padding(
          padding: const EdgeInsets.only(bottom: 48.0),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  _assetsAudioPlayer
                      .openPlaylist(Playlist(assetAudioPaths: this.assets));
                },
                child: Text("Playlist test"),
              ),
              Expanded(
                child: StreamBuilder(
                  stream: _assetsAudioPlayer.current,
                  initialData: const PlayingAudio(),
                  builder: (BuildContext context,
                      AsyncSnapshot<PlayingAudio> snapshot) {
                    final PlayingAudio currentAudio = snapshot.data;
                    return ListView.builder(
                      itemBuilder: (context, position) {
                        return ListTile(
                            title: Text(assets[position].split("/").last,
                                style: TextStyle(
                                    color: assets[position] ==
                                            currentAudio.assetAudioPath
                                        ? Colors.blue
                                        : Colors.black)),
                            onTap: () {
                              _open(position);
                            });
                      },
                      itemCount: assets.length,
                    );
                  },
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  StreamBuilder(
                    stream: _assetsAudioPlayer.isLooping,
                    initialData: false,
                    builder:
                        (BuildContext context, AsyncSnapshot<bool> snapshot) {
                      return RaisedButton(
                        child: Text(snapshot.data ? "Looping" : "Not looping"),
                        onPressed: () {
                          _assetsAudioPlayer.toggleLoop();
                        },
                      );
                    },
                  ),
                  SizedBox(width: 20),
                  RaisedButton(
                    child: Text("Seek to 2:00"),
                    onPressed: () {
                      _assetsAudioPlayer.seek(Duration(minutes: 2));
                    },
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  StreamBuilder(
                    stream: _assetsAudioPlayer.currentPosition,
                    initialData: const Duration(),
                    builder: (BuildContext context,
                        AsyncSnapshot<Duration> snapshot) {
                      Duration duration = snapshot.data;
                      return Text(durationToString(duration));
                    },
                  ),
                  Text(" - "),
                  StreamBuilder(
                    stream: _assetsAudioPlayer.current,
                    builder: (BuildContext context,
                        AsyncSnapshot<PlayingAudio> snapshot) {
                      Duration duration = Duration();
                      if (snapshot.hasData) {
                        duration = snapshot.data.duration;
                      }
                      return Text(durationToString(duration));
                    },
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: [
                  IconButton(
                    onPressed: _prev,
                    icon: Icon(AssetAudioPlayerIcons.to_start),
                  ),
                  StreamBuilder(
                    stream: _assetsAudioPlayer.isPlaying,
                    initialData: false,
                    builder:
                        (BuildContext context, AsyncSnapshot<bool> snapshot) {
                      return IconButton(
                        onPressed: _playPause,
                        icon: Icon(snapshot.data
                            ? AssetAudioPlayerIcons.pause
                            : AssetAudioPlayerIcons.play),
                      );
                    },
                  ),
                  IconButton(
                    icon: Icon(AssetAudioPlayerIcons.to_end),
                    onPressed: _next,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

String durationToString(Duration duration) {
  String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  String twoDigitMinutes =
      twoDigits(duration.inMinutes.remainder(Duration.minutesPerHour));
  String twoDigitSeconds =
      twoDigits(duration.inSeconds.remainder(Duration.secondsPerMinute));
  return "$twoDigitMinutes:$twoDigitSeconds";
}
...