我一直在搜索带声音的图库代码(Swipe),这是我做的最后一次。当我打开活动时,第一张照片播放时会发出声音,但是当我将其滑动到下一张照片时,它就不会出现。
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);
}
}
}
答案 0 :(得分:0)
不需要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();
}
}