<LinearLayout 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:orientation="vertical"
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"
>
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
In the MainAcitivity class, in the onCreate method, you write code to create a reference to SurfaceView, get the surface holder, add Callback interface to the holder, and create MediaPlayer instance. In the onSurfaceCreate method (you need to implement SurfaceHolder.Callback to use this method). You need to set the surface holder to display the video, set the data source of the MediaPlayer to the 3gp video file to be played, call the prepare() method to prepare the MediaPlayer, and start playing the video by calling the start() method.
package com.example.andtip;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity {
MediaPlayer mPlayer;
private SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//allow the window to select the format
getWindow().setFormat(PixelFormat.UNKNOWN);
//make a reference to the SurfaceVeiw
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
//get surface holder
surfaceHolder = surfaceView.getHolder();
//set fixed surface size
surfaceHolder.setFixedSize(176, 144);
//set Callback interface to the surface holder
surfaceHolder.addCallback(this);
//create a MediaPlayer instance
mPlayer=new MediaPlayer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
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
//set the surface holder to use in displaying the video
mPlayer.setDisplay(surfaceHolder);
try {
//set data source to the video file
mPlayer.setDataSource(this, Uri.parse("android.resource://com.example.andtip/"+R.raw.pixarforbirds));
//prepare the MediaPlayer
mPlayer.prepare();
//start playing the video
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
샘플 코드이다.
원문은 http://www.worldbestlearningcenter.com/tips/Android-play-video-example.htm 에서 확인이 가능하나 데이터가 사라질까봐 코드만 일단 가져왔다.