方案
我有一个问题开始让我疯了。在一个活动中,我有以下场景。在创建时: - 创建GPS列表器 - 创建位置监听器 - 启动GPS列表器和位置监听器 - 启动asyncTask,从Web下载地图/或从缓存加载地图 - 播放一个wav文件,这是一个原始资源,非常小(大约63 Kilo)这很短,基本上它说:“让我们前往起点”,它使文本视图显示10秒。
问题
很少声音充分发挥。有时候我只会听到“让我们的头”,有时候“让我们前往明星”,有时根本不玩。这就像阻止声音的其他东西。我在自己的线程中移动了声音,但仍然没有成功:
代码
private void ShowTextPlayVoice(String message, int soundResId)
{
txtProceed.setText(message);
txtProceed.setVisibility(View.VISIBLE);
final int soundId = soundResId;
Handler splashHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
txtProceed.setVisibility(View.INVISIBLE);
break;
}
super.handleMessage(msg);
}
};
Message msg = new Message();
msg.what = 0;
splashHandler.sendMessageDelayed(msg, 10000);
Thread thread = new Thread() {
public void run() {
MediaPlayer mp = MediaPlayer.create(ctx, soundId);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
};
thread.start();
}
谢谢大家的帮助。
答案 0 :(得分:5)
经过一番尝试,这是一个有效的解决方案
private MediaPlayer mediaPlayer = new MediaPlayer();
private void ShowTextPlayVoice(String message, int soundResId)
{
txtProceed.setText(message);
txtProceed.setVisibility(View.VISIBLE);
Handler splashHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
txtProceed.setVisibility(View.INVISIBLE);
break;
}
super.handleMessage(msg);
}
};
Message msg = new Message();
msg.what = 0;
splashHandler.sendMessageDelayed(msg, 10000);
new SoundTask().execute(soundResId);
}
protected class SoundTask extends AsyncTask<Integer, Void, Void>
{
@Override
protected Void doInBackground(Integer... params)
{
AssetFileDescriptor afd = ctx.getResources().openRawResourceFd(params[0]);
try
{
//final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
});
}
});
afd.close();
}
catch (Exception e) {
}
return null;
}
}