Android:如何在录制后压缩音频文件然后保存

时间:2015-10-19 08:16:47

标签: android compression audio-recording

我知道如何使用MediaRecorder录制音频文件。 我想在服务器上传音频文件,但在此之前,我想压缩它。

注意:我正以.3gpp格式录制音频。 这是我的班级:

package com.iotaconcepts.task3;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity
{

MediaPlayer mediaPlayer;
MediaRecorder recorder;
String OUTPUT_FILE;

Button beginRecording, stopRecording, playRecording, stopPlayback;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/audiorecorder.3gpp";

    beginRecording = (Button)findViewById(R.id.bt_begin);
    stopRecording = (Button)findViewById(R.id.bt_stop_recording);
    playRecording = (Button)findViewById(R.id.bt_play_recording);
    stopPlayback = (Button)findViewById(R.id.bt_stop_playback);


    beginRecording.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                beginRecording();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    stopRecording.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopRecording();
        }
    });

    playRecording.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                playRecording();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    stopPlayback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlayback();
        }
    });


}



public void beginRecording() throws IOException {
    // If media recorder is working ... stop it
    ditchMediaRecorder();

    File outFile = new File(OUTPUT_FILE);

    if(outFile.exists())
        outFile.delete();

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.prepare();
    recorder.start();
}


public void stopRecording()
{
    if (recorder != null)
        recorder.stop();
}


public void playRecording() throws IOException
{
    ditchMediaPlayer();
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(OUTPUT_FILE);
    mediaPlayer.prepare();
    mediaPlayer.start();
}


public void stopPlayback()
{
    if (mediaPlayer != null)
    {
        mediaPlayer.stop();
    }
}


public void ditchMediaRecorder()
{
    if(recorder!=null)
    {
        recorder.release();
    }
}


public void ditchMediaPlayer()
{
    if (mediaPlayer != null)
    {
        mediaPlayer.release();
    }
    }

}

我已经在清单中包含了所需的权限。

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.RECORD_AUDIO" />

1 个答案:

答案 0 :(得分:0)

如果您正在寻找快速无损压缩,请查看LZ4:https://github.com/jpountz/lz4-java。它提供两种压缩方法:快速扫描(LZ4)和高压缩(LZ4 HC)。 jar文件可以在这里找到:http://java2s.com/Code/Jar/l/Downloadlz4120jar.htm