MTS
MTS

Reputation: 19

How to stop MediaPlayer from starting each time?

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 beginning, 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]; //Input array
        mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length); //Reads the incoming array into myReadBuffer
        fos.Write(myReadBuffer, 0, myReadBuffer.Length); //Writes it into temp file

        MediaPlayer player = new MediaPlayer(); //Creates a new object
        player.SetDataSource(fis.FD);  // If here, it would just start from the start each time and add more // Sets the data source to temp file

        player.Prepare();
        player.Start();
        while (true)
        {
            // Checks if it can release resources
            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.


Basicaly, I get an array of bytes, that I store in a temporary file, and than try to play them. I know the MediaPlayer is recreated on every loop, since there I define the data source, but if I put it outside of the loop, it would get an error(as above).

Example: The song starts, is played for about 2s and than it starts over but is now played for 4s, and over again and now for 6s. Each time, more of the song is revealed.

If it was a string it would be like this:

123
123456
123456789

How would I make it to play continuously, but each time it would play a only the new part?

Upvotes: 1

Views: 181

Answers (1)

xTwisteDx
xTwisteDx

Reputation: 2472

This is a logic issue. Essentially you're writing chunks to a stream, then playing that chunk, then writing more without resetting the stream, then playing from the beginning of that stream.

What you need it to do is write a chunk to your stream, play that stream, then write a new chunk to the stream and play that stream.

  • Move your Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(temp); inside of your outer while() loop.

The reason this works is that you're writing to your fos then playing, then writing again but not disposing of your initial buffer data. Moving fos into your while loop forces a new object to be created which will contain the new buffer data, then it will play that. There's going to be an issue with audio skipping because of the loop and having to re-load the new data to be played.

To correct the skip you'll need to load your buffer asynchronously while it's being played. You can do this with a separate thread. You might need to adjust your buffer size or set a buffer condition. MediaPlayer contains a BufferingProgress property that might help.

Upvotes: 1

Related Questions