且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试将音频文件上传到服务器时出现Android问题

更新时间:2022-04-02 22:28:41

这就是您需要做的,就像调用getFileName()的相同方法一样,该方法实际上通过添加当前时间和日期来实现文件名,因此,第一次调用时,它会提示您01-01-2014 12-12-20.mp4,并且在录制20秒后,当您尝试通过doUploadFile(getFileName())上传文件时,它会再次调用该方法并为您返回续订文件名将在20秒后,即01-01-2014 12-12-40.mp4.因此,这是您两次调用同一方法的问题,只需使用一次,这就是您的最终代码.

This is what you need to do, as in the same method you are calling getFileName() which actually implements the name of file by adding current time and date, so the first time you call, it gives you say 01-01-2014 12-12-20.mp4 and after 20 seconds recording when you try to upload file via doUploadFile(getFileName()) it again calls that method and returns you a renewed file name that will be 20 seconds later that is 01-01-2014 12-12-40.mp4 . So here is your issue of calling same method twice, just use it once and here is your final code.

String myFileName = "";

@Override onCreate(){
    //Your recording process
    //Your uploading process
}
private void startRecording() {
    myFileName = getFilename();
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(myFileName);
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
        doAudioFileUpload(myFileName);
    }
}