如何在刷页时播放短音?

时间:2012-09-06 08:58:54

标签: android android-viewpager android-mediaplayer fragmentpageradapter

我在使用ViewPager时遇到问题,无法在此网站上或通过Google搜索找到答案。
如何在刷页时播放短小的声音?
如何更改我的代码,我想添加媒体播放器/播放声音。 (原始文件夹中的声音)
谢谢,

@Override
public void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
setContentView(R.layout.main);     
MyPagerAdapter adapter = new MyPagerAdapter();   
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);    
myPager.setAdapter(adapter);    
myPager.setCurrentItem(2);
}



private class MyPagerAdapter extends PagerAdapter { 

        public int getCount() { 
                return 5; 
        } 

        public Object instantiateItem(View collection, int position) { 

                LayoutInflater inflater = (LayoutInflater) collection.getContext() 
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

                int resId = 0; 
                switch (position) { 
                case 0: 
                        resId = R.layout.farleft; 
                        break; 
                case 1: 
                        resId = R.layout.left; 
                        break; 
                case 2: 
                        resId = R.layout.middle; 
                        break; 
                case 3: 
                        resId = R.layout.right; 
                        break; 
                case 4: 
                        resId = R.layout.farright; 
                        break; 
                } 

                View view = inflater.inflate(resId, null); 

                ((ViewPager) collection).addView(view, 0); 

                return view; 
        } 
        @Override 
        public void destroyItem(View arg0, int arg1, Object arg2) { 
                ((ViewPager) arg0).removeView((View) arg2); 
         } 

        @Override 
        public boolean isViewFromObject(View arg0, Object arg1) { 
                return arg0 == ((View) arg1); 
         } 

        @Override 
        public Parcelable saveState() { 
                // TODO Auto-generated method stub 
                return null; 
        } 

2 个答案:

答案 0 :(得分:1)

使用SoundPool。它专门用于播放短音(尤其是游戏,多媒体应用)。

这是一个教程 - http://content.gpwiki.org/index.php/Android:Playing_Sound_Effects_With_SoundPool

答案 1 :(得分:1)

一个例子:

private static final int rId = R.raw.sound;
private int sid=-1;
private boolean loaded = false;
SoundPool soundPool;

private SoundPool.OnLoadCompleteListener listener = 
new SoundPool.OnLoadCompleteListener(){
@Override
public void onLoadComplete(SoundPool soundPool, int sid, int status){ // could check status value here also
  if (this.sid == sid) {
    this.loaded = true;
  }
}
};


public void initSound() {
  soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
  soundPool.setOnLoadCompleteListener(listener);
  this.sid = soundPool.load(getApplicationContext(), rId, 1); 
}

public void SoundPlay() {
  new Thread(new Runnable() {
    @Override
    public void run() {
      if (loaded) {
      soundPool.play(this.sid, 1.0, 1.0, 1, 0, 1f);     
        }
  }).start();
}

所以你要添加一个像:

这样的构造函数
MyPagerAdapter() {
  initSound();
}

并使用SoundPlay();

在instantiateItem()中每次播放

我没有按原样测试上面的代码,但在我自己的东西中使用类似的东西。