Imaan Tahir
Imaan Tahir

Reputation: 1

Unable to play background audio in flutter even when the app is closed

I'm trying to implement background audio playback in my Flutter app. My app has videos that I can watch, but I want the audio to keep playing even when I close the app. I also want to control the audio using the media controls in the notification bar, like play, pause, forward, and backward. I'm using audio_service, just_audio, and audio_session, but I can't get it to work. I've set everything up correctly with permissions and configuration, but it's still not working. Can anyone suggest how to fix this or guide me on what to do? I want to implement functionality similar to YouTube Premium, where users can control media playback even after closing the app. The media controls should be available in the control center on both Android and iOS. However, I am facing an issue: the media controls appear on both Android and iOS, but on iOS, they are just appearing without functioning. On Android, I am only able to pause the video from the media controls but unable to play it again.

following is my audio_handler file

import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';

import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:flutter/cupertino.dart';
import 'package:just_audio/just_audio.dart';
import 'package:pod_player/pod_player.dart';

class MyAudioHandler extends BaseAudioHandler with SeekHandler {
  final player = AudioPlayer();

  MyAudioHandler() {
    player.playbackEventStream.map(_transformEvent).pipe(playbackState);;
  }
  Future<void> play() => player.play();
  Future<void> pause() => player.pause();
  Future<void> seek(Duration position) => player.seek(position);
  Future<void> skipToQueueItem(int i) => player.seek(Duration.zero, index: i);

  @override

  PlaybackState _transformEvent(PlaybackEvent event) {
    print("HEEEELLLLLOOOOOO");
    return PlaybackState(
      controls: [
        MediaControl.rewind,
        if (player.playing) MediaControl.pause else MediaControl.play,
        MediaControl.stop,
        MediaControl.fastForward,
      ],
      systemActions: const {
        MediaAction.seek,
        MediaAction.seekForward,
        MediaAction.seekBackward,
      },
      androidCompactActionIndices: const [0, 1, 3],
      processingState: {
        ProcessingState.idle: AudioProcessingState.idle,
        ProcessingState.loading: AudioProcessingState.loading,
        ProcessingState.buffering: AudioProcessingState.buffering,
        ProcessingState.ready: AudioProcessingState.ready,
        ProcessingState.completed: AudioProcessingState.completed,
      }
      [event.processingState]!,
      playing: player.playing,
      updatePosition: player.position,
      bufferedPosition: player.bufferedPosition,
      speed: player.speed,
    );
  }
}
`
this is how i am playing audio in background while controlling video in-app:

    controller.addListener(() async {
      if (controller.videoPlayerValue?.isPlaying ?? false) {
        print("Playing");
        await audioHandler.pause();
        update();
      } else {
        print("Paused");
        await audioHandler.play();
        update();
      }
      position = controller.currentVideoPosition;
      update();
    });

controller is playing video and it is related to pod_player


Upvotes: 0

Views: 210

Answers (0)

Related Questions