This is the

On Sat, Jan 1, 2011 at 5:34 AM, bobo123 <[email protected]> wrote:

> Hi All,
>
>  My application use MediaPlayer component to auto play 2 video files
> in the SD card.
>  and it continue to play these 2 video files until user touch the
> screen then it will quit this activity.
>
>
>  At the beginning, The media player these 2 video files very well (no
> error), however when it play several times of the 2 video file, then
> MediaPlayer server was died. it throws an exceptions"
> Java.io.exception: setDataSource error 0x800000... and I quit my
> application,  use Android device itself Video Player to play any video
> files in the SD card, but it doesn't work too. shows Media Server
> Error Dialog.
>
>   I spent a whole day to check my application, but got no result.
>    Attached is my source code, any body can help me?
>
> package com.iqQuest.android.activities;
>
> import java.io.File;
>
> import com.iqQuest.android.structures.MediaType;
> import com.iqQuest.android.utilities.MediaFilesHelper;
>
> import android.app.Activity;
> import android.app.AlertDialog;
> import android.media.AudioManager;
> import android.media.MediaPlayer;
> import android.media.MediaPlayer.OnBufferingUpdateListener;
> import android.media.MediaPlayer.OnCompletionListener;
> import android.media.MediaPlayer.OnErrorListener;
> import android.media.MediaPlayer.OnPreparedListener;
> import android.media.MediaPlayer.OnVideoSizeChangedListener;
> import android.os.Bundle;
> import android.os.Handler;
> import android.os.Message;
> import android.util.Log;
> import android.view.MotionEvent;
> import android.view.SurfaceHolder;
> import android.view.SurfaceView;
> import android.widget.Toast;
>
> public class FormVideoPlayer extends Activity  implements
> OnBufferingUpdateListener, OnCompletionListener,
> OnPreparedListener, OnVideoSizeChangedListener, OnErrorListener,
> SurfaceHolder.Callback {
>    /** Called when the activity is first created. */
>
>    private static final String TAG = "FormVideoPlayer";
>        private SurfaceView mPreview;
>        private SurfaceHolder holder;
>        private MediaPlayer mp;
>
>
>    private boolean mIsVideoSizeKnown = false;
>    private boolean mIsVideoReadyToBePlayed = false;
>    private int mVideoWidth;
>    private int mVideoHeight;
>    private int mPreVideoHeight;
>    private int mPreVideoWidth;
>    private File[] mVideoFiles;
>    private static int playIndex = 0;
>    public static boolean IsActive = false;
>
>    private boolean mifQuit = false;
>
>    private int playCount = 0;
>    private String title = "";
>    private boolean isSetVideoSize = false;
>
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        setContentView(R.layout.form_video_player);
>
>        mVideoFiles =
> MediaFilesHelper.getInstance().load(MediaType.VIDEO);
>
>        mPreview = (SurfaceView) findViewById(R.id.SurfaceView01);
>        holder = mPreview.getHolder();
>        holder.addCallback(this);
>        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
>       // holder.setFixedSize(400, 200);
>       // mp = new MediaPlayer();
>
>    }
>
>
>    private Handler mHandler = new Handler(){
>                   public void handleMessage(Message msg) {
>                           if (mifQuit) return;
>
>                           doCleanUp();
>                           releaseMediaPlayer();
>                           playVideo();
>                   }
>
>           };
>    private Runnable mPlayVideoTask = new Runnable() {
>                   public void run() {
>                            if (mifQuit) return;
>                         Message msg = mHandler.obtainMessage();
>                         Bundle b = new Bundle();
>                         b.putString("message", "Start Play Video");
>                         msg.setData(b);
>                         mHandler.sendMessage(msg);
>                   }
>           };
>
>
>    private void playVideo()
>    {
>        if (mVideoFiles == null || mVideoFiles.length == 0)
>        {
>                Toast.makeText(this.getApplicationContext(), "No video
> files",
> Toast.LENGTH_LONG).show();
>                return;
>        }
>        try {
>
>            String path = mVideoFiles[playIndex].getAbsolutePath();
>
>            mp = new MediaPlayer();
>
> mp.setDataSource(mVideoFiles[playIndex].getAbsolutePath());
>            mp.setDisplay(holder);
>            mp.prepare();
>            mp.setOnBufferingUpdateListener(this);
>            mp.setOnCompletionListener(this);
>            mp.setOnPreparedListener(this);
>            mp.setOnVideoSizeChangedListener(this);
>            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
>
>           // mp.start();
>            //set next play index
>            playIndex++;
>            if (playIndex == mVideoFiles.length)
>                playIndex = 0;
>           // mp.setLooping(true);
>        } catch (Throwable t) {
>                if (mp != null)
>                         mp.release();
>                Log.e(TAG, "error in media prep", t);
>                goBlooey(t);
>        }
>
>    }
>
>    private void goBlooey(Throwable t) {
>        AlertDialog.Builder builder=new AlertDialog.Builder(this);
>
>        builder
>        .setTitle("Exception!")
>        .setMessage(t.toString())
>        .setPositiveButton("OK", null)
>        .show();
>        }
>
>    private void showOnErrorDlg(String message)
>    {
>        AlertDialog.Builder builder=new AlertDialog.Builder(this);
>
>        builder
>        .setTitle("Exception!")
>        .setMessage(message)
>        .setPositiveButton("OK", null)
>        .show();
>    }
>
>    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
>        Log.d(TAG, "onBufferingUpdate percent:" + percent);
>        this.setTitle("onBufferingUpdate");
>
>    }
>
>    public void onCompletion(MediaPlayer arg0) {
>        Log.d(TAG, "onCompletion called");
>        this.setTitle("onCompleted");
>
>       doCleanUp();
>        mp.release();
>        mp = null;
>        playVideo();
>
>       // if (mifQuit) return;
>       //Handler.postDelayed(mPlayVideoTask, 1 * 1000);
>    }
>
>    public void onVideoSizeChanged(MediaPlayer mp, int width, int
> height) {
>        Log.v(TAG, "onVideoSizeChanged called");
>        this.setTitle("onVideoSizeChanged");
>        title = title + "|V";
>        this.setTitle(title);
>        if (width == 0 || height == 0) {
>                title = title + "|I";
>                this.setTitle(title);
>            Log.e(TAG, "invalid video width(" + width + ") or height("
> + height + ")");
>            return;
>        }
>        mIsVideoSizeKnown = true;
>        mVideoWidth = width;
>        mVideoHeight = height;
>        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
>            startVideoPlayback();
>        }
>    }
>
>    public void onPrepared(MediaPlayer mediaplayer) {
>        Log.d(TAG, "onPrepared called");
>        mIsVideoReadyToBePlayed = true;
>        title = title + "|p";
>        this.setTitle(title);
>        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
>            startVideoPlayback();
>        } /*else {
>                mp.release();
>                mp = null;
>                doCleanUp();
>                playIndex++;
>            if (playIndex == mVideoFiles.length)
>              playIndex = 0;
>            playVideo();
>        }*/
>
>    }
>
>    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");
>    }
>
>
>    public void surfaceCreated(SurfaceHolder holder) {
>        Toast.makeText(this.getApplicationContext(), "surfaceCreated",
> Toast.LENGTH_LONG);
>        Log.d(TAG, "surfaceCreated called");
>        this.setTitle("surfaceCreated");
>        playVideo();
>    }
>
>    public void onStart()
>    {
>        IsActive = true;
>        super.onStart();
>    }
>
>    public void onStop()
>    {
>        IsActive = false;
>        mifQuit = true;
>        if (mp != null)
>        {
>                if (mp.isPlaying())
>                        mp.stop();
>        }
>        releaseMediaPlayer();
>        doCleanUp();
>        super.onStop();
>    }
>
>   // @Override
>    protected void onPause() {
>        super.onPause();
>
>    }
>
>
>    @Override
>    protected void onDestroy() {
>        super.onDestroy();
>    }
>
>        public boolean onTouchEvent(MotionEvent event) {
>                boolean ret = super.onTouchEvent(event);
>                this.finish();
>                mifQuit = true;
>                return ret;
>        }
>
>    private void doCleanUp() {
>        mVideoWidth = 0;
>        mVideoHeight = 0;
>        mIsVideoReadyToBePlayed = false;
>        mIsVideoSizeKnown = false;
>        playCount = 0;
>        title="clean up";
>        this.setTitle(title);
>    }
>
>    private void releaseMediaPlayer() {
>        if (mp != null) {
>            mp.release();
>            mp = null;
>        }
>    }
>
>    private void startVideoPlayback() {
>        Log.v(TAG, "startVideoPlayback");
>
>        //mPreVideoHeight;
>        //mPreVideoWidth;
>        if (mPreVideoHeight != mVideoHeight  || mPreVideoWidth !=
> mVideoWidth )
>        {
>
>                mPreVideoHeight = mVideoHeight;
>                mPreVideoWidth = mVideoWidth;
>
>        }
>        title = title + "|setSize";
>        holder.setFixedSize(mVideoWidth, mVideoHeight);
>        //holder.setFixedSize(400, 200);
>        playCount++;
>        //this.setTitle(Integer.toString(playCount));
>       // if (!mp.isPlaying())
>        //{
>
>        title = title + "|Playing";
>        this.setTitle(title);
>        mp.start();
>        //} else {
>                //title = title + "|play";
>                //this.setTitle(title);
>        //}
>    }
>
>        @Override
>        public boolean onError(MediaPlayer mp, int what, int extra) {
>                // TODO Auto-generated method stub
>                if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED)
>                        showOnErrorDlg("Media Server Died");
>                else
>                {
>                        showOnErrorDlg("Unknown Media Server Error");
>                }
>
>                 mp.release();
>         mp = null;
>                return true;
>        }
>
> }
>
>
>
>
>
>
>
>

-- 
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]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to