Answer the question
In order to leave comments, you need to log in
Media player, saving the cache to a memory card - where to dig?
I need to save the files that the media player plays in the cache to a folder. There are no problems with simple sound files. There is a program and a class that does this. And it does it in a strange way, but it works.
Big problems with video files.
Firstly, I wanted to take the easy route and use the mega item ExoPlayer from Goodl. But it does not play my file and apparently many others
https://github.com/google/ExoPlayer/issues/19
Secondly, I tried to make my streaming to a folder in the same way as music. Here is my code:
This class starts the download:
/**
* Download the url stream to a temporary location and then call the setDataSource
* for that local file
*/
public void downloadAudioIncrement(String mediaUrl) throws IOException {
URLConnection cn = new URL(mediaUrl).openConnection();
path=mediaUrl;
cn.connect();
sizef=cn.getContentLength();
InputStream stream = cn.getInputStream();
if (stream == null) {
Log.e(getClass().getName(), "Unable to create InputStream for mediaUrl:" + mediaUrl);
}
downloadingMediaFile = new File(mContext.getCacheDir(),"downloadingMedia.dat");
// Just in case a prior deletion failed because our code crashed or something, we also delete any previously
// downloaded file to ensure we start fresh. If you use this code, always delete
// no longer used downloads else you'll quickly fill up your hard disk memory. Of course, you can also
// store any previously downloaded file in a separate data cache for instant replay if you wanted as well.
if (downloadingMediaFile.exists()) {
downloadingMediaFile.delete();
}
FileOutputStream out = new FileOutputStream(downloadingMediaFile);
byte buf] = new byte[16384];
int totalBytesRead = 0, incrementalBytesRead = 0;
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
totalBytesRead += numread;
incrementalBytesRead += numread;
totalKbRead = totalBytesRead/1000;
testMediaBuffer();
//fireDataLoadUpdate();
} while (validateNotInterrupted());
stream.close();
if (validateNotInterrupted()) {
// fireDataFullyLoaded();
}
}
/**
* Test whether we need to transfer buffered data to the MediaPlayer.
* Interacting with MediaPlayer on non-main UI thread can causes crashes to so perform this using a Handler.
*/
private void testMediaBuffer() {
Runnable updater = new Runnable() {
public void run() {
if (mMediaPlayer == null) {
// Only create the MediaPlayer once we have the minimum buffered data
if ( totalKbRead >= INTIAL_KB_BUFFER) {
try {
//mUri=Uri.parse(path);
openVideo();
} catch (Exception e) {
Log.e(getClass().getName(), "Error copying buffered conent.", e);
}
}
} else if ((totalKbRead - pfile) >= 4000){
// NOTE: The media player has stopped at the end so transfer any existing buffered data
// We test for < 1second of data because the media player can stop when there is still
// a few milliseconds of data left to play
transferBufferToMediaPlayer();
}
}
};
handler.post(updater);
}
/**
* Transfer buffered data to the MediaPlayer.
* NOTE: Interacting with a MediaPlayer on a non-main UI thread can cause thread-lock and crashes so
* this method should always be called using a Handler.
*/
private void transferBufferToMediaPlayer() {
try {
// First determine if we need to restart the player after transferring data...e.g. perhaps the user pressed pause
boolean wasPlaying = mMediaPlayer.isPlaying();
// Copy the currently downloaded content to a new buffered File. Store the old File for deleting later.
File oldBufferedFile = new File(mContext.getCacheDir(),"playingMedia" + counter + ".dat");
File bufferedFile = new File(mContext.getCacheDir(),"playingMedia" + (counter++) + ".dat");
// This may be the last buffered File so ask that it be delete on exit. If it's already deleted, then this won't mean anything. If you want to
// keep and track fully downloaded files for later use, write caching code and please send me a copy.
bufferedFile.deleteOnExit();
moveFile(downloadingMediaFile,bufferedFile);
// Pause the current player now as we are about to create and start a new one. So far (Android v1.5),
// this always happens so quickly that the user never realized we've stopped the player and started a new one
mMediaPlayer.pause();
int curPosition = mMediaPlayer.getCurrentPosition();
//mMediaPlayer.release();
//mMediaPlayer=null;
// Create a new MediaPlayer rather than try to re-prepare the prior one.
FileInputStream fis = new FileInputStream(bufferedFile);
mMediaPlayer.reset();
//mMediaPlayer = new MediaPlayer();
/*if (mAudioSession != 0) {
mMediaPlayer.setAudioSessionId(mAudioSession);
} else {
mAudioSession = mMediaPlayer.getAudioSessionId();
}
setListeners();*/
pfile=bufferedFile.length()/1024;
mMediaPlayer.setDataSource(fis.getFD());
//mMediaPlayer.setSurface(new Surface (sf));
// mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepare();
// we don't set the target state here either, but preserve the
// target state that was there before.
// mCurrentState = STATE_PREPARING;
//attachMediaController();
mMediaPlayer.seekTo(curPosition);
//mMediaPlayer.start();
// Restart if at end of prior buffered content or mediaPlayer was previously playing.
// NOTE: We test for < 1second of data because the media player can stop when there is still
// a few milliseconds of data left to play
/*boolean atEndOfFile = (totalKbRead - pfile) >= 1000;
if (wasPlaying || atEndOfFile){
mMediaPlayer.start();
}*/
// Lastly delete the previously playing buffered File as it's no longer needed.
oldBufferedFile.delete();
}catch (Exception e) {
Log.e(getClass().getName(), "Error -------------------------------------------------------", e);
}
}
Answer the question
In order to leave comments, you need to log in
in general, I dig towards vitamio. There are many different players written on it.
I dug and realized that the month was lost in vain.
See the correct answer here How to save the cache in MediaPlayer?
Or right here https://github.com/master255/ImmortalPlayer
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question