我尝试了很多从localhost路径播放音乐,但它没有播放我收到错误,因为uri没有正确给出意味着它将在下面的代码中捕获块
我在xml文件中设计了2个按钮,这是启动和停止然后在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;
}
}}
每次我登陆拦截区异常 我查了一下它的logcat
我没有使用Gpu,因为我会得到渲染错误是因为它或我没有在程序中使用uri的语法