dart 当导航到其他屏幕时,Flutter Just Audio Background未播放音频

fivyi3re  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(206)

我使用的只是音频后台包在后台播放音频。正如你所看到的,我使用的是酒吧开发上的示例代码,因为我刚刚修改了它。
问题是当我导航到另一个屏幕时,后台的音频停止了。而且,通知也消失了。我试图删除_player.dispose();方法,这是一个音频播放器的示例,然后它的工作,但现在的音频是不会改变,当我点击另一首歌。

import 'dart:io';
import 'package:audio_session/audio_session.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:uri_to_file/uri_to_file.dart';
import 'package:video_downloader/utils/common.dart';
import 'package:rxdart/rxdart.dart';

        String songURIString = "";
        int? songDurationValue = 0;
        String songTitleString = "";

        class AudioPlayer3 extends StatefulWidget {
          var songDurationSec, songTitleString, songUriString, albumTitle;
          AudioPlayer3(
              {Key? key,
              this.songDurationSec,
              this.songUriString,
              this.songTitleString,
              this.albumTitle})
              : super(key: key);

          @override
          AudioPlayer3State createState() => AudioPlayer3State();
        }

        class AudioPlayer3State extends State<AudioPlayer3> {
          static int _nextMediaId = 0;
          late AudioPlayer _player;
          late final _playlist = ConcatenatingAudioSource(children: [
            AudioSource.uri(
              Uri.parse("${widget.songUriString}"),
              tag: MediaItem(
                id: '${widget.songUriString}',
                album: "${widget.albumTitle}",
                title: "${widget.songTitleString}",
                artUri: Uri.parse("https://www.linkpicture.com/q/music-note.png"),
              ),
            ),
          ]);
          int _addedCount = 0;

          @override
          void initState() {
            super.initState();
            _player = AudioPlayer();

            WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
              _init();
              setArtUri();
              setAudioValues();
            });
          }

          Future<void> initNotification() async {
            await JustAudioBackground.init(
              androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio',
              androidNotificationChannelName: 'Audio playback',
              androidNotificationOngoing: true,
            );
          }

          Future<void> _init() async {
            final session = await AudioSession.instance;
            await session.configure(const AudioSessionConfiguration.speech());
            // Listen to errors during playback.
            _player.playbackEventStream.listen((event) {},
                onError: (Object e, StackTrace stackTrace) {
              print('A stream error occurred: $e');
            });
            try {
              await _player.setAudioSource(_playlist);
            } catch (e, stackTrace) {
              // Catch load errors: 404, invalid url ...
              print("Error loading playlist: $e");
              print(stackTrace);
            }
          }

          Future<void> setAudioValues() async {
            setState(() {
              songTitleString = widget.songTitleString;
              songURIString = widget.songUriString;
              songDurationValue = widget.songDurationSec;
              print('Song name is ' + songTitleString);
            });
          }

          Future<void> setArtUri() async {
            File file = await toFile(''); // Converting uri to file
          }

          @override
          void dispose() {
            _player.dispose();
            super.dispose();
          }

          Stream<PositionData> get _positionDataStream =>
              Rx.combineLatest3<Duration, Duration, Duration?, PositionData>(
                  _player.positionStream,
                  _player.bufferedPositionStream,
                  _player.durationStream,
                  (position, bufferedPosition, duration) => PositionData(
                      position, bufferedPosition, duration ?? Duration.zero));

          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              debugShowCheckedModeBanner: false,
              home: Scaffold(
                body: SafeArea(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                        child: StreamBuilder<SequenceState?>(
                          stream: _player.sequenceStateStream,
                          builder: (context, snapshot) {
                            final state = snapshot.data;
                            if (state?.sequence.isEmpty ?? true) {
                              return const SizedBox();
                            }
                            final metadata = state!.currentSource!.tag as MediaItem;
                            return Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Expanded(
                                  child: Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Center(
                                        child:
                                            Image.network(metadata.artUri.toString())),
                                  ),
                                ),
                                Text(metadata.album!,
                                    style: Theme.of(context).textTheme.headline6),
                                Text(metadata.title),
                              ],
                            );
                          },
                        ),
                      ),
                      ControlButtons(_player),
                      StreamBuilder<PositionData>(
                        stream: _positionDataStream,
                        builder: (context, snapshot) {
                          final positionData = snapshot.data;
                          return SeekBar(
                            duration: positionData?.duration ?? Duration.zero,
                            position: positionData?.position ?? Duration.zero,
                            bufferedPosition:
                                positionData?.bufferedPosition ?? Duration.zero,
                            onChangeEnd: (newPosition) {
                              _player.seek(newPosition);
                            },
                          );
                        },
                      ),
                      const SizedBox(height: 8.0),
                      Row(
                        children: [
                          StreamBuilder<LoopMode>(
                            stream: _player.loopModeStream,
                            builder: (context, snapshot) {
                              final loopMode = snapshot.data ?? LoopMode.off;
                              const icons = [
                                Icon(Icons.repeat, color: Colors.grey),
                                // Icon(Icons.repeat, color: Colors.orange),
                                Icon(Icons.repeat_one, color: Colors.orange),
                              ];
                              const cycleModes = [
                                LoopMode.off,
                                // LoopMode.all,
                                LoopMode.one,
                              ];
                              final index = cycleModes.indexOf(loopMode);
                              return IconButton(
                                icon: icons[index],
                                onPressed: () {
                                  _player.setLoopMode(cycleModes[
                                      (cycleModes.indexOf(loopMode) + 1) %
                                          cycleModes.length]);
                                },
                              );
                            },
                          ),
                          Expanded(
                            child: Text(
                              "Now Playing",
                              style: Theme.of(context).textTheme.headline6,
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 240.0,
                        child: StreamBuilder<SequenceState?>(
                          stream: _player.sequenceStateStream,
                          builder: (context, snapshot) {
                            final state = snapshot.data;
                            final sequence = state?.sequence ?? [];
                            return ReorderableListView(
                              onReorder: (int oldIndex, int newIndex) {
                                if (oldIndex < newIndex) newIndex--;
                                _playlist.move(oldIndex, newIndex);
                              },
                              children: [
                                for (var i = 0; i < sequence.length; i++)
                                  Dismissible(
                                    key: ValueKey(sequence[i]),
                                    background: Container(
                                      color: Colors.redAccent,
                                      alignment: Alignment.centerRight,
                                      child: const Padding(
                                        padding: EdgeInsets.only(right: 8.0),
                                        child: Icon(Icons.delete, color: Colors.white),
                                      ),
                                    ),
                                    onDismissed: (dismissDirection) {
                                      _playlist.removeAt(i);
                                    },
                                    child: Material(
                                      color: i == state!.currentIndex
                                          ? Colors.grey.shade300
                                          : null,
                                      child: ListTile(
                                        title: Text(sequence[i].tag.title as String),
                                        onTap: () {
                                          _player.seek(Duration.zero, index: i);
                                        },
                                      ),
                                    ),
                                  ),
                              ],
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          }
        }

        class ControlButtons extends StatelessWidget {
          final AudioPlayer player;

          const ControlButtons(this.player, {Key? key}) : super(key: key);

          @override
          Widget build(BuildContext context) {
            return Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                IconButton(
                  icon: const Icon(Icons.volume_up),
                  onPressed: () {
                    showSliderDialog(
                      context: context,
                      title: "Adjust volume",
                      divisions: 10,
                      min: 0.0,
                      max: 1.0,
                      stream: player.volumeStream,
                      onChanged: player.setVolume,
                      value: 5,
                    );
                  },
                ),
                StreamBuilder<PlayerState>(
                  stream: player.playerStateStream,
                  builder: (context, snapshot) {
                    final playerState = snapshot.data;
                    final processingState = playerState?.processingState;
                    final playing = playerState?.playing;
                    if (processingState == ProcessingState.loading ||
                        processingState == ProcessingState.buffering) {
                      return Container(
                        margin: const EdgeInsets.all(8.0),
                        width: 64.0,
                        height: 64.0,
                        child: const CircularProgressIndicator(),
                      );
                    } else if (playing != true) {
                      return IconButton(
                        icon: const Icon(Icons.play_arrow),
                        iconSize: 64.0,
                        onPressed: player.play,
                      );
                    } else if (processingState != ProcessingState.completed) {
                      return IconButton(
                        icon: const Icon(Icons.pause),
                        iconSize: 64.0,
                        onPressed: player.pause,
                      );
                    } else {
                      return IconButton(
                        icon: const Icon(Icons.replay),
                        iconSize: 64.0,
                        onPressed: () => player.seek(Duration.zero,
                            index: player.effectiveIndices!.first),
                      );
                    }
                  },
                ),
                StreamBuilder<double>(
                  stream: player.speedStream,
                  builder: (context, snapshot) => IconButton(
                    icon: Text("${snapshot.data?.toStringAsFixed(1)}x",
                        style: const TextStyle(fontWeight: FontWeight.bold)),
                    onPressed: () {
                      showSliderDialog(
                        context: context,
                        title: "Adjust speed",
                        divisions: 10,
                        min: 0.5,
                        max: 1.5,
                        stream: player.speedStream,
                        onChanged: player.setSpeed,
                        value: 5,
                      );
                    },
                  ),
                ),
              ],
            );
          }
        }
h9vpoimq

h9vpoimq1#

如果您使用一列流生成器来生成音频播放器,您仍然可以通过使用提供程序包管理音频播放器状态来跨多个屏幕维护音频播放器的状态。以下是实现此操作的步骤:
1.通过向pubspec. yaml文件中添加以下行,将提供程序包添加到项目中:

dependencies:
  provider: ^6.0.0

1.创建一个扩展ChangeNotifier类的新AudioPlayerProvider类。此类将包含音频播放器状态。

class AudioPlayerProvider extends ChangeNotifier {
  final AudioPlayer _audioPlayer = AudioPlayer();
  bool _isPlaying = false;

  bool get isPlaying => _isPlaying;

  Future<void> play(String url) async {
    await _audioPlayer.setUrl(url);
    await _audioPlayer.play();
    _isPlaying = true;
    notifyListeners();
  }

  Future<void> pause() async {
    await _audioPlayer.pause();
    _isPlaying = false;
    notifyListeners();
  }

  Future<void> stop() async {
    await _audioPlayer.stop();
    _isPlaying = false;
    notifyListeners();
  }

  Future<void> dispose() async {
    await _audioPlayer.stop();
    await _audioPlayer.dispose();
    super.dispose();
  }
}

1.使用ChangeNotifierProvider小部件 Package 应用的根小部件,并传入AudioPlayerProvider类的示例。

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => AudioPlayerProvider(),
      child: MyApp(),
    ),
  );
}

1.在音频播放器小部件中,使用Provider包访问AudioPlayerProvider示例并控制音频播放器。

class AudioPlayerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final audioProvider = Provider.of<AudioPlayerProvider>(context);

    return Column(
      children: [
        StreamBuilder<Duration>(
          // audio player stream builder
          stream: audioProvider.audioPlayer.positionStream,
          builder: (context, snapshot) {
            // build audio player UI
          },
        ),
        ElevatedButton(
          // play/pause button
          child: audioProvider.isPlaying ? Icon(Icons.pause) : Icon(Icons.play_arrow),
          onPressed: () {
            audioProvider.isPlaying ? audioProvider.pause() : audioProvider.play(url);
          },
        ),
      ],
    );
  }
}

1.当导航到新屏幕时,使用Navigator. push方法将新屏幕压入堆栈。

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => NewScreen(),
  ),
);

如果在dispose()方法中使用_player. pause()后仍遇到just_audio_background仅支持单个播放器示例的错误,则可能是应用中的某处仍存在对AudioPlayer示例的引用,导致无法正确处置该示例。
要解决此问题,可以尝试以下步骤:
1.检查您的应用中是否存在可能与您尝试使用的AudioPlayer小部件发生冲突的任何其他AudioPlayer小部件示例。如果您有多个AudioPlayer小部件示例,则可能会导致冲突并导致此错误。请确保您仅使用AudioPlayer小部件的一个示例。
1.导航到另一个屏幕时,请使用Navigator. pushReplacement()方法而不是Navigator. push()。这将用新屏幕替换当前屏幕,并确保在加载新屏幕之前正确释放AudioPlayer示例。
1.请确保在整个应用中正确处理AudioPlayer示例的生命周期。请确保在不再需要该示例时正确停止并处置它。
1.请考虑使用Provider或Bloc等状态管理解决方案来管理AudioPlayer示例,并确保只创建一次该示例,并在不再需要该示例时将其正确处置。
以下是如何实施这些步骤的示例:

class MyAudioPlayer extends StatefulWidget {
  @override
  _MyAudioPlayerState createState() => _MyAudioPlayerState();
}

class _MyAudioPlayerState extends State<MyAudioPlayer> {
  late AudioPlayer _player;

  @override
  void initState() {
    super.initState();
    if (_player == null) {
      _player = AudioPlayer();
      _player.setUrl('url_to_audio_file');
    }
  }

  @override
  void dispose() {
    _player.pause();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        StreamBuilder<AudioPlaybackState>(
          stream: _player.playbackStateStream,
          builder: (context, snapshot) {
            final playbackState = snapshot.data;
            final playing = playbackState?.playing ?? false;
            return Text(
              playing ? 'Playing' : 'Paused',
            );
          },
        ),
        ElevatedButton(
          onPressed: () {
            _player.play();
          },
          child: Text('Play'),
        ),
        ElevatedButton(
          onPressed: () {
            _player.pause();
          },
          child: Text('Pause'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                builder: (context) => AnotherScreen(),
              ),
            );
          },
          child: Text('Go to another screen'),
        ),
      ],
    );
  }
}

通过正确处理AudioPlayer示例的生命周期并避免与其他示例发生冲突,您应该能够解决该错误并播放音频,而不会出现任何问题。

相关问题