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

后端 未结 15 971
独厮守ぢ
独厮守ぢ 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:01

    I know this may be too late. I managed to use video from resources as this:

    MediaPlayer mp = ...;
    Uri uri = Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.my_movie);
    mp.setDataSourceUri(this, uri);
    
    0 讨论(0)
  • 2020-11-27 18:04

    If you are using multiple instances of MediaPlayer, make sure that you've executed release() on a resource before attempting to use it in a different instance.

    0 讨论(0)
  • 2020-11-27 18:06

    first thanks for your code, the open raw ressource helped me with my problem.

    I'm sorry I don't have a definite answer to your problem but based on the api media example in the SDK and my media player in my project I would suggest checking that the file opens correctly.

    otherwise maybe you need to set the display before preparing as such:

    mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
                mMediaPlayer.setDisplay(holder);
                mMediaPlayer.prepare();
    

    those are the only ideas I have

    hope it helps

    Jason

    0 讨论(0)
  • 2020-11-27 18:08

    To playback a video or audio file located in the raw folder use:

    mMediaPlayer = MediaPlayer.create(this, R.raw.a_video_or_audio_file);
    mMediaPlayer.start(); 
    

    no need to call prepare() when using MediaPlayer.create(Context,R.raw.some_resource_file)

    0 讨论(0)
  • 2020-11-27 18:09

    initilize the fileName, mediaPlayer instance:

    private MediaPlayer mp;
    private final static String fileName = "ring";
    

    for playing/starting audio file from res/raw directory:

            try 
            {   
                mp = new MediaPlayer();
                mp.setAudioStreamType(AudioManager.STREAM_RING); //set streaming according to ur needs
                mp.setDataSource(Context, Uri.parse("android.resource://yourAppPackageName/raw/"+fileName));
                mp.setLooping(true);
                mp.prepare();
                mp.start();
    
            } catch (Exception e) 
            {
                System.out.println("Unable to TunePlay: startRingtone(): "+e.toString());
            }
    

    finally stopping the audioFile:

        try
        {
            if (!(mp == null) && mp.isPlaying())
            {
                mp.stop();
                mp.release(); //its a very good practice
            }       
        }
        catch (Exception e)
        {
            System.out.println("Unable to  TunePlay: stopRingtone():"+e.toString());
        }
    
    0 讨论(0)
  • 2020-11-27 18:13

    I found Best and clear solution for this error : java.io.IOException: Prepare failed.: status=0x1

    Note

    if you want read from your sd card and got this error , your are not set
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in your Manifast .

    Note 2

    if your are want play from url and got that error maybe your are set setDataSource to like this :https://www.android.com just remove S from http like this http://www.android.com

    0 讨论(0)
提交回复
热议问题