简体   繁体   English

如何阻止 MediaPlayer 每次启动?

[英]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.我将如何修复接收字节的 MediaPlayer 代码,而不是创建一个临时文件,即保存输入,但对于每个输入,播放器从头开始,我希望它只是播放。 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.我正在使用 Xamari 表单。


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).我知道 MediaPlayer 在每个循环中都会重新创建,因为我在那里定义了数据源,但是如果我把它放在循环之外,它会得到一个错误(如上)。

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.示例:歌曲开始播放,播放了大约 2 秒,然后重新开始播放,但现在播放了 4 秒,然后又播放了 6 秒。 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?我如何让它连续播放,但每次都只播放新的部分?

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);移动你的Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(temp); inside of your outer while() loop.在外部while()循环内。

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.这样做的原因是您正在写入您的fos然后播放,然后再次写入但不处理您的初始缓冲区数据。 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.fos移动到您的 while 循环中会强制创建一个包含新缓冲区数据的新对象,然后它将播放该对象。 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. MediaPlayer包含一个可能有帮助的BufferingProgress属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM