我想从网络服务播放一首歌,我写的代码如下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
startButton = (ImageButton) findViewById(R.id.start);
Intent intent = getIntent();
mp3Path = intent.getStringExtra("mp3Path");
player = new MediaPlayer();
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
if(player != null)
player.stop();
player.setDataSource(mp3Path);
player.prepare();
player.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
但是当我单击该按钮时,它会引发以下异常,并且我被告知有关于准备的错误:
04-13 17:23:13.186: E/MediaPlayer(401): stop called in state 1
04-13 17:23:13.186: E/MediaPlayer(401): error (-38, 0)
04-13 17:23:13.206: E/MediaPlayer(401): error (1, -2147483648)
04-13 17:23:13.206: W/System.err(401): java.io.IOException: Prepare failed.: status=0x1
04-13 17:23:13.226: W/System.err(401): at android.media.MediaPlayer.prepare(Native Method)
04-13 17:23:13.226: W/System.err(401): at com.Android.SampleActivity$1.onClick(SampleActivity.java:50)