Android,通过WhatsApp截取文件共享音频 [英] Android, cutted file sharing audio via whatsapp

查看:57
本文介绍了Android,通过WhatsApp截取文件共享音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用TTS的synthesizeToFile方法创建了一个音频文件,它运行良好。

然后,当文件生成时,我想通过WhatsApp共享它,它再次正常工作。但收到文件的人收到的是"剪辑"的文件,而不是TTS生成的文件夹中的完整音频。

代码如下:

@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) {
            }
        });
    }

例如,TTS在大约5秒内记录文件.wav,但WhatsApp发送的.aac较短,可能为2秒或3秒。

编辑:
好吧,我发现共享一个.opus文件,问题就消失了。我试着这样做,从文件管理器中选择一个.opus文件,然后共享它。但当我创建操作文件时,Whatsapp再次将其转换为.aac!

有人能帮我解决此问题吗?

提前谢谢。

推荐答案

解决方案正在将(.wav、.opus、.ogg)转换为MP3。我使用这个库将音频从wav转换为mp3: https://github.com/adrielcafe/AndroidAudioConverter

导入:

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

在构造函数中使用以下代码:

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();
}

这篇关于Android,通过WhatsApp截取文件共享音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆