gpt4 book ai didi

java - 画廊与声音滑动

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

我一直在寻找带有声音的图库代码(滑动),这是我最近做的。当我打开 Activity 时,听起来会播放第一张照片,但是当我滑动它以查看下一张照片时,它不会。

    public class alphagal extends Activity {

MediaPlayer mp;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.numgal);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);

}


private class ImagePagerAdapter extends PagerAdapter {

private int[] mImages = new int[] {R.drawable.a,R.drawable.bb};
private int[] mAudio = new int[]{R.raw.capture,R.raw.capture2};

@Override
public int getCount() {
return mImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = alphagal.this;
ImageView imageView = new ImageView(context);

imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
if(position>=1){
mp = MediaPlayer.create(alphagal.this, mAudio[position-1]);
mp.start();
}
return imageView;
}
OnPageChangeListener player = new OnPageChangeListener()
{

@Override
public void onPageSelected(int arg0)
{
mp = MediaPlayer.create(alphagal.this, mAudio[arg0]);
mp.start();
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}

@Override
public void onPageScrollStateChanged(int arg0)
{
}
};

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}

最佳答案

不需要OnPageChangeListener,因为在每次轻扫时都会调用InstantiateItem()。我稍微修改了代码,可以正常工作。请检查。还有一件事,您必须覆盖onbackpress()以停止媒体播放器,否则它将继续播放如果退出应用程序。

    public class alphagal extends Activity {

MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.numgal);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);

}

private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.a,R.drawable.bb};
private int[] mAudio = new int[]{R.raw.capture,R.raw.capture2};

@Override
public int getCount() {
return mImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = alphagal.this;
ImageView imageView = new ImageView(context);

imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
if (position >= 1) {
//if you have already the instance of media player
if (mp != null) {
if (mp.isPlaying())
mp.stop();
mp.reset();

try {
AssetFileDescriptor afd = getResources()
.openRawResourceFd(mAudio[position - 1]);
if (afd == null)
return null;
mp.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
} catch (IOException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
}
} else {
// player is null
// it will create new MediaPlayer instance, setDataSource
// and call prepare
mp = MediaPlayer
.create(alphagal.this, mAudio[position - 1]);
}
mp.start();
}
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}

@Override
public void onBackPressed() {
//need to handle otherwise if you exit from the app media
//player continue to play
if (null != mp) {
mp.stop();
mp.release();
}

super.onBackPressed();
}
}

关于java - 画廊与声音滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099726/

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