2014-08-31 58 views
0

我有簡單的imageview應用程序,可以在後臺播放音樂。但是,我的問題是,當用戶將設備轉向橫向模式時,第一個音樂(比如a)開始再次播放,並且首先播放的音樂也隨之播放。我知道我有一些問題,媒體播放器,但我不知道它的確切位置位於在我的代碼...如何制止這個問題....當用戶在橫向模式下滑動時,Mediaplayer會再次播放音樂

Maainactivity.java

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.Menu; 

import android.view.MenuItem; 
import android.widget.ShareActionProvider; 

public class MainActivity extends Activity { 

    MediaPlayer oursong; 
    ViewPager viewPager; 
    ImageAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     oursong = MediaPlayer.create(MainActivity.this, R.raw.a); 
     oursong.seekTo(0); 
     oursong.start(); 

     viewPager = (ViewPager) findViewById(R.id.view_pager); 
     adapter = new ImageAdapter(this); 
     viewPager.setAdapter(adapter); 


     viewPager.setOnPageChangeListener(MyViewPagerListener); 
    } 



    private int pos = 0; 
    @Override 
    protected void onPause() { 
      super.onPause(); 

      if(oursong != null){ 
       pos = oursong.getCurrentPosition(); 
       oursong.release(); 
       oursong = null; 
      } 
    } 

    @Override 
    protected void onResume(){ 
      super.onResume(); 

     /* 
      * This is the important part, basically since your releasing the song 
      * in onPause() you are getting rid of its reference, in this case check 
      * if your song is null then if it is re-create it, else you can reuse the 
      * the original, but i suspect that calling release() in onPause() allows the 
      * song to get cleaned up by Java's Garbage Collector. 
      */ 
     oursong = MediaPlayer.create(MainActivity.this, R.raw.a); 
     oursong.seekTo(pos); // You will probably want to save an int to restore here 
     oursong.start(); 
    } 
    /* 
    * May want to add two methods here: onSaveInstanceState(Bundle outstate) & 
    * onRestoreInstanceState(Bundle savedInstanceState) to maintain playback position 
    * in onResume instead of just restarting the song. 
    */ 

    private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int pos) { 
       if (pos == adapter.getCount() - 1){ 
       // adding null checks for safety 
       if(oursong != null){ 
        oursong.pause(); 
       } 

       } else if (!oursong.isPlaying()){ 

       // adding null check for safety 
       if(oursong != null){ 
        oursong.start(); 
       } 
       }   
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
      // TODO Auto-generated method stub 

      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
      // TODO Auto-generated method stub 

      } 
     }; 

} 

回答

0

每當你旋轉手機並且從縱向切換到橫向時,活動將被銷燬並重新創建,這意味着再次調用onCreate()。

將此行添加到清單中,告訴android不要在這種情況下銷燬您的活動。

android:configChanges="orientation|screenSize" 

更多信息HERE

+0

@ Manza..thanks它沒有解決我的問題.. – biggboss2019 2014-08-31 18:29:09

相關問題