“audiolayers”和”audio_service”的播放被其他媒体应用程序中断。

huangapple go评论74阅读模式
英文:

The playback of "audiolayers" and "audio_service" is interrupted by other media applications

问题

我使用audiolayersaudio_service播放音频。它工作得很好。当我打开其他音频应用程序(如Apple Music)来播放音乐时,我的应用程序会停止播放。这没问题。但是当我返回我的应用程序查询当前播放状态时,它仍然在播放。

这是我的代码:

Container(
    height: 60.w,
    width: 60.w,
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(30.w),
        gradient: const LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [Color(0xFFD54381), Color(0xFF7644AD)]),
        ),
        child: StreamBuilder<PlaybackState>(
           stream: customAudioHandler?.playbackState,
           builder: (context, snapshot) {
             final playing = snapshot.data?.playing ?? false;
             return GestureDetector(
                onTap: () => _playOrPause(context, playing),
                  child: Icon(
                    playing ? FontIcons.stop : FontIcons.play,
                    color: Colors.white,
                    size: 40.w,
                 ),
              );
           },
        ),
   ),

我尝试使用WidgetsBindingObserver,如下所示:

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        break;
      case AppLifecycleState.resumed:
        var audioHandlerState = customAudioHandler?.playbackState.value.playing;
        var audioPlayerState = audioPlayer.state;

        // 打印 true
        debugPrint('didChangeAppLifecycleState-resumed: $audioHandlerState'); 

        // 打印 PlayerState.playing
        debugPrint('didChangeAppLifecycleState-resumed: $audioPlayerState'); 

        break;
      case AppLifecycleState.detached:
        break;
    }
    super.didChangeAppLifecycleState(state);
  }

它们的输出状态都是"playing"。

希望这有所帮助。

英文:

I use audiolayers and audio_service plays audio. It works well. When I open other audio applications (such as Apple Music) to play music, my app will stop playing. This is OK. But when I return to my app to query the current playing status, it is still playing.

This is my code:

Container(
    height: 60.w,
    width: 60.w,
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(30.w),
        gradient: const LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [Color(0xFFD54381), Color(0xFF7644AD)]),
        ),
        child: StreamBuilder&lt;PlaybackState&gt;(
           stream: customAudioHandler?.playbackState,
           builder: (context, snapshot) {
             final playing = snapshot.data?.playing ?? false;
             return GestureDetector(
                onTap: () =&gt; _playOrPause(context, playing),
                  child: Icon(
                    playing ? FontIcons.stop : FontIcons.play,
                    color: Colors.white,
                    size: 40.w,
                 ),
              );
           },
        ),
   ),

I try to use WidgetsBindingObserver

such:

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        break;
      case AppLifecycleState.resumed:
        var audioHandlerState = customAudioHandler?.playbackState.value.playing;
        var audioPlayerState = audioPlayer.state;

        // print true
        debugPrint(&#39;didChangeAppLifecycleState-resumed: $audioHandlerState&#39;); 

        // print PlayerState.playing
        debugPrint(&#39;didChangeAppLifecycleState-resumed: $audioPlayerState&#39;); 

        break;
      case AppLifecycleState.detached:
        break;
    }
    super.didChangeAppLifecycleState(state);
  }

Their output status is playing

Any help would be appreciated.

答案1

得分: 0

当另一个应用的音频会话中断您的应用的音频会话时,您会希望以某种方式收到通知,以便您可以更新您的应用状态(在您的情况下,广播一个新的 playbackState,其中 playing=false)。

您可以通过Flutter的 audio_session 包访问音频会话。通过您的会话实例,您可以监听您的会话被另一个应用中断的情况:

session.interruptionEventStream.listen((event) {
  if (event.begin) {
    switch (event.type) {
      case AudioInterruptionType.duck:
        // 在这里您可以降低音量
        break;
      case AudioInterruptionType.pause:
      case AudioInterruptionType.unknown:
        // 在这里您应该暂停您的音频
        player.pause();
        // 并且还要广播新的状态
        playbackState.add(playbackState.value.copyWith(playing: false));
        break;
    }
  }
});

官方的 audio_service 示例也演示了这个用例,尽管它使用了不同的音频播放器插件(just_audio),该插件在底层使用 audio_session 使这个用例更容易实现。

英文:

When your app's audio session is interrupted by another app's audio session, you will want to be notified in some way so that you can update your app's state (in your case, broadcast a new playbackState with playing=false).

You can access the audio session via the Flutter audio_session package. With your instance of the session, you can listen to when your session is interrupted by another app:

session.interruptionEventStream.listen((event) {
  if (event.begin) {
    switch (event.type) {
      case AudioInterruptionType.duck:
        // Here you could lower the volume
        break;
      case AudioInterruptionType.pause:
      case AudioInterruptionType.unknown:
        // Here you should pause your audio
        player.pause();
        // AND ALSO broadcast the new state
        playbackState.add(playbackState.value.copyWith(playing: false));
        break;
    }
  }
});

The official audio_service example also demonstrates this use case, although with a different audio player plugin (just_audio) which uses audio_session under the hood to make this use case easier.

huangapple
  • 本文由 发表于 2023年1月9日 10:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定