声音没有播放在viewpager中的图像滑动

时间:2014-01-14 08:26:34

标签: android android-viewpager

我在这里遇到麻烦,因为我在用户执行图像滑动时尝试播放声音:没有播放声音。

OnPageChangeListener附加到ViewPager时,我感到很反感。此外,未使用OnPageListener

与错误相关的代码:

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);
        }
    }
    }

我真的很难受。请告诉我错误的位置。

1 个答案:

答案 0 :(得分:0)

我不确切地知道你的错误是什么,但如果你说它是“通过将OnPageChangeListener附加到我的viewPager”,我会告诉你这样做:

也许尝试将OnPageChangeListener mOnPageListenerImagePagerAdapter中取出,直接放在Activity

或者将OnPageChangeListener设置为适配器:

viewPager.setOnPageChangeListener(adapter);

并制作Adapter工具onPageChangedListener

希望它会有所帮助