hi, I just play a mp4 file from the raw successfully. Steps as
follow:
1) create a SurfaceView as the active's current view.
2) make this surfaceView as the MediaPlayer's display.
3) maybe, you also need to set a appropriate higth/width for the
surfaceView.
A problem existed that the emulator will be halt when it has not
finish playing the media.
Code just for a reference.
package com.dh;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.Menu.Item;
public class Hello extends Activity implements OnErrorListener,
OnBufferingUpdateListener, OnCompletionListener {
private static final String TAG = "Hello";
private static final int PLAY_VIDEO = Menu.FIRST + 1;
private static final int PLAY_MP3 = Menu.FIRST + 2;
private static final int STOP_PLAY_ID = Menu.FIRST + 4;
private MediaPlayer mp;
private Preview mPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.v(TAG, "onCreate: ===> ");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.TRANSPARENT);
mPreview = new Preview(this);
setContentView(mPreview);
}
private void playVideo() {
try {
// SurfaceView view = new SurfaceView(this);
// Surface surface = view.getHolder().getSurface();
// setContentView(view);
Log.v(TAG, "height: " + mPreview.getHeight());
Log.v(TAG, "width: " + mPreview.getWidth());
mp = MediaPlayer.create(this, R.raw.samplemp4);
mp.setOnErrorListener(this);
mp.setDisplay(mPreview.getHolder().getSurface());
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
}catch(Exception e) {
//e.printStackTrace();
Log.e(TAG, "error: " + e.getMessage());
stopPlay();
}
}
private void playMp3() {
mp = MediaPlayer.create(this, R.raw.testmp3);
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
}
private void stopPlay() {
if (mp != null) {
mp.stop();
mp.release();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, PLAY_MP3, R.string.menu_playMp3);
menu.add(1, PLAY_VIDEO, R.string.menu_playVideo);
menu.add(1, STOP_PLAY_ID, R.string.menu_stop);
return result;
}
@Override
public boolean onOptionsItemSelected(Item item) {
Log.v(TAG, "option: " + item.getId());
switch (item.getId()) {
case PLAY_MP3:
playMp3();
break;
case PLAY_VIDEO:
playVideo();
break;
case STOP_PLAY_ID:
stopPlay();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected boolean isFullscreenOpaque() {
// Our main window is set to translucent, but we know that we
will
// fill it with opaque data. Tell the system that so it can
perform
// some important optimizations.
return false;
}
@Override
protected void onResume()
{
// Because the CameraDevice object is not a shared resource,
// it's very important to release it when the activity is
paused.
super.onResume();
// mPreview.resume();
}
@Override
protected void onPause()
{
// Start Preview again when we resume.
super.onPause();
// mPreview.pause();
}
public void onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:" + what + " extra:" + extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate---> percent:" + percent);
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion---> finished");
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
SurfaceHolder mHolder;
private boolean mHasSurface;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.setCallback(this);
mHasSurface = false;
//mHolder.setFixedSize(320, 240);
mHolder.setFixedSize(192, 242);
}
public boolean surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, start our main acquisition
thread.
mHasSurface = true;
return true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return. Stop the preview.
mHasSurface = false;
}
public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h) {
// Surface size or format has changed. This should not happen
in this
// example.
}
}
On 12月2日, 上午9时17分, "Mark Bakker" <bakker.m
...@gmail.com> wrote:
> Hi all,
> I am still trying to play a simplevideo(avi, mp4) with the media
> player of android. At
> the moment I can't get it started... has anyone idea;s?
> SurfaceView view = (SurfaceView) findViewById(R.id.surface);
> Surface surface = view.getHolder().getSurface();
> MediaPlayer mediaPlayer = new MediaPlayer();
> try {
> mediaPlayer.setDataSource("/test.avi");
> } catch (IllegalArgumentException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> mediaPlayer.setDisplay(surface);
> mediaPlayer.prepare();
> mediaPlayer.start();
> On Nov 17, 2007 11:04 PM, Peter Blazejewicz <peter.blazejew...@gmail.com> wrote:
> > hi John,
> > try using res/raw folder with placed samplevideoin it (then you can
> > access it using R.raw.your_video pointer),
> > If I remember well remote playback is not supported so you need to
> > emulate it,
> > regards,
> > Peter
> > On Nov 17, 2:45 pm, John Albano <jalb...@voodoovox.com> wrote:
> > > Hi all,
> > > Does anyone have a simple example of playingvideo? I've tried the
> > > following, but get an ActivityNotFoundException (no activity found to
> > > handle intent)...
> > > ContentURI myURL = new ContentURI("http://.../sample.mp4");
> > > Intent intent = new Intent(Intent.VIEW_ACTION, myURL);
> > > intent.setType("video/mpeg");
> > > startActivity(intent);
> > > Thanks
> > > John