Hi Megha,
It makes sense to start playing only in onPrepared(), but this doesn't
help much. I also tried various other combinations to make this work,
but can't seem to nail down the right one. Only on rare occasions the
mp4 files recorded with MediaRecorder actually play under my
conditions (returning from a ListActivity).
But, I learned that external mp4 files (pushed into the emulator) can
be played like this:
- at the first attempt to play, only sound can be heard
- I have a pause/continue button and can pause/restart the audio
- I wait for the sequence to end, then press play again (in the same
activity) and the video is now shown
With my recorded files, however, the pause button is not even working,
because I can only pause a running video. I call myPlayer.isPlaying()
to check. My recorded video files don't contain audio, and I wonder if
this may have any influence. I recall reading about problems playing
3gp files with VideoView because of the missing sound stream.
I try to add in audio into the recorded sequence:
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
But, the MediaPlayer throws IOException when it reaches
myPlayer.prepare() when I want to play back that file.
Anyway, I guess I did all I could to get past this problem in the last
few days, but can't work on this until the last minute. I submitted my
application, writing a note about this issue in the readme file. Hope
the judges will understand :)
Thanks for all your help,
Robert
On Apr 14, 10:41 pm, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> About the MediaPlayer, I think the issue is that along with waiting for the
> surface being created, the start() method should be called when the
> MediaPlayer is prepared...
>
> I tried moving the code around and it works the way you wanted it to:
>
> public void surfaceCreated(SurfaceHolder holder) {
> // TODO Auto-generated method stub
> Log.d(TAG, "surfaceCreated called");
> mp.setDisplay(holder.getSurface());
> try{
> mp.prepare();}
> catch(Exception e){}
> mp.setOnBufferingUpdateListener(this);
> mp.setOnCompletionListener(this);
> mp.setOnPreparedListener(this);
> mp.setAudioStreamType(AudioSystem.STREAM_MUSIC);
> Log.d("\n\nCREATE", "mediaplayer");
>
> }
>
> public void onPrepared(MediaPlayer mediaplayer) {
> Log.d(TAG, "onPrepared called" + mp.getDuration());
> mp.start();
>
> }
>
> Thanks,
> Megha
>
> On Sat, Apr 12, 2008 at 5:27 AM, Robert <[EMAIL PROTECTED]> wrote:
>
> > Unfortunately setting mSF to null had no influence. Some additional
> > info on what's happening:
>
> > If I leave <SurfaceView> in my main.xml, the surface won't show the
> > video, but will contain the info what was displayed on it in the
> > subactivity. If I use <VideoView> in main.xml, the subactivity info is
> > erased, but the surface remains black.
>
> > There are some rare occasions when a video is playing, but when I
> > return to the subactivty, and select the same file again, it no longer
> > plays in the main activity.
>
> > I also tried with VideoView (in main.xml and Java code), with this:
>
> > VideoView mVideoView = (VideoView)findViewById(R.id.preview);
> > mVideoView.setVideoURI(Uri.parse("file://" + data));
> > mVideoView.setMediaController(new MediaController(this));
> > mVideoView.requestFocus();
>
> > But, I experience the same thing, and additionally sometimes the app
> > crashes.
>
> > Any other ideas to try out?
>
> > Thanks,
> > Robert
>
> > On Apr 12, 4:41 am, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
> > > 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
-~----------~----~----~----~------~----~------~--~---