gpt4 book ai didi

AndroidId MediaPlayer 报错(1,-4) prepareAsync 方法

转载 作者:太空狗 更新时间:2023-10-29 14:16:15 26 4
gpt4 key购买 nike

我在 prepareAsync 方法中遇到错误 (1,-4)。文档指定这是“文件格式不正确”(对吗?),但文件是 mp3 格式。此外,当在另一个 Activity 中调用媒体播放器时,相同的文件会在旧版本的软件中重现。我不知道如何解决这个问题,所以我在这里。

应用程序代码如下所示:

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)

编辑:Dave 回答后的新日志

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)

感谢帮助

最佳答案

一些事情。

1) 如果您希望 MediaPlayer 之后仍处于可用状态,则不应调用 release()。 -4 错误(如果我没记错的话)是无效的状态转换,尝试使用已发布的 MediaPlayer 应该会产生该错误。

2) 您可以在创建MediaPlayer 时设置一次onPreparedonErroronBufferingUpdate 监听器.您现在所做的事情看起来很尴尬且没有必要。

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

关于AndroidId MediaPlayer 报错(1,-4) prepareAsync 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com