我在使用 MediaPlayer API 播放一首简单的歌曲(存储在外部 SD 中)时遇到了一些问题。我真的不知道我做错了什么,因为我没有在任何其他问题中看到代码错误,并且使用开发人员我也没有找到它。
我得到的错误是:
08-25 00:20:49.514: D/MediaPlayer(26764): mPlayerID = 94
08-25 00:20:49.519: E/MediaPlayer(26764): error (1, -2147483648)
08-25 00:20:49.597: E/MediaPlayer(26764): Error (1,-2147483648)
音频活动.java
public class AudioActivity extends Activity{
Audio audioPlayer;
public AudioActivity(){
audioPlayer = new Audio();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set of view where videos and the listView (is there is any) will appear
setContentView(R.layout.audio_player);
Button play = (Button)findViewById(R.id.playButton);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory() + "/" + "song";
audioPlayer.loadFile(path);
audioPlayer.prepare();
audioPlayer.play();
}
});
Button pauseResume = (Button)findViewById(R.id.pauseButton);
pauseResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
audioPlayer.pause();
}
});
Button stop = (Button)findViewById(R.id.stopButton);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
audioPlayer.stop();
}
});
}
}
音频类
public class Audio {
MediaPlayer mediaPlayer;
String pathSong;
public Audio(){
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
Log.d("Error", "Arg1: " + arg1 + ".Arg2: " + arg2);
mediaPlayer.reset();
return false;
}
});
}
public void loadFile(String path){
try {
mediaPlayer.setDataSource(path);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void prepare(){
try {
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
public void stop(){
mediaPlayer.stop();
}
public void pause(){
mediaPlayer.pause();
}
public int getDuration(){
return mediaPlayer.getDuration();
}
public MediaPlayer getMediaPlayer(){
return mediaPlayer;
}
public TrackInfo[] getTrackInfo(){
return mediaPlayer.getTrackInfo();
}
}
任何想法?为什么代码 (1, -2147483648) 不出现在任何地方?谢谢!