4

我正在运行一个应用程序,在该应用程序中单击图像时正在运行音频,而正在播放音频并且我将模式从横向更改为纵向或从纵向更改为横向并单击图像,它会播放另一个音频剪辑。

例子:

现在正在播放 2 个音频。但在相同模式下单击图像不会出现此问题。

当模式改变时,有什么方法可以停止以前的活动?

4

3 回答 3

2

要播放音频文件,只需使用Service不要在 Activity 内播放,这样您就可以停止 Service 以便停止歌曲。然后你应该专注于你将如何处理你的方向

  1. 再创活动
  2. 处理方向改变自己。

我最好的建议是你应该自己处理。为此你应该使用两件事:

  1. 添加
    <activity android:name=".CurrentActivity android:configChanges="keyboardHidden|orientation"> </activity>

2.呼叫

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
  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();
     }
}

如果方向改变,您应该根据需要处理服务。

于 2012-06-06T10:57:02.840 回答
1

我已经尝试过@venky 的回答,但我的布局被打乱了:( 在此处输入图像描述

在此处输入图像描述

对于肖像,当方向改变时,按钮也不起作用:(

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_prt"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="vertical" >

</LinearLayout>







<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="28dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pause1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/pause" />




    <ImageView
        android:id="@+id/play1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:src="@drawable/play" />







    <ImageView
        android:id="@+id/home1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="165dp"
        android:src="@drawable/home" />

</LinearLayout>




<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="315dp"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/kalma1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/kalma1" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >



    <ImageView
        android:id="@+id/next1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/forward" />

</LinearLayout>

是我的肖像和xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_lndscp"
android:orientation="vertical" >

<ImageView
    android:id="@+id/kalma1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="33dp"
    android:src="@drawable/kalma1" />


<ImageView
    android:id="@+id/next1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="10dp"
    android:src="@drawable/forward" />



<ImageView
    android:id="@+id/play1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_marginLeft="65dp"
    android:layout_marginTop="41dp"
    android:src="@drawable/play" />




<ImageView
    android:id="@+id/pause1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="41dp"
    android:src="@drawable/pause" />



<ImageView
    android:id="@+id/home1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_alignBottom="@+id/play1"
    android:layout_alignRight="@+id/kalma1"
    android:src="@drawable/home" />

</RelativeLayout>

景观

于 2012-06-07T07:38:57.457 回答
1

在AndroidManifest中添加此代码与您面临问题的当前活动,但这将在您更改方向时停止您的应用程序可绘制对象更改,它将使用当前方向作为两种模式的静态,但仍然尝试让我知道。

<activity android:name=".CurrentActivity
 android:configChanges="keyboardHidden|orientation">
        </activity>
于 2012-06-06T08:39:23.287 回答