9

My favorite Radio station plays a radio audio stream in mp3 format. In my android application I can receive and play it without any problem.

How can I implement a recording function? I want to record the mp3 radio stream to my Android phones SD-Card.

I tried the MediaRecorder Class without any result...

Android Developer: MediaRecorder

...

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

...

Unfortunately I cannot choose something like:

mRecorder.setAudioSource(MediaRecorder.AudioSource.MP3_STREAM); ... ;-)

How can I record a mp3 radio stream? Thanks for any help or code snippets...

Jonas
  • 121,568
  • 97
  • 310
  • 388
user4500
  • 667
  • 2
  • 9
  • 21

2 Answers2

23

Perfect! Reading the audio stream "byte by byte" is the solution. Thank you!!

Here my code snippets:

        URL url = new URL("http://myradio,com/stream.mp3");
        inputStream = url.openStream();
        Log.d(LOG_TAG, "url.openStream()");

        fileOutputStream = new FileOutputStream(outputSource);
        Log.d(LOG_TAG, "FileOutputStream: " + outputSource);

        int c;

        while ((c = inputStream.read()) != -1) {
            Log.d(LOG_TAG, "bytesRead=" + bytesRead);
            fileOutputStream.write(c);
            bytesRead++;
        }
user4500
  • 667
  • 2
  • 9
  • 21
  • have u created an app for this? – Diff.Thinkr May 05 '12 at 17:23
  • How have you used this for the MediaPlayer to use this AND save the files at the same time? Also, how is "bytesRead" is set? – Antebios Jun 21 '13 at 05:42
  • Playing and recordig are both async tasks. You can just start both without problems. bytesRead is just a counter (int) for debugging. – user4500 Aug 23 '13 at 06:10
  • Have you face problem that recorder is recording music fast than player playing? using this byte by byte approch? – Dharmendra Jun 26 '14 at 16:12
  • No I did not face this problem. I just read the inputStream as it comes in and it will be written in a FileOutputStream. The resulting mp3 plays fine. – user4500 Jun 29 '14 at 09:34
  • I tried to read stream data but I got inputStream.read()) == -1. Do you have any idea ? – Zappy.Mans Jan 03 '19 at 02:36
  • Not really an idea. Maybe it's an issue when your URL starts with "https". Some Android versions needs to handle this special. – user4500 Jan 03 '19 at 15:47
9

Maybe you should read audio stream "byte by byte" and then place it into new file? Like a simple file copy operation, using inputstream-outputstream.

  • 1
    Perfect! Reading the audio stream "byte by byte" is the solution. Thank you!! – user4500 Mar 21 '11 at 21:48
  • I tried https://stackoverflow.com/a/5384161/5856328 to read stream data but I got inputStream.read()) == -1 why data is streaming. Do you have any idea? – Zappy.Mans Jan 03 '19 at 02:35