Android 录音时的声波

发布于 2024-12-22 05:14:20 字数 3168 浏览 0 评论 0原文

如何在android中录制声音时实现声波?

在下面的代码中

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resultView = (TextView) findViewById(R.id.output);

    try {
        // the soundfile
        File storageDir = new File(Environment
                .getExternalStorageDirectory(), "com.hascode.recorders");
        storageDir.mkdir();
        Log.d(APP_TAG, "Storage directory set to " + storageDir);
        outfile = File.createTempFile("hascode", ".3gp", storageDir);

        // init recorder
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(outfile.getAbsolutePath());

        // init player
        player.setDataSource(outfile.getAbsolutePath());
    } catch (IOException e) {
        Log.w(APP_TAG, "File not accessible ", e);
    } catch (IllegalArgumentException e) {
        Log.w(APP_TAG, "Illegal argument ", e);
    } catch (IllegalStateException e) {
        Log.w(APP_TAG, "Illegal state, call reset/restore", e);
    }

    btRecord = (Button) findViewById(R.id.btRecord);
    btRecord.setOnClickListener(handleRecordClick);

    btPlay = (Button) findViewById(R.id.btPlay);
    btPlay.setOnClickListener(handlePlayClick);

}

private final OnClickListener handleRecordClick = new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!recording) {
            startRecord();
        } else {
            stopRecord();
        }
    }
};

private final OnClickListener handlePlayClick = new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!playing) {
            startPlay();
        } else {
            stopPlay();
        }
    }
};

private void startRecord() {
    Log.d(APP_TAG, "start recording..");
    printResult("start recording..");
    try {
        recorder.prepare();
        recorder.start();
        recording = true;
    } catch (IllegalStateException e) {
        Log
                .w(APP_TAG,
                        "Invalid recorder state .. reset/release should have been called");
    } catch (IOException e) {
        Log.w(APP_TAG, "Could not write to sd card");
    }
}

private void stopRecord() {
    Log.d(APP_TAG, "stop recording..");
    printResult("stop recording..");
    recorder.stop();
    recorder.reset();
    recorder.release();
    recording = false;
}

private void startPlay() {
    Log.d(APP_TAG, "starting playback..");
    printResult("start playing..");
    try {
        playing = true;
        player.prepare();
        player.start();
    } catch (IllegalStateException e) {
        Log.w(APP_TAG, "illegal state .. player should be reset");
    } catch (IOException e) {
        Log.w(APP_TAG, "Could not write to sd card");
    }
}

private void stopPlay() {
    Log.d(APP_TAG, "stopping playback..");
    printResult("stop playing..");
    player.stop();
    player.reset();
    player.release();
    playing = false;
}

private void printResult(String result) {
    resultView.setText(result);
}

How to implement sound waves during recording sound in android??

in the below code

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resultView = (TextView) findViewById(R.id.output);

    try {
        // the soundfile
        File storageDir = new File(Environment
                .getExternalStorageDirectory(), "com.hascode.recorders");
        storageDir.mkdir();
        Log.d(APP_TAG, "Storage directory set to " + storageDir);
        outfile = File.createTempFile("hascode", ".3gp", storageDir);

        // init recorder
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(outfile.getAbsolutePath());

        // init player
        player.setDataSource(outfile.getAbsolutePath());
    } catch (IOException e) {
        Log.w(APP_TAG, "File not accessible ", e);
    } catch (IllegalArgumentException e) {
        Log.w(APP_TAG, "Illegal argument ", e);
    } catch (IllegalStateException e) {
        Log.w(APP_TAG, "Illegal state, call reset/restore", e);
    }

    btRecord = (Button) findViewById(R.id.btRecord);
    btRecord.setOnClickListener(handleRecordClick);

    btPlay = (Button) findViewById(R.id.btPlay);
    btPlay.setOnClickListener(handlePlayClick);

}

private final OnClickListener handleRecordClick = new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!recording) {
            startRecord();
        } else {
            stopRecord();
        }
    }
};

private final OnClickListener handlePlayClick = new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!playing) {
            startPlay();
        } else {
            stopPlay();
        }
    }
};

private void startRecord() {
    Log.d(APP_TAG, "start recording..");
    printResult("start recording..");
    try {
        recorder.prepare();
        recorder.start();
        recording = true;
    } catch (IllegalStateException e) {
        Log
                .w(APP_TAG,
                        "Invalid recorder state .. reset/release should have been called");
    } catch (IOException e) {
        Log.w(APP_TAG, "Could not write to sd card");
    }
}

private void stopRecord() {
    Log.d(APP_TAG, "stop recording..");
    printResult("stop recording..");
    recorder.stop();
    recorder.reset();
    recorder.release();
    recording = false;
}

private void startPlay() {
    Log.d(APP_TAG, "starting playback..");
    printResult("start playing..");
    try {
        playing = true;
        player.prepare();
        player.start();
    } catch (IllegalStateException e) {
        Log.w(APP_TAG, "illegal state .. player should be reset");
    } catch (IOException e) {
        Log.w(APP_TAG, "Could not write to sd card");
    }
}

private void stopPlay() {
    Log.d(APP_TAG, "stopping playback..");
    printResult("stop playing..");
    player.stop();
    player.reset();
    player.release();
    playing = false;
}

private void printResult(String result) {
    resultView.setText(result);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-12-29 05:14:20

您想将录制的声音与预先录制的波浪声合并吗?

如果是这样,也许您可​​以尝试创建两个不同来源的声音对象。然后你也许可以间隔写入输出文件。一种是预先录制的间隔,一种是新录制的间隔。

我实际上不知道这是否是一种有效的方法。我的一般方法就是面对这个问题。

Do you want to merge your recorded sound with a pre-recorded sound of waves?

If so, maybe you could try to create two sound objects of the different sources. Then you maybe could write to the output file in intervals. One interval for the pre-recorded and one for the fresh one.

I actually don't know if that's even a valid way to do this. Just what my general approach would've been facing this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文