Hi, I have a problem using MediaPlayer SDK. ( M5-rc15, Windows )
With this problem I can't test my application without crash...It's
critical.
I modified Megha Joshi MediaPlayerTest sample code to reproduce the
problem . (http://groups.google.com/group/android-developers/files)
I added stop button in PlayVideo and I played test.mp4 in sample
project.
In PlayVideo from LocalFile menu.
Press Play, player started and Press Stop button, repeatedly.
Application will crash within 10 trial.
I submit this issue to issue tracker. And I wrote about this bug in
document about my application.
But I am not sure about this problem can be avoidable....
Can this problem be avoided?
-----------------------
MediaPlayer_Video.java------------------------------
package com.google.android.samples.media;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioSystem;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
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 MediaPlayer_Video extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private static final String TAG = "MediaPlayer";
private MediaPlayer mp;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Button mPlay;
private Button mStop;
private Bundle extras;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
/**
*
* Called when the activity is first created.
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mediaplayer);
mPreview = (SurfaceView) findViewById(R.id.surface);
mPlay = (Button) findViewById(R.id.play);
mPlay.setOnClickListener(mPlayListener);
mStop = (Button) findViewById(R.id.stop);
mStop.setOnClickListener(mStopListener);
// Set the transparency
getWindow().setFormat(PixelFormat.TRANSPARENT);
// Set a size for the video screen
holder = mPreview.getHolder();
holder.addCallback(this);
extras = getIntent().getExtras();
/*
* mp.prepare() from oncreate() causes exception
*
* playVideo(extras.getInt(MEDIA));
*/
createFile();
}
private void playVideo(Integer Media) {
try {
// If the path has not changed, just start the media
player
if (mp != null) {
mp.start();
return;
}
switch (Media) {
case LOCAL_VIDEO:
path = "/tmp/test.mp4";
break;
case STREAM_VIDEO:
path = "http://.../test.mp4";
break;
}
// Create a new media player and set the listeners
mp = new MediaPlayer();
mp.setDataSource(path);
mp.setDisplay(holder.getSurface());
mp.prepare();
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioSystem.STREAM_MUSIC);
Log.d("\n\nCREATE", "mediaplayer");
mp.start();
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
}
}
private void createFile() {
int len;
byte buf[] = new byte[1024];
InputStream is =
this.getResources().openRawResource(R.raw.test);
try {
FileOutputStream fo = new
FileOutputStream("/tmp/test.mp4");
while((len = is.read(buf)) > 0) {
fo.write(buf, 0, len);
}
fo.close();
is.close();
} catch(Exception e) {}
}
void stopVideo() {
mp.stop();
mp.release();
mp = null;
}
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
stopVideo();
}
};
private OnClickListener mPlayListener = new OnClickListener() {
public void onClick(View v) {
playVideo(extras.getInt(MEDIA));
}
};
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate percent:" + percent);
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
mp.start();
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int
j, int k) {
Log.d(TAG, "surfaceChanged called");
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceDestroyed called");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.d(TAG, "surfaceCreated called");
// mp.start();
}
}
-----------------------mediaplayer.xml------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView android:id="@+id/surface"
android:layout_width="176px"
android:layout_height="144px"
android:layout_gravity="center">
</SurfaceView>
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button android:id="@+id/play"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Play"
/>
<Button android:id="@+id/stop"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Stop"
/>
</LinearLayout>
</LinearLayout>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---