Forum Xamarin.Android

Can i use MediaRecorder to record a stream being played by MediaPlayer?

Bottom line. I want to record an input audio stream from the web - saving it to an mp3 or wav - after "listening" for say 30 minutes.

I have successfully implemented a stream being played by MediaPlayer as a background service. Done. Can I combine this with MediaRecorder to record the stream at the same time? If so, what is the technique please? I do not want to record microphone or other possible inputs such as a phone call. I would like to know if i can optionally record this without sound coming through the device (ie. a background recording). Another idea i thought of was a straight converter from an audio stream to an mp3 (or wav) after recording for say 30 minutes - but i have no idea how to do this. It was just an idea.

Can anyone offer some suggestions to me? Thanks in anticipation.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    MediaRecorder uses camera as input to record videos and microphone to record audios.
    If you want to capture the screen content I think the most appropriate control is MediaProjection:
    https://developer.android.com/reference/android/media/projection/MediaProjection
    And this is the thread introduces how to use it:
    https://stackoverflow.com/questions/32513379/how-to-record-screen-and-take-screenshots-using-android-api
    However, if you are using media player to play videos from the internet why not download it from the specific address and store it to the local storage?
    Use HttpClient to achieve it in Xamarin:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest

  • DavidKuchmarDavidKuchmar USMember ✭✭

    Thank you for responding. However, possibly I explained my situation poorly. I am not after capturing video... but a radio station streaming (audio only) and want to save, say a 30 minute segment to mp3 or wav. I am already streaming the radio station in a foreground service successfully. Now, how can i record it.

  • LandLuLandLu Member, Xamarin Team Xamurai

    MediaRecorder will record all the content coming from the microphone.
    So it's not very suitable to record for 30 minutes.
    Why not try to download the stream from the address? Store it to the local storage with mp3 as you want.

  • DavidKuchmarDavidKuchmar USMember ✭✭

    Thank you again for your feedback.

    I think I may of named the thread poorly. My problem is there is radio station audio stream playing continuously. It is not a file per se. I want to save, say 30 minutes of what is broadcast in the stream. I tried copying the file as it was being streamed. That saved the data to a file. But the file was not a valid sound file.

    My problem remains. How can I save a portion of the audio stream to a file?

    My code is:
    public void RecordAudio()
    {
    new System.Threading.Thread(new System.Threading.ThreadStart(() =>
    {
    try
    {
    string url = "the url of the stream"; // it will not let me post the link???
    URL urlStream = new URL(url);
    System.IO.Stream fred = urlStream.OpenStream();
    Java.IO.InputStream inputStream = ((Android.Runtime.InputStreamInvoker)fred).BaseInputStream;
    Java.IO.File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments);
    String filename = path.AbsolutePath;
    filename += "/record.mp3";
    Java.IO.File outputSource = new Java.IO.File(filename);
    Java.IO.OutputStream fileOutputStream = new FileOutputStream(outputSource);
    int bytesRead = -1;
    byte[] buffer = new byte[30 * 1024];
    while ((bytesRead = inputStream.Read(buffer)) > 0)
    {
    if (Profile.ThisKeepRecording)
    {
    byte[] buffer2 = new byte[bytesRead];
    fileOutputStream.Write(buffer2);
    }
    else
    {
    goto Done;
    }
    }
    }
    catch (Exception e)
    {
    string a = e.Message;
    }
    Done:;
    })).Start();
    return;

  • tekkemPhanitekkemPhani Member
    edited March 24

    @DavidKuchmar can you share the source code for broadcasting video/audio with media recorder ?

Sign In or Register to comment.