gpt4 book ai didi

android - 在ViewPager和慢速应用程序中滑动后声音不会停止

转载 作者:行者123 更新时间:2023-12-03 02:12:45 25 4
gpt4 key购买 nike

我仍然是开发应用程序的初学者。我试图在我的viewPager中滑动,以显示图片和按钮。按下按钮时,将根据图片播放声音(我正在使用情况)。它起作用,当我滑动时,前一个声音停止,但是当我再次按下该按钮时,后一个声音不播放。我认为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 );

}
}

最佳答案

你可以改变:

@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或switch暂停并查找到MediaPlayer数据的开头。

关于android - 在ViewPager和慢速应用程序中滑动后声音不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22690304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com