2

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 ?

Dexter
  • 1,710
  • 2
  • 17
  • 34

1 Answers1

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.

Dexter
  • 1,710
  • 2
  • 17
  • 34
  • I have a stream and i am write it to the RandomAccessFile but if i play the file the audio has much artifacts so bad audio. Do you know what is wrong? – Timay Jul 13 '15 at 20:50
  • 1
    Timay don't use the method I wrote above. Use custom decoding and playback using audio track. OR try creating an http server on local host to forward the stream to the media player. – Dexter Jul 13 '15 at 21:58