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

嘿 有计划改成录音成mp3吗? wav格式太大了 #1

Open
badboy-tian opened this issue Mar 3, 2019 · 2 comments
Open

嘿 有计划改成录音成mp3吗? wav格式太大了 #1

badboy-tian opened this issue Mar 3, 2019 · 2 comments

Comments

@badboy-tian
Copy link

No description provided.

@badboy-tian
Copy link
Author

首先, 谢谢你的这个库, 然后我改好了, 按照 https://www.jianshu.com/p/065bfe6d3ec2 这个教程编译lame
然后只需要改一个类就OK了

/**
 * 语音文件帮助类 用户保存相关文件
 */

public class AudioFileHelper {

    public static final String TAG = "AudioFileHelper";
    private AudioFileListener listener;
    private String savePath;
    private RandomAccessFile randomAccessFile;
    private File targetFile;
    private IdealRecorder.RecordConfig config;

    public AudioFileHelper(AudioFileListener listener) {
        this.listener = listener;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public void setRecorderConfig(IdealRecorder.RecordConfig config) {
        this.config = config;
    }


    public void start() {
        try {
            open(savePath);
        } catch (IOException e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onFailure(e.toString());
            }
        }
    }

    public void save(short[] data, int offset, int size) {
        if (randomAccessFile == null) {
            return;
        }
        try {
            write(randomAccessFile, data, offset, size);
        } catch (IOException e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onFailure(e.toString());
            }

        }
    }

    public void finish() {
        try {
            close();
        } catch (IOException e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onFailure(e.toString());
            }

        }
    }

    private byte[] mp3Buffer;

    private void open(String path) throws IOException {
        if (TextUtils.isEmpty(path)) {
            Log.d(TAG, "Path not set , data will not save");
            return;
        }
        if (this.config == null) {
            Log.d(TAG, "RecordConfig not set , data will not save");
            return;
        }
        targetFile = new File(path);

        if (targetFile.exists()) {
            targetFile.delete();
        } else {
            File parentDir = targetFile.getParentFile();
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
        }
        short bSamples;
        short nChannels;
        int sRate;
        if (config.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) {
            bSamples = 16;
        } else {
            bSamples = 8;
        }

        if (config.getChannelConfig() == AudioFormat.CHANNEL_IN_MONO) {
            nChannels = 1;
        } else {
            nChannels = 2;
        }
        sRate = config.getSampleRate();
        randomAccessFile = new RandomAccessFile(targetFile, "rw");
        randomAccessFile.setLength(0);
        Log.d(TAG, "mp3 path: " + path);
        SimpleLame.init(sRate, nChannels, sRate, bSamples);
        long bufSize = AudioRecord.getMinBufferSize(sRate, config.getChannelConfig(), config.getAudioFormat());
        mp3Buffer = new byte[(int) (7200 + (bufSize * 2 * 1.25))];
    }

    private void write(RandomAccessFile file, short[] data, int offset, int size) throws IOException {
        int encodedSize = SimpleLame.encode(data, data, size, mp3Buffer);
        file.write(mp3Buffer, offset, encodedSize);
    }

    private void close() throws IOException {
        try {
            if (randomAccessFile == null) {
                if (listener != null) {
                    listener.onFailure("File mp3 error exception occurs");
                }
                return;
            }
            final int flushResult = SimpleLame.flush(mp3Buffer);
            randomAccessFile.write(mp3Buffer, 0, flushResult);
            Log.d(TAG, "mp3 size: " + randomAccessFile.length());
            if (listener != null) {
                listener.onSuccess(savePath);
            }

        } finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
                randomAccessFile = null;
                SimpleLame.close();
            }
        }
    }

    public void cancel() {
        if (randomAccessFile == null) {
            return;
        }
        if (targetFile == null) {
            return;
        }
        if (targetFile.exists()) {
            targetFile.delete();
        }
        randomAccessFile = null;
        targetFile = null;
    }
}

@ideastudios
Copy link
Owner

你为何如此优秀

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

No branches or pull requests

2 participants