react-native-sound icon indicating copy to clipboard operation
react-native-sound copied to clipboard

getDuration() not working

Open dereksweet opened this issue 8 years ago • 15 comments

I am trying to use getDuration() to determine when the user is at the end of their playback, but it is returning incorrect values. For example I have a 7 minute 12 second recording for which it should be returning something in the vicinity of 432 seconds, but it is currently returning approximately 410.5. This is on iOS, have not tested on Android. I know for sure the recording is 7 minutes and 12 seconds because I can sit there and listen to it, and watch the timer based on getCurrentTime() count all the way up..

EDIT: To add some more context for this, I was playing an .aac audio file recorded using the package react-native-audio

dereksweet avatar Jul 12 '17 16:07 dereksweet

Do you have some code we can see?

gvenk avatar Aug 24 '17 19:08 gvenk

I'm also experiencing an issue where getDuration() is not returning the correct length of an aac file recorded with react-native-audio.

  loadRecording() {
    let recordingFilePath = AudioUtils.DocumentDirectoryPath + "/" + this.props.recording.filename;
    let soundFile = new Sound(recordingFilePath, '', (error) => {
      if (error) {
        console.log('failed to load the sound', error);
        return;
      }
      // loaded successfully
      console.log('duration in seconds: ' + soundFile.getDuration() + ', number of channels: ' + soundFile.getNumberOfChannels());
    });
  }

mattlm avatar Aug 25 '17 04:08 mattlm

I currently don't have a setup with react-native-audio, so it's a bit difficult for me to test it. I'll test it with a 'normal' aac-file, to see if it's happing with this also.

gvenk avatar Aug 25 '17 08:08 gvenk

@dereksweet I believe we discussed something similar in #225 Does it help?

trepidity avatar Sep 27 '17 13:09 trepidity

I have this issue on Android. It works fine on iOS, but on Android the call to getDuration() is returning -0.001 seconds. Any idea why?

scruff311 avatar Dec 12 '17 00:12 scruff311

+1

Uysim avatar Jan 09 '18 07:01 Uysim

+1

sankar4n avatar Mar 27 '19 09:03 sankar4n

I am having this same issue, I am using react-native-audio lib to record the audio. This is my audio config:

    SampleRate: 22050,
    Channels: 1,
    AudioQuality: "Low",
    AudioEncoding: "aac",
    AudioEncodingBitRate: 32000

Audio recorded in android device works fine in both android and iOS devices, able to get duration for them. Audio recorded from iOS device works fine iOS device, but for same iOS recorded audio, I am not able to get duration while playing that in android(it give -0.001 seconds duration, which is default). Any idea how to solve this? Help appreciated. Thanks.

meetjpatel avatar Jul 11 '19 13:07 meetjpatel

Could you post a link to an .aac file that exhibits the problem, along with code that demonstrates the problem?

paulmelnikow avatar Jul 11 '19 15:07 paulmelnikow

Hi @paulmelnikow , this is the link for iPhone recorded audio: http://reantamchet.zluck.com/storage/app/audios/1562906494-1562906495986.aac

this is the record function:

async _record() {
     AudioRecorder.prepareRecordingAtPath(audioPath, {
        SampleRate: 22050,
        Channels: 1,
        AudioQuality: "Low",
        AudioEncoding: "aac",
      });

      try {
        const filePath = await AudioRecorder.startRecording();
      } catch (error) {
        console.log("error",error);
      }
    }

this is upload audio api function:

export const postRes = (token,course_id,id,answer,audio) => {
    return new Promise((resolve,reject) => {

        var params = new FormData();
  	params.append("audio",{uri: Platform.OS == 'ios' ? audio.split('file://')[1] : ('file://'+audio), type: 'audio/aac', name: new Date().getTime()+'.aac', filename : new Date().getTime()+'.aac'});
       
        fetch(config.API_URL+`/courses/addanswer?`,{
            method: 'post',
            body : params,
            headers: {
                'authorization': 'Bearer '+token,
                'accept': 'application/json',
            },
        })
        .then(res => res.json())
        .then(json => resolve(json))
        .catch(e =>{
            reject(error)
        });
    });
}

this is the play function:

play = async () => {
        if(this.sound){
            this.sound.play(this.playComplete);
            this.setState({playState:'playing'});
        }else{
            const filepath = 'http://reantamchet.zluck.com/storage/app/audios/1562906494-1562906495986.aac;
            this.sound = new Sound(filepath, '', (error) => {
                if (error) {
                    console.log('failed to load the sound', error);
                    Alert.alert('Notice', 'audio file error. (Error code : 1)');
                    this.setState({playState:'paused'});
                }else{
                    this.setState({playState:'playing', duration:this.sound.getDuration()});
                    this.sound.play(this.playComplete);
                }
            });
        }
    }

meetjpatel avatar Jul 12 '19 07:07 meetjpatel

Seems it is still not resolved

deepgosalia1 avatar Jun 03 '20 20:06 deepgosalia1

Any news for this issue?

hepifish avatar Jul 12 '21 07:07 hepifish

I have this issue on Android. It works fine on iOS, but on Android the call to getDuration() is returning -0.001 seconds. Any idea why?

same here, have you got the solution?

PrasannaUplogic avatar Nov 18 '21 13:11 PrasannaUplogic

Issue is still here... Getting wrong values when using getDuration() of .aac file

Immorrtall avatar Mar 02 '22 15:03 Immorrtall

I encountered this issue and wrote up a first attempt at fixing it. More information and caveats can be found in the PR: #784

isilher avatar Mar 07 '22 17:03 isilher