我在prepareAsync方法中遇到错误(1,-4)。文档指定这是“不正确的文件格式”(对吗?)但文件是mp3格式。此外,当在另一个Activity中调用mediaplayer时,在旧版本的软件中复制了相同的文件。 我不知道如何解决问题所以我在这里。
该应用程序的代码如下所示:
SingletonMediaPlayer 将管理在应用程序中用作单例的媒体播放器对象
public class SingletonMediaPlayer {
private static SingletonMediaPlayer instance;
private MediaPlayer mp;
private int buffer_state;
private SingletonMediaPlayer() {
Log.d("SMP","Creating new media player");
this.mp = new MediaPlayer();
this.buffer_state = 0;
}
public static SingletonMediaPlayer getInstance() {
if (instance == null) {
instance = new SingletonMediaPlayer();
}
return instance;
}
public void play(String path, final TextView tv_messaging){
if(this.mp.isPlaying()){
Log.d("SMP","Player is playing, now I'll stop and reset it");
this.mp.stop();
this.mp.reset();
this.mp.release();
}
Log.d("SMP","Set audio stream type");
this.mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Log.d("SMP","Set data source");
this.mp.setDataSource(path);
Log.d("SMP","Prepare async");
this.mp.prepareAsync();
Log.d("SMP","Done!");
tv_messaging.setText("Connecting to the server...please wait");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv_messaging.setText(e.toString());
Log.e("SMP","IllegalArgumentException");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv_messaging.setText(e.toString());
Log.e("SMP","SecurityException");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv_messaging.setText(e.toString());
Log.e("SMP","IllegalStateException");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv_messaging.setText(e.toString());
Log.e("SMP","IOException");
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv_messaging.setText(e.toString());
Log.e("SMP","Generic Exception");
}
}
public void checkBufferState(final TextView tv_buffer_message){
final SingletonMediaPlayer self = this;
Log.d("SMP"," Set on prepared listener");
this.mp.setOnErrorListener(new OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
});
this.mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mPlayer) {
OnBufferingUpdateListener lis = new OnBufferingUpdateListener(){
public void onBufferingUpdate(MediaPlayer mPlayer, int percent) {
Log.d("SMP"," Mediaplayer ready (preparation done). Inside buffer listener");
self.buffer_state = percent;
if(tv_buffer_message != null){
tv_buffer_message.setText(percent+"%");
}
}
};
Log.d("SMP"," Mediaplayer ready (preparation done). Installing buffer listener");
mPlayer.setOnBufferingUpdateListener(lis);
Log.d("SMP"," Mediaplayer ready (preparation done). Starting reproduction");
mPlayer.start();
Log.d("SMP"," Mediaplayer ready (preparation done). Done!");
}
});
}
public int getBufferState(){
return this.buffer_state;
}
}
我正在创建按钮,当点击按钮时应播放mp3。所以,就在用于创建按钮的循环之前,我有
final SingletonMediaPlayer mediaPlayer = SingletonMediaPlayer.getInstance();
mediaPlayer.checkBufferState(tv_sel_ep);
而且,对于每个按钮,我都有
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.play(link, Text_view_used_to_show_error);
}
});
日志
02-15 14:35:24.165: D/dalvikvm(1415): GC_EXTERNAL_ALLOC freed 58K, 52% free 2633K/5379K, external 391K/517K, paused 87ms
02-15 14:35:25.705: D/NetworkActivity(1415): Starting SelectEpisodeActivity
02-15 14:35:25.995: D/SMP(1415): Creating new media player
02-15 14:35:26.005: D/SMP(1415): Set on prepared listener
02-15 14:35:28.745: W/KeyCharacterMap(1415): No keyboard for id 0
02-15 14:35:28.745: W/KeyCharacterMap(1415): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-15 14:35:28.745: I/qqqqqqqqqqqqq(1415): qqqqqqqqqqqqqqqqqqqq
02-15 14:35:29.235: D/dalvikvm(1415): GC_CONCURRENT freed 307K, 53% free 2669K/5639K, external 477K/989K, paused 8ms+12ms
02-15 14:35:30.275: D/NetworkActivity(1415): Starting SelectEpisodeActivity
02-15 14:35:30.415: D/SMP(1415): Set on prepared listener
02-15 14:35:32.485: D/SMP(1415): Set audio stream type
02-15 14:35:32.485: D/SMP(1415): Set data source
02-15 14:35:32.785: D/SMP(1415): Prepare async
02-15 14:35:32.785: D/SMP(1415): Done!
02-15 14:35:32.885: W/MediaPlayer(1415): info/warning (1, 26)
02-15 14:35:32.885: I/MediaPlayer(1415): Info (1,26)
02-15 14:35:32.885: E/MediaPlayer(1415): error (1, -4)
02-15 14:35:32.885: E/MediaPlayer(1415): Error (1,-4)
编辑:戴夫回答后的新日志
02-16 14:35:11.265: D/dalvikvm(12723): GC_EXTERNAL_ALLOC freed 59K, 52% free 2633K/5379K, external 391K/517K, paused 133ms
02-16 14:35:13.265: D/NetworkActivity(12723): Starting SelectEpisodeActivity
02-16 14:35:13.405: D/SMP(12723): Creating new media player
02-16 14:35:13.415: D/SMP(12723): Set on prepared listener
02-16 14:35:17.365: D/SMP(12723): Set audio stream type
02-16 14:35:17.365: D/SMP(12723): Set data source
02-16 14:35:17.435: D/SMP(12723): Prepare async
02-16 14:35:17.435: D/SMP(12723): Done!
02-16 14:35:17.445: W/MediaPlayer(12723): info/warning (1, 26)
02-16 14:35:17.445: E/MediaPlayer(12723): error (1, -4)
02-16 14:35:17.465: I/MediaPlayer(12723): Info (1,26)
02-16 14:35:17.465: E/MediaPlayer(12723): Error (1,-4)
感谢您的帮助
答案 0 :(得分:1)
夫妻俩。
1)如果您希望release()
之后仍处于可用状态,则不应致电MediaPlayer
。 -4错误(如果我没记错的话)是一个无效的状态转换,尝试使用已发布的MediaPlayer
应该会产生错误。
2)您可以在创建onPrepared
时设置onError
,onBufferingUpdate
和MediaPlayer
个侦听器一次。你现在在做什么似乎很尴尬和不必要。
3)遇到Android问题时发布logcat输出。
如果你只做#1,你可能会得到一些可行的东西。不过,在实际使用调用回调的方法之前设置监听器是一个好主意。
编辑:
private static SingletonMediaPlayer instance;
private MediaPlayer mp;
private int buffer_state;
// Add a member for the TextView
private TextView tv_buffer_message;
private SingletonMediaPlayer() {
Log.d("SMP","Creating new media player");
mp = new MediaPlayer();
Log.d("SMP","Set audio stream type");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnErrorListener(new OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int arg1, int arg2) {
// YOU SHOULD REALLY DO SOMETHING HERE!!! IT COULD BE INFORMATIVE!!!
return false;
}
});
Log.d("SMP"," Installing buffer listener");
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){
public void onBufferingUpdate(MediaPlayer mPlayer, int percent) {
Log.d("SMP"," Mediaplayer ready (preparation done). Inside buffer listener");
buffer_state = percent;
if(tv_buffer_message != null){
tv_buffer_message.setText(percent+"%");
}
}
};
Log.d("SMP"," Set on prepared listener");
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mPlayer) {
Log.d("SMP"," Mediaplayer ready (preparation done). Starting reproduction");
mPlayer.start();
Log.d("SMP"," Mediaplayer ready (preparation done). Done!");
}
});
buffer_state = 0;
}
// Remove the checkBufferState method and call the following in its place:
public setBufferMessageView(TextView tv) {
tv_buffer_message = tv;
}
// getInstance, play, and getBufferState can stay the same, except remove the call to release