我在广播中对收到短信进行判断,然后根据短信内容想要播放音频文件.
在使用mediaplayer是可以正常播放的,但是使用soundpool就无法播放了,
错误提示,
E/AudioCache: Heap size overflow! req size: 1052672, max size: 1048576
我的音频文件大小是100k,格式是mp3;我的代码是
package com.aa.safe.safe.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.SoundPool; import android.telephony.SmsMessage; import android.util.Log; import com.aa.safe.safe.R; /** * Created by the18 on 2016/11/19 0019. */ public class SmsBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("SmsBroadcast", "onReceive: " + "进入了短信接受"); Object[] pdus = (Object[]) intent.getExtras().get("pdus"); for(Object obj:pdus){ SmsMessage msg = SmsMessage.createFromPdu((byte[]) obj); String msgBody = msg.getMessageBody(); if("#*location*#".equals(msgBody)) { Log.d("SmsBroadcast", "onReceive: location" ); abortBroadcast(); }else if("#*alarm*#".equals(msgBody)) { Log.d("SmsBroadcast", "onReceive: alarm" ); final SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC,0); final int load = sp.load(context, R.raw.faka,1); sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { sp.play(load,1.0f,1.0f,0,1,1.5f); } }); // MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.faka); // mediaPlayer.start(); abortBroadcast(); }else if("#*wipedata*#".equals(msgBody)) { Log.d("SmsBroadcast", "onReceive: wipedata" ); abortBroadcast(); }else if("#*lockscreen*#".equals(msgBody)) { Log.d("SmsBroadcast", "onReceive: lockscreen" ); abortBroadcast(); } } } }
错误已经很明显了,播放的文件大小超过SoundPool所支持的内存限制的大小。SoundPool对内存是有限制的,它只支持小而急促的音频文件。你可以换个更小的音频文件试试。