In the previous post, you learnt to use the MediaPlayer class to play, pause, and stop a sound file. In this Android tip, I am going to show you how to use the MediaPlayer class to play a 3gp video file. Playing the video file is simple as playing the sound file. However, you need a SurfaceView to display the video.
Now to demonstrate the video playing example with MediaPlayer, you will create a new Android project. Then modify the acitivity_main.xml file to add one SurfaceView as shown below.
<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>
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
}
Posted by: Dara | post date: 07-12-2014 | Subject: Android Apps Development
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.