2012-09-04 63 views
1

我嘗試在用戶單擊按鈕時播放聲音,如果在活動打開時的第一個3-5秒內點擊按鈕,這很有效。如果我等待3-5秒以上,則沒有聲音。我也沒有得到任何錯誤的聲音....SoundPool在打開我的活動3秒後沒有播放

任何想法appriciated!

更新:這隻發生在我的HTC。如果我在三星Galaxy S 2上試用它,它可以正常工作!

在我的logcat中說:「AudioHardwareQSD:AudioHardware pcm播放將要待機」任何解決方法?

@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);} 
+0

在這兩種情況下'mSoundPool.play()'返回的int值是什麼? – iTurki

+0

我在3秒內獲得了1的聲音值。之後的值爲2。沒有得到值爲0的... – user1163234

回答

0

這是我如何得到這個不行最好的,但它的工作原理...

檢查,看看是否該設備是由HTC,然後創建播放聲音啞通知。您還必須確保您的文件是MP3而不是wav或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....}