Search code examples
flutterjust-audio

What to use instead of AudioPlaybackState, cannot find the documentation


I was looking at a clients app but I cannot find the solution. It was a really old code and I have updated the just_audio plugin to the latest version and i am getting errors in the code below I don't know what to do. I understand the mapping but I don't know what to write instead of "AudioPlaybackState".

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:online_radio/radio/radio_player.dart';
import 'package:online_radio/radio/radio_state.dart';

class JustAudioPlayer extends RadioPlayer {
  final AudioPlayer _audioPlayer = AudioPlayer();
  Stream<RadioState> radioStateStream;

  JustAudioPlayer() {
    radioStateStream = _audioPlayer.playbackStateStream.map(_mapToRadioState);
  }

  @override
  Future<void> setUrl(String url) async {
    return _audioPlayer.setUrl(url);
  }

  @override
  Future<void> play({@required String url}) async {
    return _audioPlayer.play();
  }

  @override
  Future<void> pause() {
    return _audioPlayer.pause();
  }

  RadioState _mapToRadioState(AudioPlaybackState audioState) {
    switch (audioState) {
      case AudioPlaybackState.none:
      case AudioPlaybackState.stopped:
        return RadioState.STOPPED;
      case AudioPlaybackState.paused:
        return RadioState.PAUSED;
      case AudioPlaybackState.playing:
        return RadioState.PLAYING;
      default:
        return RadioState.UNKNOWN;
    }
  }
}

These are the errors i am getting:

These are the errors i am getting

Code


Solution

  • This is probably something you could have found by reading their current documentation. Specifically this section (https://github.com/ryanheise/just_audio/tree/minor/just_audio#working-with-state-streams):

    player.playerStateStream.listen((state) {
      if (state.playing) ... else ...
      switch (state.processingState) {
        case ProcessingState.idle: ...
        case ProcessingState.loading: ...
        case ProcessingState.buffering: ...
        case ProcessingState.ready: ...
        case ProcessingState.completed: ...
      }
    });
    
    // See also:
    // - durationStream
    // - positionStream
    // - bufferedPositionStream
    // - sequenceStateStream
    // - sequenceStream
    // - currentIndexStream
    // - icyMetadataStream
    // - playingStream
    // - processingStateStream
    // - loopModeStream
    // - shuffleModeEnabledStream
    // - volumeStream
    // - speedStream
    // - playbackEventStream
    

    As you can see there are now multiple streams which can be used to achieve the same behaviour as before:

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:just_audio/just_audio.dart';
    import 'package:online_radio/radio/radio_player.dart';
    import 'package:online_radio/radio/radio_state.dart';
    
    class JustAudioPlayer extends RadioPlayer {
      final AudioPlayer _audioPlayer = AudioPlayer();
      Stream<RadioState> radioStateStream;
    
      JustAudioPlayer() {
        radioStateStream = _audioPlayer.playerStateStream.map(_mapToRadioState);
      }
    
      @override
      Future<void> setUrl(String url) async {
        return _audioPlayer.setUrl(url);
      }
    
      @override
      Future<void> play({@required String url}) async {
        return _audioPlayer.play();
      }
    
      @override
      Future<void> pause() {
        return _audioPlayer.pause();
      }
    
      RadioState _mapToRadioState(PlayerState playerState) {
        final processingState = playerState?.processingState;
        final playing = playerState?.playing;
    
    
        if (playing == true) return RadioState.PLAYING;
        if (processingState == ProcessingState.completed || 
             processingState == ProcessingState.idle) return RadioState.STOPPED;
        if (processingState == ProcessingState.ready) return RadioState.PAUSED;
        return RadioState.UNKNOWN;
        }
      }
    }
    

    Please note: This code was not tested and you should do more testing to make sure that it works in the way you need it.