Search code examples
androidmedia-playerprepare

MediaPlayer prepare fail IOException


My question is does having too many MediaPlayers cause an IOException preparefailed: status= 0x1 to log?

The way my program is running is i use a separate instance of Media Player for each video I want to play. At the end of the run i stop the videoPlayer, release it, and turn it null; this is ok sometimes but other times when i move between video's too fast i get a IO exception and the video will not play. I also have a mediaPlayer playing some background music in a service.

Basically my Video activity gets a new call each time a file ends playback. Could this be the error and i should try to just reuse the same media player with a different file?

Thanks in advance


Solution

  • I found my own answer. I don't know if this is a good way to do it but:

    if(videoFile != null)
            {
                Log.i("INITPLAYER", videoFile);
                afd = getAssets().openFd(videoFile);
    
                instructionVideoPlayer = new MediaPlayer();
    
                instructionVideoPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
    
                instructionVideoPlayer.setDisplay(holder);
    
                instructionVideoPlayer.prepare();
    
                instructionVideoPlayer.setOnCompletionListener(instructionVideoComplete);
                instructionVideoPlayer.setOnPreparedListener(this);
            }
            else
                Log.i("VideoPlayer", "noVideoFile");
    
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.i(this.toString(), "IOEXception");
            e.printStackTrace();
    
    //Here is the fix:
            instructionVideoPlayer.release();
            instructionVideoPlayer = null;
            initPlayer();
    // reinit after prepare failed. although this can bring in an infinte loop if video file does not exits
        } catch (Exception e)
        {
            Log.i("InitPlayer", e.getClass().toString());
            e.printStackTrace();
        }