滑动页面时如何播放短音?

How to play short sounds while swiping pages ?(滑动页面时如何播放短音?)
本文介绍了滑动页面时如何播放短音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

使用 SoundPool.它专为播放短音而设计(尤其是在游戏、多媒体应用中).

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

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

这篇关于滑动页面时如何播放短音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)