Android Media Player: Url is not set properly

2019-08-26 02:16发布

i tried a lot to play the music from localhost path but it is not playing i am getting an error as uri is not correctly given means it is going to catch block in the below code

i have designed as this in xml file 2 buttons which is start and stop then populated the uri in java file can anyone help me in correcting the code below

public class MusicAndroidActivity extends Activity {
static MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonPlay = (Button) findViewById(R.id.play);
    buttonPlay.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Uri myUri = Uri.parse("http://192.168.1.7/Android/music/vande.mp3");
            mPlayer = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mPlayer.setDataSource(getApplicationContext(), myUri);
            } catch (IllegalArgumentException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (SecurityException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            }
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {                        
                    //mp.start();                       
                    mPlayer.start();
                }
            });

        }
    });

    buttonStop = (Button) findViewById(R.id.stop);
    buttonStop.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            if(mPlayer!=null && mPlayer.isPlaying()){
                mPlayer.stop();
            }
        }
    });
}

protected void onDestroy() {
    super.onDestroy();

    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}}

everytime i land on catch block exception i checked my logcat it is telling as enter image description here

i havent used the Gpu because i will get rendering error is it because of that or i didn't use the syntax for uri above in the program

0条回答
登录 后发表回答