The following code shows how to use VideoView to
play a m4v
media file.
Layout file
<?xml version="1.0" encoding="utf-8"?> <!--// w w w . j a v a 2 s . c o m Copyright (c) 2012 Manning See the file license.txt for copying permission. --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/main_portrait_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.25" android:orientation="horizontal" > <ScrollView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="test" /> </ScrollView> <View android:id="@+id/main_portrait_position" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_margin="2dp" android:layout_weight="1" android:background="#ffffff" /> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="2dp" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="#ffffff" /> <ScrollView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_marginTop="2dp" android:layout_weight="0.75" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="text" /> </ScrollView> </LinearLayout> <VideoView android:id="@+id/main_videoview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /> </RelativeLayout>
Java code
/******************************************************************************* * Copyright (c) 2012 Manning/*www . j a va2 s . co m*/ * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack014; import android.app.Activity; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.MediaController; import android.widget.RelativeLayout; import android.widget.VideoView; public class MainActivity extends Activity { private VideoView mVideoView; private View mPortraitPosition; private View mPortraitContent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPortraitPosition = findViewById(R.id.main_portrait_position); mPortraitContent = findViewById(R.id.main_portrait_content); mVideoView = (VideoView) findViewById(R.id.main_videoview); // We use a post to call initVideoView because // we need the width and height of mPortraitPosition mVideoView.post(new Runnable() { @Override public void run() { initVideoView(); } }); } private void initVideoView() { mVideoView.setMediaController(new MediaController(this)); Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bigbuck); mVideoView.setVideoURI(uri); setVideoViewPosition(); mVideoView.start(); } private void setVideoViewPosition() { switch (getResources().getConfiguration().orientation) { case Configuration.ORIENTATION_LANDSCAPE: { mPortraitContent.setVisibility(View.GONE); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); params.addRule(RelativeLayout.CENTER_IN_PARENT); mVideoView.setLayoutParams(params); break; } case Configuration.ORIENTATION_SQUARE: case Configuration.ORIENTATION_UNDEFINED: case Configuration.ORIENTATION_PORTRAIT: default: { mPortraitContent.setVisibility(View.VISIBLE); int[] locationArray = new int[2]; mPortraitPosition.getLocationOnScreen(locationArray); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( mPortraitPosition.getWidth(), mPortraitPosition.getHeight()); params.leftMargin = locationArray[0]; params.topMargin = locationArray[1]; mVideoView.setLayoutParams(params); break; } } } @Override public void onConfigurationChanged(Configuration newConfig) { setVideoViewPosition(); super.onConfigurationChanged(newConfig); } }