Android MediaPlayer throwing “Prepare failed.: status=0x1” on 2.1, works on 2.2

后端 未结 15 1031
独厮守ぢ
独厮守ぢ 2020-11-27 17:17

I\'ve been really banging my head against the table trying to get the MediaPlayer class to try to play h.264-encoded videos on Android 2.1. My code is rather simple:

<
15条回答
  •  自闭症患者
    2020-11-27 18:16

    I know that I am late here but hopefully this helps somebody else. I worked around this issue by setting up a setOnCompletionListener callback that explicitly releases the MediaPlayer object after the media is done playing.

    I can't take credit for this solution, as it was originally posted by Ronny here: How do you detect when a sound file has finished?

    But since this is the first answer when I search for Android+Failed Status 0x1 here is my code that solved this problem for me:

     public void playSound(String soundPath){
        MediaPlayer m = new MediaPlayer();
    
        m.setOnCompletionListener(new OnCompletionListener() {
    
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
    
        });
    
        try {
    
            AssetFileDescriptor descriptor = mContext.getAssets().openFd(soundPath);
            m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                            descriptor.getLength());            
    
            descriptor.close();
    
            m.prepare();
            m.setVolume(100f, 100f);
            m.setLooping(false);
            m.start();
    
        } catch (Exception e) {
            //Your catch code here.
        }
     }   
    

提交回复
热议问题