2012-09-03 52 views
0

我正在做一個報警系統。警報工作正常,但聲音不起作用..我正在做一個小測試,我不明白問題出在哪裏。在警報活動中,我有以下代碼:Alarmmanager&Soundpool - Android

 setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); 
    try { 
     AssetManager assetManager = getAssets(); 
     AssetFileDescriptor descriptor = assetManager.openFd("sound.ogg"); 
     mySoundId = soundPool.load(descriptor, 1); 
    } catch (IOException e) { 
     Log.d(TAG,"We've problem's!!, " 
       + e.getMessage()); 
    } 

    soundPool.play(mySoundId, 1, 1, 0, 0, 1); 

有沒有錯誤..但音樂不起作用..相反,如果我編程播放在底部的工作!任何人都可以幫助我嗎?

+0

'soundPool.play()'返回什麼? – iTurki

+0

@iturki返回零:( –

回答

1

我剛試過你的代碼,發現問題。這是SoundPool中的一個常見問題。在播放之前,您需要等待聲音完成加載。這就是爲什麼soundPool.play()返回零。聲音還沒有準備好。

在API 8,setOnLoadCompleteListener()是這將解決這個問題介紹:

soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
    public void onLoadComplete(SoundPool soundPool, int sampleId,int status) { 
     //play the sound. 
    } 
}); 

但是,如果你的目標下的API,你不能告訴是肯定的,如果你的聲音是準備好進行播放或不。在這種情況下,我建議你使用MediaPlayer

+0

如果我刪除擴展名.ogg我有一個excepcion:'( –

+0

只是可以肯定,你從代碼中刪除它,而不是從實際的文件,對不對?:) – iTurki

+0

@HéctorOrtiz已更新 – iTurki