开发者

Recording Stream being played on Android

I am developing a application in Android that plays out a Shoutcast stream using the MediaPlayer. I have a requirement of parallely recording the played str开发者_JS百科eam to the SD card in the MP3 format

Is there is a way out in Android? Is there any sample code available to achieve this


I used to do this with the Last.FM player (when it actually worked). It was, however, not a simple means of recording.

Step 1: Write proxy with stream recording function

Step 2: root your phone

Step 3: run on phone:

iptables -t nat -N proxy
iptables -t nat -A OUTPUT -m owner --uid-owner (uid of streaming app) -p tcp -j proxy
iptables -t nat -A proxy -p tcp -j DNAT --to proxyip:port

My 'step 1' was written in perl, and rather messy. For shoutcast, there may be a recording proxy already available.


I am working on something similar and facing the same problem.

I am able to play the stream but i am having trouble with the recording part, i assume i have to read the stream bit by bit and save it to a file. So far this was my approach for the recording.

P.S. Don't forget permissions while recording.

    private void startRecording() {
    String fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    fileName += "/FM-Recording-"+recordFile;
    mRecorder = new MediaRecorder();
    //mRecorder.setAudioSource(mediaPlayer.getAudioSessionID());
    mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setOutputFile(fileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed "+e.toString());
    }

    mRecorder.start();
}

   private class RecorderThread extends Thread {
    public void go(){
        runOnUiThread (new Thread(new Runnable() {
            public void run() {
                isRecording = true;
                startRecording();
            }
        }));
    }
    }

Here is layout: Application Layout


Or implement something like this

  private void startRecording() {

    BufferedOutputStream writer = null;
    try {
        URL url = new URL(RADIO_STATION_URL);
        URLConnection connection = url.openConnection();

        writer = new BufferedOutputStream(new FileOutputStream(new File(fileName)));
        recordingStream = connection.getInputStream();

        final int BUFFER_SIZE = 100;

        byte[] buffer = new byte[BUFFER_SIZE];

        while (recordingStream.read(buffer, 0, BUFFER_SIZE) != -1 && isRecording) {
            writer.write(buffer, 0, BUFFER_SIZE);
            writer.flush();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            recordingStream.close();
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜