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

Trying to play multiple audio files one by one with one audio player object. But if I don't initalize it again before playing it gives this error #1174

Closed
Aneess333 opened this issue Jun 18, 2022 · 6 comments
Labels
bug platform-android Affects the android platform stale Automatically close this issue, if no activity is recognized waiting for report Wait for the author to respond to the conversation

Comments

@Aneess333
Copy link

Aneess333 commented Jun 18, 2022

E/MediaPlayerNative( 8096): attachNewPlayer called in state 4
D/AudioPlayers( 8096): Unexpected error!
D/AudioPlayers( 8096): java.lang.IllegalStateException
D/AudioPlayers( 8096): 	at android.media.MediaPlayer._setDataSource(Native Method)
D/AudioPlayers( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1301)
D/AudioPlayers( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1284)
D/AudioPlayers( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1241)
D/AudioPlayers( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1218)
D/AudioPlayers( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1183)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:51)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:87)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:58)
D/AudioPlayers( 8096): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
D/AudioPlayers( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
E/flutter ( 8096): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(Unexpected error!, null, java.lang.IllegalStateException
E/flutter ( 8096): 	at android.media.MediaPlayer._setDataSource(Native Method)
E/flutter ( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1301)
E/flutter ( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1284)
E/flutter ( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1241)
E/flutter ( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1218)
E/flutter ( 8096): 	at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1183)
E/flutter ( 8096): 	at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
E/flutter ( 8096): 	at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:51)
E/flutter ( 8096): 	at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
E/flutter ( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:87)
E/flutter ( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
E/flutter ( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
E/flutter ( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
E/flutter ( 8096): 	at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:58)
E/flutter ( 8096): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/flutter ( 8096): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/flutter ( 8096): 	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
E/flutter ( 8096): 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
E/flutter ( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
E/flutter ( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
E/flutter ( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
E/flutter ( 8096): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
E/flutter ( 8096): , null)
E/flutter ( 8096): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 8096): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
E/flutter ( 8096): <asynchronous suspension>
E/flutter ( 8096): #2      AudioPlayer.play (package:audioplayers/src/audioplayer.dart:111:5)
E/flutter ( 8096): <asynchronous suspension>
E/flutter ( 8096): #3      AudioBloc._onAudioPicking (package:task4/blocs/audio_bloc/audio_bloc.dart:130:7)
E/flutter ( 8096): <asynchronous suspension>
E/flutter ( 8096): #4      Bloc.on.<anonymous closure>.handleEvent (package:bloc/src/bloc.dart:211:13)
E/flutter ( 8096): <asynchronous suspension>
E/flutter ( 8096): 
@Aneess333 Aneess333 added the bug label Jun 18, 2022
@Aneess333
Copy link
Author

If i initalize it again then I can't access it onChange methods in bloc. Because of that I can't access total time and it's remaining time.

@Gustl22
Copy link
Collaborator

Gustl22 commented Jun 25, 2022

Can you reproduce it with the example or do you have some sample project? What are the exact steps to reproduce? Also use the issue template!

@plato79
Copy link

plato79 commented Sep 5, 2022

I'm also getting this error. Although, I'm only playing one file at a time. I execute audioplayer.stop then execute .play for another file after that.

The thing is it doesn't happen always, most of the time there's no problem but sometimes I get this error, and of course there's no audio playing.

My code is like this:

This part executes on next/prev button:

       if (isPlaying) stopPlaying();
       playAudio();

  void playAudio() async {
    if (audioQuestion!.getAudio(language) != '') {
      audioPlayer
          .play(
        UrlSource(await audioUrl(audioSource!.getAudio())),
      )
          .then((value) {
        setState(() => isPlaying = true);
      }).onError((error, stackTrace) {
        if (kDebugMode) {
          print(error.toString());
        }
      });
    }
  }

I pull audioUrl via cachemanager that's why there's an await on audioUrl.

  void stopPlaying() {
    if (isPlaying) {
      audioPlayer
          .stop()
          .then((_) => setState(() => isPlaying = false))
          .onError((error, stackTrace) {
        if (kDebugMode) {
          print(error.toString());
        }
      });
    }
  }

@Gustl22 Gustl22 added the platform-android Affects the android platform label Sep 21, 2022
@Gustl22
Copy link
Collaborator

Gustl22 commented Sep 21, 2022

Can you test with current main branch in pubspec.yaml?

dependency_overrides:
  audioplayers:
    git:
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers'
  audioplayers_platform_interface:
    git:
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_platform_interface'
  audioplayers_web:
    git:
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_web'
  audioplayers_linux:
    git:
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_linux'
  audioplayers_android:
    git:
      url: https://github.com/bluefireteam/audioplayers.git
      path: 'packages/audioplayers_android'

@Gustl22 Gustl22 added the waiting for report Wait for the author to respond to the conversation label Sep 21, 2022
@plato79
Copy link

plato79 commented Sep 21, 2022

found out the problem was mine. On the flutter discord server Xazin helped me to solve the problem. I was stopping playback and starting another one immediately after. But it seems stop takes time and had to wait for it.. So await stopPlayback() then await playAudio() worked.

@Gustl22 Gustl22 added the stale Automatically close this issue, if no activity is recognized label Sep 21, 2022
@github-actions
Copy link

This issue was closed by the stale bot due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform-android Affects the android platform stale Automatically close this issue, if no activity is recognized waiting for report Wait for the author to respond to the conversation
Projects
None yet
Development

No branches or pull requests

3 participants