我正在使用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>
機器人:configChanges =「方向| keyboardHidden |屏幕尺寸|屏幕布置:」要通過這個屬性。 – SathishKumar
@SathishKumar這是快速,骯髒,可能是最糟糕的建議。 –
@MaciejGórski代碼處理onConfigurationChnage,所以只有我給了這個 – SathishKumar