PalyTestActivity.class is the main activity class
=================================
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PalyTestActivity extends Activity implements SurfaceHolder.Callback
{
Button btStop;
private MediaPlayer mediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btStop=(Button)findViewById(R.id.stop);
mediaPlayer = new MediaPlayer();
SurfaceView surface = (SurfaceView)findViewById(R.id.surface);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
btStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
callstop();
}
});
}
private void callstop()
{
mediaPlayer.stop();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
/*try {
mediaPlayer.setDisplay(holder);
} catch (IllegalArgumentException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
} catch (IllegalStateException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
}*/
try {
mediaPlayer.setDisplay(holder);
// if want to play from sd card you can use this code
/ /mediaPlayer.setDataSource("/sdcard/test2.3gp");
Uri myuri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.yourvideofile);
//make a folder in res which name is raw and put video files //
mediaPlayer.setDataSource(PalyTestActivity.this, myuri);
mediaPlayer.prepare();
mediaPlayer.setVolume(1f, 0.5f);
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.start();
int pos = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
}
catch (IllegalArgumentException e)
{
Log.d("MEDIA_PLAYER", e.getMessage());
}
catch (IllegalStateException e)
{
Log.d("MEDIA_PLAYER", e.getMessage());
}
catch (IOException e)
{
Log.d("MEDIA_PLAYER", e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
mediaPlayer.release();
}
}
=========================================
Main.xml
======================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop" />
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="209dp" />
</LinearLayout>
=================================================
//////////////////////////////////////manifest.xml///////////////////////////////////////////
=================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.play"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PalyTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment