如何将音频文件转换为字节

时间:2012-11-05 17:13:59

标签: android inputstream

我创建了以下progamm,其任务非常简单:录制然后显示音频。我试图添加另一种方法,允许我以图形方式显示记录的样本。为此,我在一年前使用了@Robby Pond在另一个问题上提出的建议:

http://stackoverflow.com/questions/5333908/fileinputstream-to-byte-array-in-android-application

我活动的核心是:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class RecorderTutorial extends Activity implements OnClickListener{

    MediaRecorder mRecorder = new MediaRecorder();
    MediaPlayer mPlayer = new MediaPlayer();
    boolean isRecording = false;



    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recorder_tutorial);
    findViewById(R.id.play_back_button).setOnClickListener(this);
    findViewById(R.id.record_button).setOnClickListener(this);
    findViewById(R.id.draw).setOnClickListener(this);
    }



    public void onClick(View v) {

        mPlayer.stop();

        switch (v.getId()) {

            case R.id.play_back_button:

                if (!isRecording && !mPlayer.isPlaying()) {


                    try {
                        mPlayer.reset();
                        mPlayer.setDataSource("/sdcard/audio_demo.3gp");
                        mPlayer.prepare();
                        mPlayer.start();
                    } catch (Exception e) {

                    Toast.makeText(this, "Error playing back audio.",Toast.LENGTH_SHORT).show();

                    }

                }

            break;

            case R.id.record_button:

                if (isRecording) {

                    isRecording = false;
                    ((Button)(findViewById(R.id.record_button))).setText("record");
                    mRecorder.reset();

                } else {

                    try {

                        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile("/sdcard/audio_demo.3gp");
                        mRecorder.prepare();
                        mRecorder.start();

                        ((Button)(findViewById(R.id.record_button))).setText("stop");

                        isRecording = true;

                    } catch (Exception e) {

                        Toast.makeText(this, "Error starting recorder.",Toast.LENGTH_SHORT).show();

                    }

                }

            break;

            case R.id.draw:

                final Context context = v.getContext();
                int bytesRead;

                try {

                    InputStream is = context.openFileInput("/sdcard/audio_demo.3gp");

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    byte[] b = new byte[1024];

                    while ((bytesRead = is.read(b)) != -1) {

                        bos.write(b, 0, bytesRead);

                }

                byte[] bytes = bos.toByteArray();

                System.out.println(" One random values is: "+bytes[5]+" \n");

                }catch(Exception e) {

                    Toast.makeText(this, "Error starting draw. ",Toast.LENGTH_SHORT).show();

                }

            break;
        }
    }

    @Override
    public void onDestroy() {

        if (isRecording) {

            Toast.makeText(this, "Recorder stopped.",Toast.LENGTH_SHORT).show();
            mRecorder.stop();

        }

        mRecorder.release();
        mPlayer.stop();
        mPlayer.release();
        super.onDestroy();

    }

}

有人能告诉我为什么第三个选项-R.id.draw case-在try中断,我执行那部分代码?我用错误的方法打开文件或类似的东西吗?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

使用FileInputStream.read(byte [])可以更有效地阅读。

通常,您不希望将任意大小的文件读入内存。

大多数解析器都会使用InputStream。也许您可以告诉我们您是如何使用该文件的,我们可以建议更合适。

以下是使用read()的字节缓冲区版本的方法:

byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer));
 }