使用android媒体播放器播放更改的文件。

时间:2022-02-19 14:46:37

is there anyway to play a file that is being dynamically added to, using android media player ?

是否存在使用android媒体播放器动态添加的文件?

I tried using RandomAccessFile :

我尝试使用RandomAccessFile:

public static RandomAccessFile tempMp3;
tempMp3 = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/ggg", "rw");
StaticData.tempMp3.setLength(file_size);
StaticData.tempMp3.seek(0);

//Here I call the auto update function and after X bytes are written I call playMediaPlayer

//这里我调用了自动更新函数,在写完X字节后,我调用了playMediaPlayer。

StaticData.mediaPlayer.setDataSource(StaticData.tempMp3.getFD());
StaticData.mediaPlayer.prepare();
StaticData.mediaPlayer.start();

So basically I am updating and playing at the same time. The problem is that MediaPlayer and my Update function both are accessing and moving the file pointer, which messes up the MediaPlayer.

所以基本上我同时在更新和播放。问题是,MediaPlayer和我的更新函数都在访问和移动文件指针,这使MediaPlayer混乱。

Is there any workaround ? Can I use separate file pointers ?

有变通办法吗?我可以使用单独的文件指针吗?

1 个解决方案

#1


0  

Solved this.

解决了这个问题。

Java NIO to the rescue. Using memory-mapped IO, we can write to our file, without disturbing the file pointer, so the android media player is happy. Here is a helpful link. The trick is to set the size of our MappedByteBuffer = size of music file. We also need to ensure that the media player pauses if we have not yet written to the buffer. This can be achieved by monitoring the file-pointer.

Java NIO来拯救。使用内存映射IO,我们可以写入文件,而不打扰文件指针,因此android媒体播放器是快乐的。这里有一个有用的链接。诀窍是设置MappedByteBuffer的大小=音乐文件的大小。我们还需要确保媒体播放器暂停,如果我们还没有写到缓冲区。这可以通过监视文件指针来实现。

#1


0  

Solved this.

解决了这个问题。

Java NIO to the rescue. Using memory-mapped IO, we can write to our file, without disturbing the file pointer, so the android media player is happy. Here is a helpful link. The trick is to set the size of our MappedByteBuffer = size of music file. We also need to ensure that the media player pauses if we have not yet written to the buffer. This can be achieved by monitoring the file-pointer.

Java NIO来拯救。使用内存映射IO,我们可以写入文件,而不打扰文件指针,因此android媒体播放器是快乐的。这里有一个有用的链接。诀窍是设置MappedByteBuffer的大小=音乐文件的大小。我们还需要确保媒体播放器暂停,如果我们还没有写到缓冲区。这可以通过监视文件指针来实现。