3

我对 ViewPager 有疑问,无法在此站点或通过 Google 搜索找到答案。
滑动页面时如何播放短音?
如何更改我的代码,我想添加 mediaplayer/Play sound 。(原始文件夹中的声音)
谢谢,

@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; 
        } 
4

2 回答 2

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() 中每次播放;

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

于 2012-09-06T10:33:29.373 回答
1

使用声音池。它专为播放短声音而设计(尤其是在游戏、多媒体应用程序中)。

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

于 2012-09-06T10:13:26.747 回答