无法在横向和纵向之间切换方向

时间:2013-07-08 07:11:36

标签: android media-player surfaceview screen-orientation

我正在使用SurfaceViewMediaPlayer。当我将方向从纵向更改为横向时,它不会改变。我有两个文件夹layout-port and layout-land。这是代码

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"  android:configChanges="orientation|screenSize">
        <activity
            android:name="com.example.vividplayer.MainActivity"
            android:label="@string/app_name"  android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
layout-land中的

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surfaceview"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <Button android:id="@+id/mBtnmore" android:layout_width="wrap_content"
        android:layout_height="35dip" android:text="More"
        android:layout_marginRight="5dip" android:layout_alignParentRight="true"
        android:layout_marginTop="8dip" />
    <Button android:id="@+id/mBtnhints" android:layout_width="wrap_content"
        android:layout_marginBottom="4dip" android:layout_height="35dip"
        android:text="Hints" android:layout_marginRight="5dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>
布局端口

中的

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surfaceview"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />

</RelativeLayout>

以下是MainActivity.class

public class MainActivity extends Activity implements Callback {
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            getWindow().setFormat(PixelFormat.UNKNOWN); 
            surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setFixedSize(176, 144);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            mediaPlayer = new MediaPlayer();

            String stringPath = "http://mspss.com/vivid/videos/oceans-clip.mp4";

            if (mediaPlayer.isPlaying()) {
                mediaPlayer.reset();
            }

            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


            try {
                mediaPlayer.setDataSource(getApplicationContext(),Uri.parse(stringPath));
                mediaPlayer.prepare();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaPlayer.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
         mediaPlayer.setDisplay(surfaceHolder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

}

3 个答案:

答案 0 :(得分:1)

最适合您的方案是从您的清单中删除android:configChanges="orientation",因为您只想在定位期间更改布局。

如果您使用android:configChanges="orientation",则必须在以下活动中实施:

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // Handle Lanscape codes here
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        // Handle portrait codes here
    }
}

答案 1 :(得分:0)

不改变的原因是因为你要求它不要改变。

因此,请删除

android:configChanges="orientation"

或接受!

答案 2 :(得分:0)

尝试将此添加到您的清单文件

android:screenOrientation="fullSensor"