2013-06-12 92 views
34

我非常習慣於Android編程,我想學習如何使用soundpool方法,我曾經搜索過它,但是我發現的所有內容似乎都不易理解!我希望你給我演示一個運行2種聲音的非常簡單的例子。 /命名爲下yourApp/RES 原料使用soundpool示例播放聲音

回答

48

1,創建1個文件夾

2.Do複製粘貼此文件夾

爲深入瞭解在您的鈴聲檢查這個Tutorial 1Tutorial 2

SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 

/** soundId for Later handling of sound pool **/ 
int soundId = sp.load(context, R.raw.windows_8_notify, 1); // in 2nd param u have to pass your desire ringtone 

sp.play(soundId, 1, 1, 0, 0, 1); 

MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.windows_8_notify); // in 2nd param u have to pass your desire ringtone 
//mPlayer.prepare(); 
mPlayer.start(); 

您還可以設置監聽

sp.setOnLoadCompleteListener(new OnLoadCompleteListener() 
{ 
    @Override 
    public void onLoadComplete(SoundPool soundPool, int sampleId,int status) { 

} 
}); 

對於棒棒糖檢查@ user3833732回答。

+0

非常感謝你..現在 –

+10

的構造函數的Soundpool不推薦使用,但由於SoundPool.Builder需要API等級21,我想我仍然在使用它。 – Noumenon

+0

最後兩行(MediaPlayer的東西)是不必要的。其餘的,這是有幫助的。謝謝:) – voghDev

44

是的。我也經歷過這個。但爲了安全起見,我保存了一段我在網上找到的代碼。 雖然還沒有使用過它,我知道它會派上用場,很快...

1)您需要創建AudioAttributes對象:

AudioAttributes attributes = new AudioAttributes.Builder() 
    .setUsage(AudioAttributes.USAGE_GAME) 
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
    .build(); 

2)創建的Soundpool對象:

SoundPool sounds = new SoundPool.Builder() 
    .setAudioAttributes(attributes) 
    .build(); 

3)如何在所有API級別上使用SoundPool示例:

SoundPool sound; 

protected void createSoundPool() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     createNewSoundPool(); 
    } else { 
     createOldSoundPool(); 
    } 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
protected void createNewSoundPool(){ 
    AudioAttributes attributes = new AudioAttributes.Builder() 
     .setUsage(AudioAttributes.USAGE_GAME) 
     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
     .build(); 
    sounds = new SoundPool.Builder() 
     .setAudioAttributes(attributes) 
     .build(); 
} 

@SuppressWarnings("deprecation") 
protected void createOldSoundPool(){ 
    sounds = new SoundPool(5,AudioManager.STREAM_MUSIC,0); 
} 
+0

注意:如果想要具有相同的行爲,可以在createNewSoundPool的Builder中創建.setMaxStreams(5),以同時播放多個聲音,就像在舊版本中一樣。 5也可以是最終的int或者其他東西。 –

4

我寫了一個可用於加載聲音文件並在需要時播放的SoundPoolManager。你可以看到它here

謝謝。

+1

文章中的代碼無法讀取 –

+0

您的意思是不可讀的? –

+1

我發現SoundPoolManager代碼可讀,但使用int代替SoundPool的好處對我而言並不清楚。我猜這是爲了方便。如果你能更充分地解釋這一點,這將有所幫助。 SoundPoolManager是否解決了SoundPool的缺點,使我的代碼更安全/更便攜/更易於管理/更高效?如果是這樣以及爲什麼?提前致謝。 –

1

這是一個很小的工作soundPool的一個例子,它取自here並稍作修改以匹配後21個API。

需要注意的一件事是maxStreams,它表示允許並行運行多少個數據流,如果它是一個(默認值),它可以從構建器中刪除。

import android.app.Activity; 
import android.content.Context; 
import android.media.AudioManager; 
import android.media.SoundPool; 

public class SoundManager extends Activity 
{ 
    static SoundPool soundPool; 
    static int[] sm; 
    static AudioManager amg; 


    public static void InitSound() { 

    int maxStreams = 1; 
    Context mContext = getApplicationContext(); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     soundPool = new SoundPool.Builder() 
       .setMaxStreams(maxStreams) 
       .build(); 
    } else { 
     soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0); 
    } 

    sm = new int[3]; 
    // fill your sounds 
    sm[0] = soundPool.load(mContext, R.raw.sound_1, 1); 
    sm[1] = soundPool.load(mContext, R.raw.sound_2, 1); 
    sm[2] = soundPool.load(mContext, R.raw.sound_3, 1); 

    amg = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 
    } 

    static void playSound(int sound) { 

     soundPool.play(sm[sound], 1, 1, 1, 0, 1f); 
    } 

    public final void cleanUpIfEnd() { 
    sm = null; 
    soundPool.release(); 
    soundPool = null; 
    } 
} 
+1

不使用'amg' – FracturedRetina

+0

它用於'init'中 – Idan

+0

它已初始化但未使用。 – m0skit0