簡體   English   中英

Android的SoundPool無法播放任何聲音

[英]Android's SoundPool not playing any sound

我已經盡力使這項工作可行。 Android的SoundPool類正在加載.ogg音頻文件(已測試),但無法播放。 由於某些奇怪的原因,我無法設法使它正常工作。 我已經關注了很多有關此問題的教程和論壇,但仍然無法正常工作。

這是我的代碼:

作為全局變量:

 SoundPool soundPool;
 boolean ya = false;

在我的活動的onCreate上:

   this.setVolumeControlStream(AudioManager.STREAM_MUSIC);    
   if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                soundPool = new SoundPool.Builder()
                .setMaxStreams(15)
                .build();
    }else{
        soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
    }

    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool sp, int sampleId, int status) {
            ya = true;
        }
    });     
    soundPool.load(this, R.raw.explosion_1, 1);

最后,在我的按鈕之一的onClick方法上:

public void instructions(View v){
    if(ya){
        soundPool.play(R.raw.explosion_1, 0.99f, 0.99f, 1, 0, 0.99f);
    }
}

請幫忙。

為了播放聲音,您需要將從load()獲得的id傳入play()

int explosion = soundPool.load(this, R.raw.explosion_1, 1);
soundPool.play(explosion, 0.99f, 0.99f, 1, 0, 0.99f);

我已經使用以下代碼播放了SD卡中的聲音,它對我有用。

private SoundPool mSoundPool;
private int mSoundID;

private boolean isLoaded = false;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Start Greeting View Activity...");

setContentView(R.layout.greeting_view_activity);
//mGiGiView = (GreetingWidget) findViewById(R.id.gigi_greet);
//mGiGiView.setOnTouchListener(this);

//Set default animation sound path.
String soundAnimUrl = "/gigi/anim/evening.ogg";

// get the Bundle out of the Intent.
Bundle extras = getIntent().getExtras();
if (extras != null) {

    // check to see if "soundAnimUrl" is in the bundle, if so then
    // assign it's value to animUrl if not, assign null to soundAnimUrl.
    soundAnimUrl = extras.containsKey("soundAnimUrl") ? extras
            .getString("soundAnimUrl") : null;
}

// Set the hardware buttons to control the music.
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

// Load the sound.
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId,
            int status) {
        isLoaded = true;

        // Play the sound when loaded
        play();
    }
});

mSoundID = mSoundPool
        .load(getFile(Environment.DIRECTORY_MUSIC, soundAnimUrl)
                .getPath(), 1);

//Play sound from raw directory
// soundID = soundPool.load(this, R.raw.greeting1, 1);      
}

private void play() {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
        .getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
        .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;

// Is the sound loaded already?
if (isLoaded) {
    mSoundPool.play(mSoundID, volume, volume, 1, 0, 1f);
    Log.d(TAG, "Played sound");
}
}






 @Override
public boolean onTouch(View v, MotionEvent event) {


  if (event.getAction() == MotionEvent.ACTION_DOWN) {


switch (v.getId()) {

        case R.id.gigi_greet:
            play();
            break;

        default:
            break;
        }
    }
    return false;

}   









 /**

 * Get File instance from sd card path.

 * 

 * @param deviceFolderPath

 *            - Pictures, Music, etc

 * @param dbFilePath

 *            - path stored in db (/gigi/anim/morning.ogg)

 * @return

 */

public File getFile(final String deviceFolderPath, final String dbFilePath) {

// Create full path
String picturePath = deviceFolderPath.concat(File.separator).concat(
        dbFilePath);

// Create file
File mFile = getExternalFilesDir(picturePath);

return mFile;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM