MediaPlayer not showing up

All your problems with Audio, Video and Images.

MediaPlayer not showing up

Postby littleandroid101 » Sun Nov 14, 2010 6:30 am

I had searched a few websites regarding on how to display a video to the Android application.

I have looked at:
http://davanum.wordpress.com/2009/12/04 ... le-take-2/
multimedia-problems-f28/videoview-doesn-t-work-at-all-mediaplayer-is-a-no-go-t16384.html
http://developer.android.com/resources/ ... Video.html <---this one doesn't work for me, it force closes

So, I was wondering why am I not able to view the MediaPlayer. Is it because of my emulator or API? Perhaps, the file size is too big that it never finished buffering the video. I'm using API 8 if it matters. I'll be honest, I don't understand what is going on in the logcat.

Here is my code so far.

Code: Select all
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import android.util.Log;

public class Test extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
    private static final String TAG = "Test";
    private static final String dataSourceUrl = "http://www.ultivillage.com/images/stories/UVTV/WJUC2010/2010WJUCFranceVsSwitzerlandCOTD.mp4";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    private void playVideo() {
        doCleanUp();
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(dataSourceUrl);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }
   
    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 onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            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;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");
        playVideo();

    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");

    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaPlayer();
        doCleanUp();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMediaPlayer();
        doCleanUp();
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }}


main.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <SurfaceView
      android:id="@+id/surface"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center">
   </SurfaceView>   
</FrameLayout>


Logcat error
Code: Select all
11-13 21:23:25.266: ERROR/MediaPlayer(749): error (1, -2147483648)
11-13 21:23:25.606: ERROR/MediaPlayer(749): Error (1,-2147483648)


Logcat debug
Code: Select all
11-13 21:23:52.320: DEBUG/Test(749): onBufferingUpdate percent:99
11-13 21:23:53.327: DEBUG/Test(749): onBufferingUpdate percent:99
11-13 21:23:54.337: DEBUG/Test(749): onBufferingUpdate percent:99


I'm totally lost right now, and I have no clue on what to do. I will really appreciate if I can get some help.
littleandroid101
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Nov 14, 2010 5:00 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests