2014-01-21 303 views
0

我已經使這個程序。這裏使用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的背景音。

回答

0

我試過的onCreate這個&工作對我來說..

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 ..現在它是爲我工作很好..

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(player);在setAdapter之前。它可能按預期工作。方便的技巧

+0

它沒有奏效。 ..我做了這個.. myPager.setOnPageChangeListener(player); \t \t myPager.setAdapter(adapter); –

+0

@SuRajPrince這是一個查看「錯誤」(或功能)。請參閱code.google.com/p/android/issues/detail?id=27526。以編程方式調用onpageChangedListener.onPageSelected(0)。 – jiashie

+0

謝謝你的迴應我已經做到了..你能告訴我,如果我在屏幕上有多個圖像查看器,但只有一個在屏幕上顯示,它可以通過按鈕進行更改。可能嗎?? –