gpt4 book ai didi

java - Android MediaPlayer错误

转载 作者:行者123 更新时间:2023-12-03 01:48:22 24 4
gpt4 key购买 nike

E/MediaPlayer: EventHandler handleMessage thread id is 1
E/MediaPlayer: EventHandler handleMessage thread id is 1
E/MediaPlayer: currentThread is 1, handleMessage mTimeProvider hashcode is 1112571032, mTimeProvider is android.media.MediaPlayer$TimeProvider@42507c98, msg is { when=-19ms what=7 target=android.media.MediaPlayer$EventHandler }

尝试播放音频时出现上述错误,为什么出现此错误有任何想法,相同的代码在某些地方有效,我正在从片段中调用静态函数?

                        Global.playAudio("sounds/add_comment.mp3",context); 

public static void playAudio(String aud, Context context) {
final MediaPlayer mp;

try {
AssetFileDescriptor fileDescriptor =
context.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

最佳答案

让我知道这是否有帮助-

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener,AudioManager.OnAudioFocusChangeListener{
MediaPlayer mp;
AudioManager mAudioManager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//got audio focus
playAudio("bell.mp3",this);
}

}

private void playAudio(String aud, Context context) {
try {
AssetFileDescriptor fileDescriptor =
context.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepareAsync();
mp.setOnPreparedListener(this);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}

@Override
public void onAudioFocusChange(int i) {

}
}

在这里,我首先尝试获得音频焦点,然后让Mediaplayer异步执行任务。

关于java - Android MediaPlayer错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41806278/

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