我创建了一个实时接收消息的活动。有时消息只是一个通知,有时消息会导致弹出是/否警报对话框。我想为此创建一个 10 秒的声音通知,所以我在课堂上声明。我将 mediaplayer 设为全局,以免每次需要播放声音时都创建它。
final MediaPlayer mediaPlayer = new MediaPlayer();
final Handler handler = new Handler();
我在活动 oncreate 中设置数据源
try {
mediaPlayer.setDataSource(getBaseContext(), defaultRingtoneUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
然后当我收到我在onnewintent中做的消息时
try {
mediaPlayer.prepare();
mediaPlayer.start();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
}, 10000);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
最后在单击警报对话框时停止警报声音我已经完成
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
response = "1";
Log.v("Yes button clicked", response);
//some code
if(mediaPlayer.isPlaying())
mediaPlayer.stop();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
response = "-1";
Log.v("No button clicked", response);
//some code
if(mediaPlayer.isPlaying())
mediaPlayer.stop();
break;
}
}
};
我一直在测试这个。在 xperia p 上始终具有预期的行为。完全没有问题。今天我得到了一个新的熔岩虹膜(它有一个更小的 ROM 512M)。在第一条消息上它会播放声音,但之后我在准备时收到 IOexception 错误status=0xFFFFFF8E
关于我在这里做错了什么的任何想法?如果为此目的有更清洁的方法,我将愿意更改实施。
01-01 09:53:54.813: D/YourActivity(15988): onNewIntent is called!
01-01 09:53:54.816: W/System.err(15988): java.io.IOException: Prepare failed.: status=0xFFFFFF8E
01-01 09:53:54.818: W/System.err(15988): at android.media.MediaPlayer.prepare(Native Method)
01-01 09:53:54.822: W/System.err(15988): at com.example.delmole.OrderActivity.onNewIntent(OrderActivity.java:309)
01-01 09:53:54.822: W/System.err(15988): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1156)
01-01 09:53:54.823: W/System.err(15988): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2464)
01-01 09:53:54.823: W/System.err(15988): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2477)
01-01 09:53:54.823: W/System.err(15988): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2486)
01-01 09:53:54.823: W/System.err(15988): at android.app.ActivityThread.access$1400(ActivityThread.java:162)
01-01 09:53:54.824: W/System.err(15988): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1435)
01-01 09:53:54.824: W/System.err(15988): at android.os.Handler.dispatchMessage(Handler.java:107)
01-01 09:53:54.824: W/System.err(15988): at android.os.Looper.loop(Looper.java:194)
01-01 09:53:54.824: W/System.err(15988): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-01 09:53:54.825: W/System.err(15988): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 09:53:54.825: W/System.err(15988): at java.lang.reflect.Method.invoke(Method.java:525)
01-01 09:53:54.826: W/System.err(15988): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-01 09:53:54.826: W/System.err(15988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-01 09:53:54.827: W/System.err(15988): at dalvik.system.NativeStart.main(Native Method)