声音播放在viewpager上

时间:2014-01-21 05:19:25

标签: android audio 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;
    }

}

这里mp用于图像滑动声音& BG用于背景声音。

2 个答案:

答案 0 :(得分:0)

查看viewpager的源代码。

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity)     {
...
if (mFirstLayout) {
    // We don't have any idea how big we are yet and shouldn't have any pages either.
    // Just set things up and let the pending layout handle things.
    mCurItem = item;
    if (dispatchSelected && mOnPageChangeListener != null) {
         mOnPageChangeListener.onPageSelected(item);
    }
    if (dispatchSelected && mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageSelected(item);
    }
    requestLayout();
...

// in setAdapter(), It will call setCurrentItemInternal() ..

所以,只需setOnPageChangeListener(播放器);在setAdapter之前。它可能会按预期工作。方便的技巧

答案 1 :(得分:0)

我试过创造这个&它对我有用..

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.setOnPageChangeListener(player);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
        BG = MediaPlayer.create(this, R.raw.jingle);
        BG.setLooping(true);
        BG.setVolume(100, 100);
        BG.start();
        mp = MediaPlayer.create(Numbers.this, mAudio[0]);
        mp.start();
    }

再次添加最后两行代码onSelect ..现在它对我来说很好..