papesky
papesky

Reputation: 637

Android, cutted file sharing audio via whatsapp

in my app I create an audio file using TTS' synthesizeToFile method and it works fine.

Then, when the file is generated, I want to share it via whatsapp and it works fine again. But who recieve the file recieve it "cutted", not the complete audio as it is in my folder generated by TTS.

This is the code:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void shareAudioText() {
        String textToShare = mEditTextMain.getText().toString();
        File file = new File (mContext.getExternalFilesDir(null), "AudioFiles");

        if (!file.exists()) {
            boolean status = file.mkdir();
            if (status) {
                Toast.makeText(mContext, "Directory created successfully " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(mContext, "Directory create failed", Toast.LENGTH_SHORT).show();
            }
        }

        File audioFile = new File(file.getAbsolutePath() + "/myau.wav");
        textToSpeech.synthesizeToFile(textToShare, null, audioFile, "try");
        textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
            }

            @Override
            public void onDone(String utteranceId) {
                File file = new File (mContext.getExternalFilesDir(null), "AudioFiles");
                File audioFile = new File(file.getAbsolutePath() + "/myau.wav");
                final Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("audio/wav");
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(audioFile.getAbsolutePath()));

                if (shareIntent.resolveActivity(getContext().getPackageManager()) != null) {
                    mContext.startActivity(shareIntent);
                }
            }

            @Override
            public void onError(String utteranceId) {
            }
        });
    }

For example TTS records a file .wav during about 5 seconds, but the .aac sent by whatsapp is shorter, may be 2 or 3 seconds.

EDIT:
Well, I found that sharing a .opus file, the problem disappears. I tried to do this choosing a .opus file from a file manager and then sharing it. But when I create the opus file Whatsapp converts it to .aac again!

Can someone help me to fix this issue?

Thanks in advance.

Upvotes: 1

Views: 551

Answers (1)

Gon Juarez
Gon Juarez

Reputation: 69

The solution is transforming the (.wav, .opus, .ogg) to mp3. I used this library for converting the audio from wav to mp3: https://github.com/adrielcafe/AndroidAudioConverter

Imports:

import cafe.adriel.androidaudioconverter.AndroidAudioConverter;
import cafe.adriel.androidaudioconverter.callback.IConvertCallback;
import cafe.adriel.androidaudioconverter.callback.ILoadCallback;
import cafe.adriel.androidaudioconverter.model.AudioFormat;

Use this in the constructor:

public AudioConv(Context context1) {
    AndroidAudioConverter.load(context1, new ILoadCallback() {
        @Override
        public void onSuccess() {
            // Great!
        }
        @Override
        public void onFailure(Exception error) {
            // FFmpeg is not supported by device
        }
    });
}
public void convertAudio(File file){
    /**
     *  Update with a valid audio file!
     *  Supported formats: {@link AndroidAudioConverter.AudioFormat}
     */

    IConvertCallback callback = new IConvertCallback() {
        @Override
        public void onSuccess(File convertedFile) {
            //Toast.makeText(MainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
            audio=convertedFile;
            Log.e("paths",convertedFile.getAbsolutePath());
        }
        @Override
        public void onFailure(Exception error) {
            // Toast.makeText(MainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
            Log.e("paths",error.getMessage());
        }
    };
    //Toast.makeText(this, "Converting audio file...",  Toast.LENGTH_SHORT).show();
    AndroidAudioConverter.with(context)
            .setFile(file)
            .setFormat(AudioFormat.MP3)
            .setCallback(callback)
            .convert();
}

Upvotes: 3

Related Questions