我仍然是开发应用程序的初学者。我试图刷过我的viewPager,它显示一张图片和一个按钮。按下按钮时,将根据照片播放声音(I' m使用情况)。它可以工作,当我滑动时,前一个声音停止,但是当我再次按下按钮时,以下声音不会播放。我认为在OnPageChangeListener中存在一些问题。其次,我的应用程序最初没有声音工作正常,但在viewPager中添加声音后,整个系统似乎很慢,它经常停电和挂起。请帮我解决这个问题。十分感谢大家。
int position;
OnPageChangeListener player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
//setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);
final ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setOnPageChangeListener(player);
//
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.car_alarm);
final MediaPlayer cpButtonClick = MediaPlayer.create(this, R.raw.car_ignition);
final MediaPlayer fpButtonClick = MediaPlayer.create(this, R.raw.helicopter);
final MediaPlayer ffButtonClick = MediaPlayer.create(this, R.raw.motorcycle);
//
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapterchap1 imageAdapter = new ImageAdapterchap1(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
images.add(imageView);
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
Button info=(Button) findViewById(R.id.audio);
info.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (viewpager.getCurrentItem()) {
case 0:
mpButtonClick.start();
// do something
break;
case 1:
cpButtonClick.start();
// do something
break;
case 2:
fpButtonClick.start();
// do something
break;
case 3:
ffButtonClick.start();
// do something
break;
case 4:
// do something
break;
}
}
});
}
OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg1)
{
ffButtonClick.stop();
mpButtonClick.stop();
cpButtonClick.stop();
fpButtonClick.stop();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
viewpager.setOnPageChangeListener(player);
}
public void destroyItem( View pager, int position, Object view )
{
((ViewPager)pager).removeView( (View)view );
} }
答案 0 :(得分:0)
您可以更改:
@Override
public void onPageSelected(int arg1)
{
ffButtonClick.stop();
mpButtonClick.stop();
cpButtonClick.stop();
fpButtonClick.stop();
}
要:
@Override
public void onPageSelected(int arg1)
{
ffButtonClick.pause();
ffButtonClick.seekTo(0);
mpButtonClick.pause();
mpButtonClick.seekTo(0);
cpButtonClick.pause();
cpButtonClick.seekTo(0);
fpButtonClick.pause();
fpButtonClick.seekTo(0);
}
由于停止MediaPlayer也会卸载音频,您应该再次使用.prepare()
。此外,更好的解决方案是使用lastPage
实例变量来保存ViewPager的最后一个选定页面,并使用if或a开关来暂停并寻找MediaPlayer数据的开头。