问题描述
我在这里遇到了麻烦,因为我想在用户进行图像刷卡播放声音:没有声音正在播放。
I am having trouble here as I am trying to play a sound while the user performs an image swipe: no sound is being played.
我附上当 OnPageChangeListener
我的 ViewPager
越来越anerror。此外, OnPageListener
不被使用。
I am getting anerror when attaching OnPageChangeListener
to my ViewPager
. Also, OnPageListener
is not used.
code相关的错误:
Code related to the error:
package com.android.viewpager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static MediaPlayer mp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(mOnPageListener);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,};
private int[] mAudio= new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.eight,R.raw.nine};
@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 = MainActivity.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
public OnPageChangeListener mOnPageListener = new OnPageChangeListener() {
@Override public void onPageSelected(int position) {
//for Sound
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(MainActivity.this, mAudio[position]);
mp.start();
}
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override public void onPageScrollStateChanged(int state) {}
};
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
我真的陷入严重这里。请告诉我哪里出错。
I am really stuck badly here. Please tell me where the error is.
推荐答案
我不知道你的错误到底是什么,但如果你说这是通过附加OnPageChangeListener我viewPager,我会告诉你这样做
I don't know exactly what your error is, but if you say that it's "by attaching OnPageChangeListener to my viewPager", I will tell you to do that :
也许尝试让你的 OnPageChangeListener mOnPageListener
出你的 ImagePagerAdapter
,把它直接在你的活动
。
Maybe try to get your OnPageChangeListener mOnPageListener
out of your ImagePagerAdapter
, put it directly in your Activity
.
或者设置 OnPageChangeListener
适配器:
viewPager.setOnPageChangeListener(adapter);
和让你的适配器
实施 onPageChangedListener
。
希望这将有助于
这篇关于声音不玩veiwpager图像刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!