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

Error: Attempt to call getDuration in wrong state #266

Closed
VictorUvarov opened this issue Aug 26, 2019 · 11 comments
Closed

Error: Attempt to call getDuration in wrong state #266

VictorUvarov opened this issue Aug 26, 2019 · 11 comments

Comments

@VictorUvarov
Copy link
Contributor

VictorUvarov commented Aug 26, 2019

I get the following error when trying to get duration for a remote URL. I am trying to get the duration to load before I show the widget because it is really confusing for the user to see a position of 0 and a duration of 0.

  • Platform: Android
  • Code
AudioPlayer _audioPlayer = AudioPlayer();
final url = 'https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3'
int success = await _audioPlayer.setUrl(url);

if (success == 1) {
    int duration = await _audioPlayer.getDuration();
}
  • Error
V/MediaHTTPService(13455): MediaHTTPService(android.media.MediaHTTPService@12f1aa7): Cookies: null
V/MediaHTTPService(13455): makeHTTPConnection: CookieHandler (java.net.CookieManager@c5f5f0f) exists.
V/MediaHTTPService(13455): makeHTTPConnection(android.media.MediaHTTPService@12f1aa7): cookieHandler: java.net.CookieManager@c5f5f0f Cookies: null
V/MediaHTTPService(13455): MediaHTTPService(android.media.MediaHTTPService@1e47afd): Cookies: null
V/MediaHTTPService(13455): makeHTTPConnection: CookieHandler (java.net.CookieManager@c5f5f0f) exists.
V/MediaHTTPService(13455): makeHTTPConnection(android.media.MediaHTTPService@1e47afd): cookieHandler: java.net.CookieManager@c5f5f0f Cookies: null
V/MediaHTTPService(13455): MediaHTTPService(android.media.MediaHTTPService@86fa3f2): Cookies: null
V/MediaHTTPService(13455): makeHTTPConnection: CookieHandler (java.net.CookieManager@c5f5f0f) exists.
V/MediaHTTPService(13455): makeHTTPConnection(android.media.MediaHTTPService@86fa3f2): cookieHandler: java.net.CookieManager@c5f5f0f Cookies: null
E/MediaPlayerNative(13455): Attempt to call getDuration in wrong state: mPlayer=0x79ec7b73c0, mCurrentState=4
E/MediaPlayerNative(13455): error (-38, 0)
E/MediaPlayer(13455): Error (-38,0)
V/MediaPlayer(13455): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(13455): cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer(13455): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(13455): cleanDrmObj: mDrmObj=null mDrmSessionId=null
@kaifiappsmaven
Copy link

While initializing audio player use below method -
_durationSubscription =
Injector.getAUdio().onDurationChanged.listen((duration) {
if (!mounted) return;
setState(() {
if (_playerState != PlayerState.paused ||
_playerState != PlayerState.stopped) {
_duration = duration;
}
});
});
It will return duration of audio.
In meantime this value is fetched, you can show streaming.. function. When this value is update show it in widget

@VictorUvarov
Copy link
Contributor Author

I already listen for onDurationChanged. I would like to show the user the duration of the audio before it starts playing.

@erickzanardo
Copy link
Member

You can get the duration without playing, check this on the example app: https://github.com/luanpotter/audioplayers/blob/master/example/lib/main.dart#L112

@VictorUvarov
Copy link
Contributor Author

VictorUvarov commented Aug 27, 2019

Yes, I have done that, but I still get the same error.

An exception has occurred.
PlatformException (PlatformException(error, Unsupported value: java.lang.IllegalStateException, null))

Some reason it keeps complaining about attachNewPlayer called in state 4
Is there an example for remote URL duration?
I added the demo to the example of my fork https://github.com/VictorUvarov/audioplayers?organization=VictorUvarov&organization=VictorUvarov

@shikha1992
Copy link

As suggested by @erickzanardo , above example to fetch duration is working fine. I implemented same in demo.
Future _getDuration() async {
int duration = await Future.delayed(Duration(seconds: 0), () => Injector.getAUdio().getDuration());
debugPrint('duration$duration');
_duration = Duration(milliseconds: duration);
return duration;
}
After initializing audioplayer, duration is fetched after passing url to audioplayer.

@VictorUvarov
Copy link
Contributor Author

VictorUvarov commented Aug 28, 2019

Did you test with a remote URL? For example, https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3 Or does getDuration() only work with local asset files? If you get it to work with remote URL I would love to see the function for getting a remote URL duration.

@VictorUvarov
Copy link
Contributor Author

Turns out the duration is working correctly. The files I was testing were not in the proper format.

@volgin
Copy link
Contributor

volgin commented Jan 18, 2020

The solution proposed by @shikha1992 does not work, unless I set duration of a delay to 500ms (at 250ms it works sometimes). This is definitely a bug.

The same code for iOs works without a delay.

@Nailik
Copy link

Nailik commented Mar 11, 2020

I had the same Problem with getDuration and when i want to read mp3 Tags, i don't want a 250-500ms delay for every single song.
The best way to get the duration ist maybe https://pub.dev/packages/flutter_ffmpeg (Flutter FFMPEG).
It can also read title etc and audio-tls supports down to android 16

@chornbe
Copy link

chornbe commented Aug 17, 2020

I've had to implement the delay. I went through hoop after hoop after hoop. Ultimately, I just elected to NOT show or care about the duration until playback started :(

Seems I have a lot of flutter code that basically says:

if( true == isAndroid ) { /* do all the workaround stuff here because android */ }

:(

@xiaoxin-sky
Copy link

The solution proposed by @shikha1992 does not work, unless I set duration of a delay to 500ms (at 250ms it works sometimes). This is definitely a bug.

The same code for iOs works without a delay.

Thanks,I fixed the problem with delay 300ms in Android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants