Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] stopping .withUI creates a bad state and spams "This is BAD !!!" message #12

Closed
japhiaolson opened this issue Jun 23, 2020 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@japhiaolson
Copy link

japhiaolson commented Jun 23, 2020

I need Help for:

  • Debugging issue

Using Sounds 0.8.9
Tested on Android Emulator (debug mode)

Not sure if I'm doing this incorrectly, or if there is an issue with the library.

I initialize with:
SoundPlayer.withUI(canSkipForward: true, canSkipBackward: true);

Upon clicking the "forward/backward" buttons in the OS shade the event propagates to the onSkipForward/onSkipBackward listeners which then call .stop() and move to the next item in the playlist array and call .play() (I'm not using the Album part of the API as each "track" actually consists of sequentially playing through more than one sound clip with silent delays between each clip).
The above works fine until I dispose/release at which point the error message "E/SoundPlayer(13652): MediaPlayerOnPreparedListener timer: mMediaBrowserHelper.mediaControllerCompat is NULL. This is BAD !!!" starts spamming the console.

If I just call release() after every stop() then the error never happens, however that means that the OS shade slides out for a second and then back in everytime the forward/back buttons are pressed by the user.

If the sounds are allowed to stop on their own (e.g., naturally reach the end of their duration) then the next playlist item plays fine and no errors propagate. It only happens if I manually call stop() without calling release(). I believe my only option right now would be to get the sound duration and seekTo near the end and let it stop itself, but that doesn't seem like the right way to handle this.

To simplify, I'm looking to do the following:

  • Start playing an audio clip > play()
  • User taps Next in the OS shade > await stop()
  • Audio stops immediately and plays next clip > play()
  • Leave route/screen > release()

Just let me know if you all need any more details or if I'm just not handling this scenario correctly.

Here's a small sample that triggers the error when you play the sound, skip to next sound, release sound.

import 'package:flutter/material.dart';
import 'package:sounds/sounds.dart';
import 'dart:io';

import '../widgets/texttospeech/device_voices.dart';
import '../models/caching/texttospeech_model.dart';

class TestSounds extends StatefulWidget {
  static const routeName = "/testSound";
  @override
  State<StatefulWidget> createState() => _TestSounds();
}

class _TestSounds extends State<TestSounds> {
  SoundPlayer flutterSound;
  int _activeTrack = 0;

  List<TextToSpeechParameters> _testSpeech = [
    TextToSpeechParameters(
        word: "Hello, what's up this is a sound test",
        voiceName: "en-AU-Wavenet-B",
        speakingRate: "1",
        languageCode: "en-US"),
    TextToSpeechParameters(
        word: "This is a test of sound again",
        voiceName: "en-AU-Wavenet-B",
        speakingRate: "1",
        languageCode: "en-US")
  ];

  @override
  void initState() {
    super.initState();
    flutterSound =
        SoundPlayer.withUI(canSkipForward: true, canSkipBackward: false);
    _setupSubscriptions();
  }

  _setupSubscriptions() {
    flutterSound.onSkipBackward = () async {
      //_gotoPrevPlaylistItem();
    };
    flutterSound.onSkipForward = () {
      _gotoNextPlaylistItem();
    };
  }

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

  void _releaseSounds() async {
    if (!flutterSound.isStopped) {
      await flutterSound.stop();
    }
    await flutterSound.release();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          child: Column(
            children: [
              MaterialButton(
                child: Text("PLAY"),
                onPressed: () {
                  if (flutterSound.isStopped) {
                    _activeTrack = 0;
                    _playTrack(_activeTrack);
                  }
                },
              ),
              MaterialButton(
                child: Text("RELEASE"),
                onPressed: () {
                  _releaseSounds();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  _gotoNextPlaylistItem() async {
    if (!flutterSound.isStopped) {
      await flutterSound.stop();
    }
    _playTrack(_incrementTrack);
  }

  int get _incrementTrack {
    if (_activeTrack >= _testSpeech.length - 1) {
      _activeTrack = 0;
    } else {
      _activeTrack++;
    }
    return _activeTrack;
  }

  _playTrack(int activeTrack) async {
    File soundClip = await synthesizeLocalSound(_testSpeech[_activeTrack]);
    Track track = Track.fromFile(soundClip.path);
    flutterSound.play(track);
  }

  Future<File> synthesizeLocalSound(TextToSpeechParameters speechParams) async {
    File tts = await LocalTextSpeech.synthesizeToFile(speechParams);

    return tts;
  }
}
@japhiaolson japhiaolson added the help wanted Extra attention is needed label Jun 23, 2020
@japhiaolson
Copy link
Author

Nevermind on this - I worked it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant