简体   繁体   English

使用Android Media Player播放更改的文件

[英]Playing a changing file using android media player

is there anyway to play a file that is being dynamically added to, using android media player ? 反正有使用Android Media Player播放动态添加到的文件吗?

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和我的Update函数都正在访问和移动文件指针,这弄乱了MediaPlayer。

Is there any workaround ? 有什么解决方法吗? Can I use separate file pointers ? 我可以使用单独的文件指针吗?

Solved this. 解决了这个。

Java NIO to the rescue. Java NIO可以解救。 Using memory-mapped IO, we can write to our file, without disturbing the file pointer, so the android media player is happy. 使用内存映射的IO,我们可以在不干扰文件指针的情况下写入文件,因此android媒体播放器很高兴。 Here is a helpful link . 这是一个有用的链接 The trick is to set the size of our MappedByteBuffer = size of music file. 诀窍是设置MappedByteBuffer的大小=音乐文件的大小。 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. 这可以通过监视文件指针来实现。

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

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