is there anyway to play a file that is being dynamically added to, using android media player ?
I tried using 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
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.
Is there any workaround ? Can I use separate file pointers ?
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.