本文介绍了滑动页面时如何播放短音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 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
这篇关于滑动页面时如何播放短音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!