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.

Checking if MediaPlayer is playing couses a short stop?

I have this code, that runs in the background and when I receive my part of a song, it is than being played. I than save it to a temp mp3 file, and than it is played, but between parts, there is a small stop. I use while, to check if a song is playing, so I can release the resources. How would I remove that stop. I am using Xamarin.Forms (that is why there are Java libraries).

This is my code:

while (true)
{
    try
    {
        byte[] myReadBuffer = new byte[10000];
        MediaPlayer player = new MediaPlayer();
       Java.IO.File temp = Java.IO.File.CreateTempFile("temp", "txt");
       Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(temp);
       Java.IO.FileInputStream fis = new Java.IO.FileInputStream(temp);
       temp.DeleteOnExit();
       mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
       fos.Write(myReadBuffer, 0, myReadBuffer.Length);
       fos.Close();
       player.SetDataSource(fis.FD);
       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 did try Thread.Sleep(), but I think that is kinda not so efficient.

Answers

Sign In or Register to comment.