android - soundpool 提示堆内存溢出
PHP中文网
PHP中文网 2017-04-17 17:55:12
0
1
806

我在广播中对收到短信进行判断,然后根据短信内容想要播放音频文件.
在使用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();
            }
        }
    }
}
PHP中文网
PHP中文网

认证0级讲师

membalas semua(1)
PHPzhong

Ralat jelas, saiz fail yang dimainkan melebihi had memori yang disokong oleh SoundPool. SoundPoolMemori adalah terhad dan ia hanya menyokong fail audio yang kecil dan mendesak. Anda boleh cuba menukar kepada fail audio yang lebih kecil.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan