viewpager图像从未播放的数组中滑动第一个声音

时间:2014-01-21 10:47:15

标签: android android-viewpager

我做了这个应用程序。这里使用了viewpager进行图像滑动和扫描。播放与图像匹配的声音。现在它工作正常,但只有问题是在第一页图像上它没有播放声音,因为页面在启动时不刷卡。我该如何解决这个问题?这是我的代码

public class Learning_Numbers extends Activity {
    MediaPlayer mp;
    MediaPlayer BG;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);
        ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setOnPageChangeListener(player);
        myPager.setCurrentItem(0);
        BG = MediaPlayer.create(this, R.raw.jingle);
        BG.setLooping(true);
        BG.setVolume(100, 100);
        BG.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        BG.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        BG.start();
    }

    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,R.raw.ten};

    private int imageArra[] = { 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,R.drawable.no10};

 OnPageChangeListener player = new OnPageChangeListener()
    {

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

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

myPager.setCurrentItem(0);中的onCreate()后面的代码下面填写:

mp = MediaPlayer.create(Learning_Numbers.this, mAudio[0]);
mp.start();

希望它能运作.. !!!