Hello fellow developers!

I'm having quite a hard time using the android.media.MediaPlayer
class.
I would like to implement a very simple proof of concept Activity that
just launches a mp4 video.
I've successfully achieved this by using a callback like this:

See ACTIVITY 1.

It successfully plays the video.

After that I tried the Callback-free version:

See ACTIVITY 2.

The result is sound coming out just fine and a black square instead of
a video.

Does anyone knows what is going on here?
In using:
android-sdk-linux_x86-1.1_r1
java-sun6-sdk
Ubuntu 9.04 32bits

Thank you!

##############BEGIN ACTIVITY 1###########
package test.test;

import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

class MyCallback implements SurfaceHolder.Callback,
MediaPlayer.OnPreparedListener{

        private String path;
        private MediaPlayer mMediaPlayer;

        public MyCallback(String string) {
                path=string;
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int
width,int height) {}

        public void surfaceCreated(SurfaceHolder holder) {
        mMediaPlayer = new MediaPlayer();
        try {
                        mMediaPlayer.setDataSource(path);
                        mMediaPlayer.setDisplay(holder);
                mMediaPlayer.prepare();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void onPrepared(MediaPlayer mp) {
                mp.start();
        }

}

public class test extends Activity {

        private SurfaceView mPreview;
        private SurfaceHolder holder;

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPreview = (SurfaceView) findViewById(R.id.SurfaceView01);
        holder = mPreview.getHolder();
        holder.addCallback(new MyCallback("/sdcard/yu2.mp4"));
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}

##############END OF ACTIVITY 1###########

##############BEGIN ACTIVITY 2###########
package test.test;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class test extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Button but = (Button) findViewById(R.id.Button01);
                but.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                show();
                        }
                });
        }

        private void show() {
                SurfaceView sv = (SurfaceView) findViewById(R.id.SurfaceView01);
                SurfaceHolder holder = sv.getHolder();
                holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                MediaPlayer mp = new MediaPlayer();

                try {
                        mp.setDataSource("/sdcard/yu2.mp4");
                        mp.setDisplay(holder);
                        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mp.prepare();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                mp.start();
        }
}
##############END OF ACTIVITY 2###########

##############LAYOUT###########
<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:id="@+id/AbsoluteLayout01"
xmlns:android="http://schemas.android.com/apk/res/android";
android:layout_height="fill_parent"
android:layout_width="fill_parent"><SurfaceView android:id="@+id/
SurfaceView01" android:layout_height="100px"
android:layout_width="100px"></SurfaceView>
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_y="200px"></
Button>
</AbsoluteLayout>

##############END OF LAYOUT###########
--~--~---------~--~----~------------~-------~--~----~
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