我正在使用 Android 中的媒体播放器进行音频演示。当我旋转模拟器的屏幕时,我只需要播放一次歌曲。但是当我实现这个应用程序并旋转屏幕时,按下 Ctrl+F10 或 Ctrl+F11 按钮进行横向和纵向我面临的问题我的歌曲正在播放以再次开始我开始旋转歌曲是一次又一次地开始双重和双重。这是我的代码。
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();
}
}
}
我在 res 目录的 layout-land 文件夹中添加了 audio.xml 文件
<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>