问题
I am trying to play a sound when a user clicks on a button, This works well if I click the button wothin the first 3-5 seconds of when the activity is open however if I wait more then 3-5 seconds there is no sound. I am also not getting any error regarding the sound....
Any ideas would be appriciated!
UPDATE: This is happening only on my HTC. If I try this on a Samsung Galaxy S 2 it works fine!!!!
In my logcat is says: "AudioHardwareQSD: AudioHardware pcm playback is going to standby" any workaround for this???
@Override
protected void onCreate(Bundle savedInstanceState)
{
mSoundPoolMap = new HashMap<Integer, Integer>();
mSoundPool = new SoundPool(16, AudioManager.STREAM_RING, 0);
mAudioManager= (AudioManager) mcontext.getSystemService(Context.AUDIO_SERVICE);
mSoundPoolMap.put(1, mSoundPool.load(mcontext, R.raw.tap, 1));}
@Override
public void onClick(View v)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 0, 1);}
回答1:
Here is how I got this to work not the best but it works...
Checked to see if the device is from HTC and then created a dummy notification that plays a sound. Also you have to make sure that your file is MP3 not wav or OGG...
public static void playSound(int index)
{
String m_strManufacture = Build.MANUFACTURER;
Log.i(TAG, "Device Name: " + m_strManufacture);
if (m_strManufacture.equals("HTC"))
{
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
Intent notificationIntent = new Intent(mContext, mContext.getClass());
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
// mContext.getResources().getResourceName(R.raw.ringer);
notification.sound = Uri.parse("android.resource://com.yourpackages/raw/" + sound);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
else {//Do the soundpool work....}
来源:https://stackoverflow.com/questions/12261867/soundpool-not-playing-after-3-second-of-opening-my-activity