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

Fix ios audio duration #784

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

isilher
Copy link

@isilher isilher commented Mar 7, 2022

time

Context

While working with aac audio files I noticed the returned duration was often incorrect, shorter than the actual playtime of the audio file. This issue has also been described in #228. The included changes fix the problem for me, with the disclaimer that I have only tested this with local aac files.

The problem

The cause of the issue seems to be that the AVAudioPlayer is only taking a rough estimate of the total file duration. This may be even more off target when working with remote files that might not have completed loading when playing starts.

The proposed solution

I propose to also load the file in AVURLAsset, which allows for an option (AVURLAssetPreferPreciseDurationAndTimingKey) to be specified. This way we can force iOS to load the whole audio file, asynchronously request the duration property and only then continue initialisation.

Caveats

  • I have not tested this change with remove files.
  • I have not tested this change with other filetypes.
  • I am not very experienced with iOS and ObjC development, so the presented solution may need some polishing.

@iamyahyaahmed
Copy link

this works for me
setTimeout(() => { setSoundDuration(sound?.getDuration().toFixed(0)) }, 2000);

@isilher
Copy link
Author

isilher commented Oct 10, 2023

@iamyahyaahmed setting a timeout can work, but it may still fail if a file ever takes longer than the given delay to load (2 seconds in your example). And in cases where the loading might take shorter time, you have now enforced a wait time of 2 seconds where the user needs to wait for the UI to load.

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

Successfully merging this pull request may close these issues.

None yet

2 participants