2013-07-20 36 views
1

我正在使用Android中的媒體播放器進行音頻演示。當我旋轉模擬器的屏幕時,我只需要播放一次該歌曲。但是當我執行此應用程序時,按住Ctrl + F10或Ctrl + F11的按鈕進行風景和人像的旋轉,我面臨的問題是我的歌曲再次開始雙重播放我開始旋轉歌曲開始雙倍和雙倍。這是我的代碼。如何在Android中旋轉屏幕時僅播放音頻音樂一次?

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.audio); 
     init(); 
     imgVw.setImageResource(R.raw.teddy_two); 

     prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     final SharedPreferences.Editor prefsEdit = prefs.edit(); 

     mp = MediaPlayer.create(Audio_Activity.this,R.raw.issaq_tera_by_vishu); 
     mp.setLooping(false); 
     btnChapter.setEnabled(false); 
     prefsEdit.putBoolean("mediaplaying", true); 
     prefsEdit.commit(); 
     mp.start(); 

     System.out.println("Media Plyer Is Start !!!"); 




     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       // TODO Auto-generated method stub 

       System.out.println("Media Plyer Is Complete !!!"); 

       /*final SharedPreferences.Editor prefsEdit = prefs.edit(); 
       prefsEdit.putBoolean("mediaplaying", false); 
       prefsEdit.commit();*/ 
       btnChapter.setEnabled(true); 
       System.out.println("Music is over and Button is enable !!!!!!"); 
       //mp.start(); 
      } 
     }); 


     @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
      Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

我有加布局,土地文件夾中的文件audio.xml在res目錄

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/display_Images" 
     android:layout_width="fill_parent" 
     android:layout_height="260dp" 
     android:background="@android:color/black" /> 

    <TableLayout 
     android:id="@+id/table_Audio" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="300dp" > 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/btnPause_Resume" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/audio" /> 

      <Button 
       android:id="@+id/btnChapter" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/chapter" > 
      </Button> 
     </TableRow> 


    </TableLayout> 

</RelativeLayout> 

這裏是我的AndroidMeniFest.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.audio_demo" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.audio_demo.Audio_Activity" 

      android:label="@string/app_name" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

機器人:configChanges =「方向| keyboardHidden |屏幕尺寸|屏幕布置:」要通過這個屬性。 – SathishKumar

+0

@SathishKumar這是快速,骯髒,可能是最糟糕的建議。 –

+0

@MaciejGórski代碼處理onConfigurationChnage,所以只有我給了這個 – SathishKumar

回答

0

管理MediaPlayerActivity上下文不是一個好主意。每次配置更改時都會調用onCreate。它也在那裏泄漏內存。

對於快速,骯髒但不是最差的解決方案,請創建Application子類,將該代碼移動到那裏的函數並僅調用該函數一次,例如,從Application.onCreate

如果你想從Activity控制啓動,使用方法:

MyApp app = (MyApp) getApplication(); 
app.methodStartingPlayback(); 

,並確保你守衛裏面MyApp電話:

private boolean playing = false; 

public void methodStartingPlayback() { 
    if (playing) { 
     return; 
    } 
    playing = true; 
    // ... 
} 

爲了更好的解決方案,使用Service控制回放。

+0

我不明白如何在我的應用程序中使用此代碼我沒有一個想法,因爲我是Android的初學者。可以舉個例子。 – tazeenmulani

0

我相信MaciejGórski說的是添加一個標誌來檢查音樂是否在播放之前播放。所以你有一個名爲「玩」的變量並將其實例化爲false。然後在打開聲音的地方檢查標誌是否爲假,如果是,請打開音樂並打開標誌。拿音樂控制位,並把一個方法,如下(使用MacieGorski的代碼爲基礎)

private boolean playing = false; 

public void methodStartingPlayback() 
{ 
    if (!playing) 
    { 
    mp = MediaPlayer.create(Audio_Activity.this,R.raw.issaq_tera_by_vishu); 
    mp.setLooping(false); 
    btnChapter.setEnabled(false); 
    prefsEdit.putBoolean("mediaplaying", true); 
    prefsEdit.commit(); 
    playing = true; 
    mp.start(); 
    } 
    //playing = true; 
    // ... 
} 

在備選方案中,也或許此外,你應該有的onDestroy方法殺死音樂,如下(歸功於拉爾斯Vorgel的教程,鏈接,我現在找不到。)

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    // TODO Auto-generated method stub 
    if (mp != null) { 
     mp.release(); 
     mp = null; 
    } 

} 

希望這有助於