1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] justAudio used very simply per the example creates an Exception

Discussão em 'Mobile' iniciado por Stack, Setembro 28, 2024 às 13:23.

  1. Stack

    Stack Membro Participativo

    I have completed an app using Just Audio. I built it following the example provided. I want to add background music. I thought I followed Ryan Heise's example for a simple player. I must be missing something. I receive the following Exception:

    Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'MediaItem' in type cast)

    on this line:

    await musicPlayer.setAudioSource(AudioSource.uri(Uri.parse(url)));

    Here is the function that fails with this line:

    Future<void> playBackgroundMusic() async {
    int rng = Random().nextInt(7);
    // Inform the operating system of our app's audio attributes etc.
    // We pick a reasonable default for an app that plays speech.
    final session = await AudioSession.instance;
    await session.configure(const AudioSessionConfiguration.speech());
    String musicFilename = filenameList[rng];
    String url =
    "MY_SERVER/music/$musicFilename";
    final musicPlayer = AudioPlayer();
    try {
    await musicPlayer.setAudioSource(AudioSource.uri(Uri.parse(url)));
    } on PlayerException catch (e) {
    print("Error loading audio source: $e");
    }
    musicPlayer.play();
    }



    This function is called by the first initState in the app:

    @override
    void initState() {
    super.initState();
    playBackgroundMusic();
    }


    A similar question 78571039 was asked and the answer given was to import Audio Service and add this code:

    AudioSource.uri( Uri.parse(item.music), tag: MediaItem( id: "${item.id}", album: "${musicImage}", title: "${item.name}", artUri: Uri.parse('${musicImage}'), ), ),


    This solved my problem - but there is no way I would have known this from the Just Audio documentation. I think this indicates that something (documentation) or code needs to change in Just Audio. This problem was not solved on Github in September 2023 due to lack of specificity. https://github.com/ryanheise/just_audio/issues/991.

    So I am asking either to be pointed to the existing documentation that says this import and line of code is required or to update the documentation. This is too good of a plugin to have something that would keep people from using it.

    Continue reading...

Compartilhe esta Página