panthro
panthro

Reputation: 24061

SoundPool Loading from URL

I load my mp3 via a path into sound pool. For reasons I don't want to go into I dont want to load via R.raw...

But I get the error:

play(int, float, float, int, int, float) in the type SoundPool is not  applicable for the arguments (Object, float, float, int, int, float)

Is there a way to convert the object to an int?

SoundPool mySoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
HashMap mySoundPoolMap = new HashMap<Integer, Integer>(); 
mySoundPoolMap.put(0,  mySoundPool.load("android.resource://com.App.Notifications/raw/myMP3",1));

float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

mySoundPool.play(mySoundPoolMap.get(0), streamVolume, streamVolume, 1, 0, 1f);

Upvotes: 1

Views: 3637

Answers (1)

slayton
slayton

Reputation: 20319

Looking at the SoundPool docs there are a couple of ways to add something to a sound pool:

  1. load(AssetFileDescriptor afd, int priority)
  2. load(Context context, int resId, int priority)
  3. load(String path, int priority)
  4. load(FileDescriptor fd, long offset, long length, int priority)

Because you don't want to load a bundled asset you can't use the either of the first two options. That means you have to use option 3 or 4.

The best option is to probably download the sound file yourself from the url and then save the file and then add the sound to the SoundPool using a FileDescriptor that points to that file or using the String that contains the path of the file.

Update: When you add an item to the SoundPool using load an int is returned. This int is a handle to the sound. Then to play the sound you must use that int to tell the SoundPool which sound you want to play:

For example:

sp = new SoundPool();
int handle1 = sp.load(/* add sound 1*/);
int handle2 = sp.load(/* add sound 1*/);

sp.play(handle2, /*other args*/); // play sound 2

Upvotes: 1

Related Questions