有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android媒体播放器:Url设置不正确

我尝试了很多从localhost路径播放音乐,但它没有播放我得到了一个错误,因为uri没有正确给出,这意味着它将在下面的代码中捕获块

我在xml文件中设计了两个按钮,分别是开始和停止,然后在java文件中填充uri。有人能帮我纠正下面的代码吗

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;
    }
}}

每次我在捕捉块上着陆时异常 我查了我的航海日志,它告诉我 enter image description here

我没有使用Gpu,因为我会得到渲染错误,是因为这个还是我没有在程序中使用上面uri的语法


共 (0) 个答案