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

后端 未结 15 1088
独厮守ぢ
独厮守ぢ 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: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());
        }
    

提交回复
热议问题