EZMusicBoxSDK/src/main/java/com/wms/ezmusicbox/sdk/streaming/StreamingMediaPlayer.java
/**
     * 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.
     */
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.
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 {
	    	boolean wasPlaying = false;
		    int curPosition = 0;
		    if(mediaPlayer != null) {
			    wasPlaying = mediaPlayer.isPlaying();
			    curPosition = mediaPlayer.getCurrentPosition();
		    }
	    	File oldBufferedFile = new File(FileUtil.getAppExternalStoragePath(context), "playingMedia" + counter + ".dat");
	    	File bufferedFile = new File(FileUtil.getAppExternalStoragePath(context), "playingMedia" + (counter++) + ".dat");
	    	bufferedFile.deleteOnExit();   
	    	moveFile(mediaFile, bufferedFile);
		    if(mediaPlayer != null) {
			    mediaPlayer.pause();
			    mediaPlayer = createMediaPlayer(bufferedFile);
			    mediaPlayer.seekTo(curPosition);
			    boolean atEndOfFile = mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition() <= 1000;
			    if (wasPlaying || atEndOfFile) {
				    mediaPlayer.start();
			    }
		    }
	    	oldBufferedFile.delete();
	    }
	    catch (Exception e) {
	    	Log.e(getClass().getName(), "Error updating to newly loaded content.", e);            		
		}
    }
private void transferBufferToMediaPlayer ( ) { try { boolean wasPlaying = false ; int curPosition = 0 ; if ( mediaPlayer != null ) { wasPlaying = mediaPlayer . isPlaying ( ) ; curPosition = mediaPlayer . getCurrentPosition ( ) ; } File oldBufferedFile = new File ( FileUtil . getAppExternalStoragePath ( context ) , "playingMedia" + counter + ".dat" ) ; File bufferedFile = new File ( FileUtil . getAppExternalStoragePath ( context ) , "playingMedia" + ( counter ++ ) + ".dat" ) ; bufferedFile . deleteOnExit ( ) ; moveFile ( mediaFile , bufferedFile ) ; if ( mediaPlayer != null ) { mediaPlayer . pause ( ) ; mediaPlayer = createMediaPlayer ( bufferedFile ) ; mediaPlayer . seekTo ( curPosition ) ; boolean atEndOfFile = mediaPlayer . getDuration ( ) - mediaPlayer . getCurrentPosition ( ) <= 1000 ; if ( wasPlaying || atEndOfFile ) { mediaPlayer . start ( ) ; } } oldBufferedFile . delete ( ) ; } catch ( Exception e ) { Log . e ( getClass ( ) . getName ( ) , "Error updating to newly loaded content." , e ) ; } }
Transfer buffered data to the MediaPlayer.
Transfer buffered data to the MediaPlayer .
// First determine if we need to restart the player after transferring data, e.g. perhaps the user pressed pause // Copy the currently downloaded content to a new buffered File. Store the old File for deleting later. // 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. // 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 // Create a new MediaPlayer rather than try to re-prepare the prior one. //  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 // Lastly delete the previously playing buffered File as it's no longer needed.