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

MEDIA.getDuration() returns -1 if MEDIA.play() never been called at least once #177

Open
engr-Eghbali opened this issue Aug 29, 2018 · 10 comments
Labels

Comments

@engr-Eghbali
Copy link

hi.
I had an issue as said in title : MEDIA.getDuration() returns -1 if MEDIA.play() never been called at least once
and only way i can fix it is play() and pause() media in background and then get duration !

@janpio janpio added the bug label Aug 29, 2018
@janpio
Copy link
Member

janpio commented Aug 29, 2018

What platform? What platform version?
What device were you testing on? What OS version was that running?
What version of the plugin?

What is MEDIA? Do you mean Media?

@engr-Eghbali
Copy link
Author

@janpio
platform android(v7.0.0 ) on "HTC One A9" & "Samsung galaxy A7" , both android nougat and 5.0.2 plugin version.
sorry for mistake, yes Media is true. (include recorded voice and video in this case).

@janpio
Copy link
Member

janpio commented Aug 29, 2018

Then first update your cordova-android to the current version 7.1.1 to make sure this is not fixed already.

@engr-Eghbali
Copy link
Author

Done, the bug remains.

@janpio
Copy link
Member

janpio commented Aug 29, 2018

Documentation says:

media.getDuration
Returns the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.

Is it possible that the data is actually not available before play was executed? When is the file actually requested/loaded?

@engr-Eghbali
Copy link
Author

I tried different ways,data should not available "onSuccess"? working version is like:

 navigator.device.capture.captureAudio(onSuccess, onError, options);

   function onSuccess(mediaFiles) {
      var  path,recordedVoice,stat,interval,;
     
       path = mediaFiles[0].fullPath;
       recordedVoice = new Media(path, function(){console.log("well played!")}, function(e){console.log("error:"+e)},function(status){stat=status});
      
       recordedVoice.play();
       recordedVoice.pause();

         document.getElementById("playBTN").addEventListener("click",()=>{
           var constant=100/(recordedVoice.getDuration()*10);
///etc..

@janpio
Copy link
Member

janpio commented Aug 30, 2018

  1. You didn't mention before that you are working with a file that was just created with captureAudio.
  2. Why should this make a difference? The relevant part here is that you create a new Media object with a path to a certain file. If this plugin doesn't actually read the file data until you call play() - then it just can't know the duration. You will have to check this plugin's code to understand when it is reading the file to get the duration.

If the current state then is not what you want, create a new issue and suggest this to be implemented or best create a PR yourself.

@engr-Eghbali
Copy link
Author

Yes, I think that's a need, thanks a lot.

@zhanghuanchong
Copy link

I have a solution to get the correct duration after recording on iOS:

After below line:

// no callback - that will happen in audioRecorderDidFinishRecording

Add:

        [self prepareToPlay:audioFile withId:mediaId];
        double duration = round(audioFile.player.duration * 1000) / 1000;
        [self onStatus:MEDIA_DURATION mediaId:mediaId param:@(duration)];

@pagano
Copy link

pagano commented Nov 24, 2020

I have a solution to get the correct duration after recording on iOS:

After below line:

// no callback - that will happen in audioRecorderDidFinishRecording

Add:

        [self prepareToPlay:audioFile withId:mediaId];
        double duration = round(audioFile.player.duration * 1000) / 1000;
        [self onStatus:MEDIA_DURATION mediaId:mediaId param:@(duration)];

Nice catch zhang! I don't know how to do it, but a pull request for this would be great

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

No branches or pull requests

4 participants