Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Programming - How to play a SoundPool sound from a broadcastreceiver?
    primarykey
    data
    text
    <p>I'm very new to Android development and would like to know how to play a SoundPool sound within a broadcastreceiver?</p> <p>I read somewhere that using SoundPool is the way go play the sound but I don't know how to set it up properly.</p> <p>I have some sound files such as wave and mp3 files in my Eclipse res\raw folder. I would like to play a file called half.wav</p> <p>Can you show example code I need to place into my broadcastreceiver?</p> <p>Here is a first attempt at the code but I do get an error stating that soundID = soundPool.load(this, R.raw.half, 1); </p> <p>"The Method Load(Context, Int, Int) In The Type SoundPool Is Not Applicable..."</p> <p>Here is the code for the class:</p> <pre><code>package ChimeMe.BigBen; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; import android.os.Bundle; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; public class AlarmReceiver extends BroadcastReceiver { private SoundPool soundPool; private int soundID; boolean loaded = false; @Override public void onReceive(Context context, Intent intent) { try { // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.half, 1); Toast.makeText(context, "This is the alarm.", Toast.LENGTH_SHORT) .show(); } catch (Exception e) { Toast.makeText( context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } } </code></pre> <p>Thanks in advance.</p> <p>Truly, Emad </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload