tags:

views:

68

answers:

1

Let's suppose that we have the following scenario: something is playing on an android device (an mp3 par example, but it could be anything that use the audio part of an android device). From an application (android application :) ), I would like to intercept the audio stream to analyze it, to record it, etc. From this application (let's say "the analyzer") I don't want to start an mp3 or something, all I want is to have access to the audio stream of android.

Any advice is appreciated, it could a Java or C++ solution.

A: 

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();
}

}

androidworkz
Thank you very much for your answer, but with your suggestion I can only access the microphone audio source. How can I access the audio stream that is sent to speaker phone or headphones when another application play a mp3 par example? Or how can I access the audio stream without limiting my options to microphone?
have you tried using MediaRecorder.AudioSource.DEFAULT?
androidworkz
Yes, I tried MediaRecorder.AudioSource.DEFAULT, but it only give me the microphone audio stream.
I found this today... I am not sure if it will help you or nothttp://github.com/esnyder/callrecorder/tree/f0073c0a0aa42b64ae7bbc4a906dcae6b38cd14cPrior to API v4 the possible audio source for recording audio on thehandset was limited to the microphone; in order to do call recordingpeople resorted to putting the phone in speakerphone mode and justaccepting the crappy recording quality.API v4 introduced 3 new sources:VOICE_UPLINKVOICE_DOWNLINKVOICE_CALL
androidworkz