I am developing a media player that should be able to play audio from the Internet. As I already noted, the standard MediaPlayer rather unstable when playing remote content: it often crashes or stops playing for no reason. On the other hand, I want to implement multimedia caching. But I did not find a way to get buffered content from MediaPlayer to save it somewhere on the device.
So, I would like to implement buffering for myself and make MediaPlayer play only local media. The first way that I see is to upload a sufficient number of media files to a file, start playing this file using MediaPlayer and continue downloading the rest of the media file (with a pause when the media file is not loaded enough and other similar events are processed).
The question is, can I play the file and add it at the same time? Now I have IllegalStateExceptions when calling MediaPlayer setDataSource
UPD
IllegalStateException was IllegalStateException by invalid logic. MediaPlayer is playing now.
But another problem arose.
So, the first 5% of the tracks load, MediaPlayer starts playing, plays those 5%, and then stops. Is this a way to keep playing?
Starting playback is trivial:
private void startPlayback(final File file) { try { FileInputStream fs = new FileInputStream(file); mPlayer.setDataSource(fs.getFD()); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(e); } }
Maybe a different type of stream is useful for this case?
android android-mediaplayer
darja
source share