2012-09-30 110 views
0

Soundpool停止功能不起作用。我有很多圖像哪些圖像是在視圖中,我需要播放該圖像相關的聲音,我試圖使用soundpool對象音樂播放nice.but我的問題是在滾動時間,如果我迅速移動1圖像2圖像,1 ,播放2張圖像放棄聲音。 但我需要設置時,2圖像是視圖位置時間自動我需要停止1圖像relted聲音,我需要播放只有2圖像relted聲音。Soundpool停止問題?

感謝,

package com.example.pictures; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

public class Pictures extends Activity implements OnPageChangeListener{ 
SoundManager snd; 
int sound1,sound2,sound3; 
View view=null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.picturespage); 
MyPagerAdapter adapter = new MyPagerAdapter(); 
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
myPager.setAdapter(adapter); 
myPager.setCurrentItem(0); 
myPager.setOnPageChangeListener(this); 
snd = new SoundManager(this); 
sound1 = snd.load(R.raw.sound1); 
sound2 = snd.load(R.raw.sound2); 
sound3 = snd.load(R.raw.sound3); 

} 
public void onPageScrollStateChanged(int arg0) { 
    // TODO Auto-generated method stub 

} 

public void onPageScrolled(int arg0, float arg1, int arg2) { 
    // TODO Auto-generated method stub 

} 

public void onPageSelected(int position) { 
    // TODO Auto-generated method stub 

    switch (position) { 
    case 0:     
      snd.play(sound1); 
      lastPlayed = sound1; 
     break; 
    case 1: 
      snd.stop(lastPlayed); 
     snd.play(sound2); 
      lastPlayed = sound2; 
     break; 
    case 2: 
      snd.stop(lastPlayed); 
      snd.play(sound3); 
      lastPlayed = sound3; 
     break; 
    case 3: 
     Toast.makeText(this, "test", Toast.LENGTH_SHORT).show(); 
     break; 
    case 4: 
     Toast.makeText(this, "test2", Toast.LENGTH_SHORT).show(); 
     break; 
    case 5: 
     Toast.makeText(this, "5", Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 
}; 

SoundManager類中的類;

public class SoundManager { 
//.... 
//.... 
//.... 

    public SoundManager(Context appContext) 
    { 
     sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0); 
     pContext = appContext; 
    } 

    // Load up a sound and return the id 
    public int load(int sound_id) 
    { 
     return sndPool.load(pContext, sound_id, 1); 
    } 

    // Play a sound 
    public int play(int sound_id) 
    { 
     return sndPool.play(sound_id, leftVolume, rightVolume, 1, 0,rate);  

    } 

    public void stop(int sound_id) { 
      sndPool.stop(sound_id);   
    } 
} 

回答

2

我不明白多少你在說什麼,只是看着你的代碼不正確地使用停機功能。

stop功能應該接受流ID,不是聲音ID

  • 流id是從play函數返回的值,並標識播放曲目的特定實例。

  • 聲音ID是從load函數返回的值,並標識加載的資源。