Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

android - How to play short sounds while swiping pages ?

I have problem with ViewPager and can't find answer on this site or via a Google search.
How to play short sounds while swiping pages?
How can I change my code, I want to add mediaplayer/Play sound . (The sounds in the raw folder)
Thanks,

@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; 
        } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use SoundPool. It is specifically designed to play short sounds (esp in games, multimedia apps).

here is a tutorial - http://content.gpwiki.org/index.php/Android:Playing_Sound_Effects_With_SoundPool


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...