Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...