Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

MediaPlayer starts playing from beginning, on each loop?

How would I fix my MediaPlayer code, that receives bytes, than creates a temp file, that is saving input, but for each input, the player starts from the beggining, and I want it to just play. This is my code:

Java.IO.File temp = Java.IO.File.CreateTempFile("temp", "mp3");
Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(temp);
Java.IO.FileInputStream fis = new Java.IO.FileInputStream(temp);
temp.DeleteOnExit();

MediaPlayer player = new MediaPlayer();
player.SetDataSource(fis.FD);  
// If set here, there is an error
//12-09 17:29:44.472 V/MediaPlayer( 9927): setDataSource(58, 0, 576460752303423487)
//12-09 17:29:44.472 E/MediaPlayer( 9927): Unable to to create media player

while (true)
{
    try
    {
        byte[] myReadBuffer = new byte[10000];
        mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
        fos.Write(myReadBuffer, 0, myReadBuffer.Length);

        MediaPlayer player = new MediaPlayer();
        player.SetDataSource(fis.FD);  // If here, it would just start from the start each time and add more 
        player.Prepare();
        player.Start();
        while (true)
        {
            if (!player.IsPlaying)
            {
                player.Release();
                break;
            }
        }
    }
    catch (System.IO.IOException ex)
    {
        System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
    }
} 

I am using Xamari Forms.

Sign In or Register to comment.