我有一个像这样的方法来检查API的通知。 在这种方法中,我试图播放声音(小尺寸声音),通过显示通知:
public void initNotification(Context cntx, final Typeface typeface){
if(User_Session.isLoggedIn() != true){
mNotifHolder.setVisibility(View.INVISIBLE);
}else{
mNotifHolder.setVisibility(View.VISIBLE);
mNotifHolder.setOnClickListener(openNotifActivity);
HashMap<String, String> user = User_Session.GetUserDetails();
String strToken = user.get(SessionManagement.KEY_USERTOKEN);
String URL = Constants.NotifiCount_URL+strToken;
if (NotificationCount != null && !NotificationCount.isDone() && !NotificationCount.isCancelled()){ return; }
NotificationCount = Ion.with(cntx).load(URL).asJsonObject().setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
System.out.println("RESULT OF NOTIFICATION COUNT: "+result);
if(e == null){
if(result != null){
String count = result.get("count").getAsString();
if(count.equals("0")){
mNotifHolder.setVisibility(View.VISIBLE);
notifBack.setBackgroundResource(R.drawable.ic_notif_off);
notifText.setVisibility(View.INVISIBLE);
}else{
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
}
int Count = Integer.parseInt(count);
if(Count > 10){
count = "+10";
}
notifBack.setBackgroundResource(R.drawable.ic_notif_on);
notifText.setVisibility(View.VISIBLE);
notifText.setTypeface(typeface);
notifText.setText(count);
mNotifHolder.setVisibility(View.VISIBLE);
ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, .5f, ScaleAnimation.RELATIVE_TO_SELF, .5f);
scale.setDuration(300);
scale.setInterpolator(new OvershootInterpolator());
mNotifHolder.startAnimation(scale);
}
}else{
Crouton.makeText(MainActivity.this, "ERROR", Style.ALERT).show();
}
}else{
Crouton.makeText(MainActivity.this, "ERROR", Style.ALERT).show();
}
}
});
}
}
但有时app崩溃了,我在某些设备上遇到了这个错误:
java.util.concurrent.TimeoutException: android.media.SoundPool$SoundPoolImpl.finalize() timed out after 10 seconds
at android.media.SoundPool$SoundPoolImpl.release(Native Method)
at android.media.SoundPool$SoundPoolImpl.finalize(SoundPool.java:616)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
at java.lang.Thread.run(Thread.java:841)
我该如何解决?