Hi Robert,

 What if you set mSF to null in surfaceDestroyed() call... Does that help?

 Thanks,
 Megha

On Fri, Apr 11, 2008 at 2:27 PM, Robert <[EMAIL PROTECTED]> wrote:

>
>
> Hi Megha,
>
> I am successfully using your classes to capture and view video files.
> But, can't seem to reliable reuse the same surface when returning from
> a subactivity. I tried playing a video file in onActivityResult(), and
> although managed to play it on rare occasions, most of the time the
> video is not shown. Since I've been running the same code over and
> over again (and only sometimes successfully), I am assuming that the
> surface is not ready to receive the video sequence. How can I make
> sure surfaceCreated() has already done its job? Should I add something
> to onResume() or onRestart() in my main activity?
>
> The relevant code:
>
> public class mediaclass extends Activity implements
> SurfaceHolder.Callback {
>  private SurfaceView mSF;
>  private SurfaceHolder mHolder = null;
>  private boolean mHasSurface;
>  private MediaPlayer myPlayer;
>
>  @Override public void onCreate(Bundle icicle) {
>        super.onCreate(icicle);
>        setContentView(R.layout.main);
>        mSF = (SurfaceView) findViewById(R.id.preview);
>        mSF.getHolder().addCallback(this);
>        mHasSurface = false;
>  }
>
>  public void surfaceCreated(SurfaceHolder holder) {
>        mHolder = holder;
>        mHasSurface = true;
>  }
>
>  public void surfaceDestroyed(SurfaceHolder holder) {
>        mHolder = null;
>        mHasSurface = false;
>  }
>
>  @Override protected void onActivityResult(int requestCode, int
> resultCode, String data, Bundle extras) {
>        super.onActivityResult(requestCode, resultCode, data, extras);
>
>        myPlayer = new MediaPlayer();
>        if (mHasSurface == true){
>                myPlayer.setDisplay(mHolder.getSurface());
>        }
>        try {
>                myPlayer.setDataSource(data); // data contains the full
> path/
> filename (from SD card)
>                myPlayer.prepare();
>        } catch (IOException e) {
>                Log.e(TAG, e.getMessage(), e);
>        }
>        myPlayer.start();
>  }
>
> As you can see, I am checking mHasSurface, before attaching to the
> surface. I even tried waiting for mHasSurface to become true in an
> "infinite" loop, but no luck with that either :( What else can I try?
>
> This is how the surface is defined in main.xml:
> <SurfaceView
> android:id="@+id/preview"
> android:layout_width="176dip"
> android:layout_height="144dip"
> android:layout_alignParentTop="true"
> android:layout_marginLeft="4px"
> />
>
> Also can you suggest a way to display a PNG image from SD card on a
> SurfaceView? Seems that MediaPlayer was not designed for this.
>
> Thanks and regards
> Robert
>
> On Apr 8, 1:19 am, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > This is because the surface is not yet ready for the mediaplayer, please
> > call the mediaPlayer initialization code in or after OnSurfaceCreate()
> > callback..
> >
> > Thanks,
> > Megha
> >
> > On Mon, Apr 7, 2008 at 9:45 AM, fubin <[EMAIL PROTECTED]> wrote:
> >
> > > In Sample given by google There is a button to invoke a file.
> > >    private void playVideo(Integer Media) {
> > >        try {
> > >            // If the path has not changed, just start the media
> > > player
> > >            path = "/data/room.mp4";
> >
> > >            // 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 OnClickListener mPlayListener = new OnClickListener() {
> > >        public void onClick(View v) {
> > >            playVideo(extras.getInt(MEDIA));
> >
> > >        }
> > >    };
> >
> > > When I copy the code directly to onCreate like the following program
> > > creshed...:
> >
> > >    public void onCreate(Bundle icicle) {
> > >        super.onCreate(icicle);
> > >        setContentView(R.layout.mediaplayer);
> > >        mPreview = (SurfaceView) findViewById(R.id.surface);
> >
> > >        // Set the transparency
> > >        getWindow().setFormat(PixelFormat.TRANSPARENT);
> > >        // Set a size for the video screen
> > >        holder = mPreview.getHolder();
> > >        holder.addCallback(this);
> > >        try {
> > >            // If the path has not changed, just start the media
> > > player
> > >            path = "/data/room.mp4";
> >
> > >            // 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);
> > >        }
> > > }
> >
> > > There is no error information for debug.
> > > Anyone have the same problem?
> > > Thanks in advance.
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to