Viewpager声音与图像不同步

时间:2014-01-14 04:35:50

标签: android android-viewpager

所以基本上我想在用户滑动图像时播放短音。有许多图像&我听到这个声音。声音很好但我的问题是在这里我的声音与图像不匹配。可能我想我还没有在我的代码中实现OnPageChangeListener。任何人都可以帮我这个如何实现这个代码..

package com.android.learning_numbers;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Numbers extends Activity {

     public class serv extends Service{

        MediaPlayer mp;
        public IBinder onBind1(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        public void onCreate()
        {   
            mp = MediaPlayer.create(this, R.raw.jingle);
            mp.setLooping(true);
        }
        public void onDestroy()
        {       
            mp.stop();
        }
        public void onStart(Intent intent,int startid){

            String tag = null;
            Log.d(tag, "On start");
            mp.start();
        }
        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
         }
         }

    public static MediaPlayer mp = null;

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

     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.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 = Numbers.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); 

//for Sound 
if (mp != null) {
             mp.reset();
             mp.release();
                }

//the audio files are running here but not in sync with image
        mp = MediaPlayer.create(Numbers.this,mAudio[position]);
        mp.start();

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

这里我如何使用OnPageChangeListener ??

1 个答案:

答案 0 :(得分:0)

您的问题是,无论何时创建项目,您都在播放声音,这可能不一定与更改项目时相同。您可以使用OnPageChangeListener在类中定义它,如下所示:

public OnPageChangeListener mOnPageListener = new OnPageChangeListener() {

    @Override public void onPageSelected(int position) {
        //for Sound 
        if (mp != null) {
            mp.reset();
            mp.release();
        }

        //the audio files are running here but not in sync with image
        mp = MediaPlayer.create(Numbers.this, mAudio[position]);
        mp.start();

    }
    @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    @Override public void onPageScrollStateChanged(int state) {}

};

然后你只需将它附加到你的ViewPager:

viewPager.setOnPageChangeListener(mOnPageListener);

我还建议您不要每次都重新创建MediaPlayer,而是为每个页面缓存它。