java – 如何拦截Android设备上的音频流?

前端之家收集整理的这篇文章主要介绍了java – 如何拦截Android设备上的音频流?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们假设我们有以下情况:某些东西正在 Android设备上播放(一个mp3的例子,但它可以是使用Android设备的音频部分的任何东西).从应用程序(android应用程序:)),我想拦截音频流来分析它,记录它等.从这个应用程序(让我们说“分析器”)我不想启动一个mp3或某事所有我想要的是访问android的音频流.

任何建议是赞赏,它可以是Java或C解决方案.

解决方法

http://developer.android.com/reference/android/media/MediaRecorder.html
public class AudioRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    final String path;

    /**
     * Creates a new audio recording at the given path (relative to root of SD
     * card).
     */
    public AudioRecorder(String path) {
        this.path = sanitizePath(path);
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + path;
    }

    /**
     * Starts a new recording.
     */
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted.  It is " + state
                    + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

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

    /**
     * Stops a recording that has been prevIoUsly started.
     */
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }
}

猜你在找的Android相关文章