Android 上正在播放的录制流

发布于 2024-10-30 14:42:37 字数 124 浏览 0 评论 0原文

我正在 Android 中开发一个应用程序,它使用 MediaPlayer 播放 Shoutcast 流。我需要将播放的流以 MP3 格式并行录制到 SD 卡

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 stream 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

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

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

发布评论

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

评论(3

岁吢 2024-11-06 14:42:38

我曾经使用 Last.FM 播放器执行此操作(当它实际工作时)。然而,这并不是一种简单的记录方式。

第1步:编写带有流录制功能的代理

第2步:root你的手机

第3步:在手机上运行:

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

我的“第1步”是用perl编写的,相当混乱。对于shoutcast,可能已有可用的录制代理。

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.

梦中楼上月下 2024-11-06 14:42:38

我正在做类似的事情并面临同样的问题。

我可以播放流,但我在录制部分遇到问题,我想我必须一点一点地读取流并将其保存到文件中。到目前为止,这就是我的录音方法。

PS 录制时不要忘记权限。

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

这是布局:
应用程序布局

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

恬淡成诗 2024-11-06 14:42:38

或者实施这样的事情

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

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