just_audio icon indicating copy to clipboard operation
just_audio copied to clipboard

[just_audio_background] - not able to work with LockCachingAudioSource

Open tushar0518 opened this issue 2 years ago • 7 comments

Which API doesn't behave as documented, and how does it misbehave? I'm already using the just_audio plugin with the LockCachingAudioSource mechanism but recently I integrated the just_audio_background plugin, implementation for the Android side and IOS side I did it perfectly as the docs say But when I try to play audio with this setup(shown below screenshot) it gives me errors like that a platform player with a specific identifier already exists

Minimal reproduction project https://github.com/tp-coderbabu/just_audio_lockcaching_audiosource.git

To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  1. run the git project
  2. Provide two buttons 1st code is working as expected with the given example
  3. 2nd button not working as it contains the LockCachingAudioSource
  4. See errors

Error messages

E/flutter ( 4255): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Platform player 5d3c487a-bfe1-408b-9508-8bb0189d348e already exists, null, null, null)
E/flutter ( 4255): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter ( 4255): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter ( 4255): <asynchronous suspension>
E/flutter ( 4255): #2      MethodChannelJustAudio.init (package:just_audio_platform_interface/method_channel_just_audio.dart:13:5)
E/flutter ( 4255): <asynchronous suspension>
E/flutter ( 4255): #3      _PlayerAudioHandler._init (package:just_audio_background/just_audio_background.dart:353:20)
E/flutter ( 4255): <asynchronous suspension>
E/flutter ( 4255): 

Expected behavior I guess it should work with LockCachingAudioSource too.

Screenshots with this setup, it's not working Screenshot 2023-10-04 at 6 00 13 PM

using this code as in the example app it's working fine Screenshot 2023-10-04 at 6 01 31 PM

Desktop (please complete the following information):

  • OS: MacOS Monterey 12.3

Smartphone (please complete the following information):

  • Device: Pixel 6a
  • OS: Android 10
  • Device: iPhone 12 pro
  • OS: IOS 15.2

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.6, on macOS 12.3 21E230 darwin-x64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.80.2)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Additional context Not found any.

tushar0518 avatar Oct 04 '23 12:10 tushar0518

I'm closing this since there is no git repo for me to clone. (If you don't know what I'm referring to, it is because you did not follow the instructions for submitting a bug report.)

ryanheise avatar Oct 04 '23 12:10 ryanheise

@ryanheise I have provided a git repo, Please take a look, and sorry for not following the instructions

tushar0518 avatar Oct 04 '23 13:10 tushar0518

You also deleted the steps to reproduce section.

ryanheise avatar Oct 04 '23 13:10 ryanheise

@ryanheise there aren't any specific steps for reproduction, yet I'm adding it now with the other instructions, I hope all the instructions are fulfilled.

tushar0518 avatar Oct 05 '23 04:10 tushar0518

@ryanheise have you looked at the issue?

tushar0518 avatar Oct 06 '23 04:10 tushar0518

I have just reopened the issue, but no I have not gotten to it yet.

ryanheise avatar Oct 06 '23 05:10 ryanheise

@tushar0518 I noticed that calling player.setAudioSource() without calling audioSource.request() throws this error. I had a similar issue. Test device was: Google Pixel 7 pro.

This is a snippet of my entire method

Future<void> startPlayingAudio(Track? track) async {
    try {
      final thumbnail = audioTeachings
          .firstWhereOrNull((element) => element.id == track?.albumId)
          ?.thumbnail;
      var audioSource = LockCachingAudioSource(
        Uri.parse(track?.url ?? ""),
        tag: MediaItem(
          // Specify a unique ID for each media item:
          id: track?.id ?? "0",
          // Metadata to display in the notification:
          title: track?.title ?? "",
        ),
        cacheFile: File(thumbnail ??
            '${(await getTemporaryDirectory()).path}/cache/${Uri.parse(track?.url ?? "").pathSegments.last}'),
      );
      audioSource.request();
      // Load and play the audio source
      await audioPlayer.setAudioSource(
        audioSource,
        initialPosition: Duration.zero,
      ); 
    } catch (e) {
      // Handle error, such as displaying a message to the user
    }
  }

photizzo avatar Jan 08 '24 01:01 photizzo