OnVideoSizeChanged never called - android

I'm working with a VideoView for showing a video stream. Because I need the MediaController to be attached to the view itself and I want to prevent the black screen flicker caused by the videoview. I've tested the below code on my Nexus 7, worked like a charm. But now I tested it on my SGS2, and for some reason OnVideoSizeChanged is never called.
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.i("ONPREP", "called");
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
videoview.setMediaController(mc);
mc.setAnchorView(videoview);
videoview.setBackgroundColor(Color.TRANSPARENT);
Log.i("ONSIZECHANGE", "called");
}
});
}
The video is playing like it should, I tested this by removing the OnVideoSizeChangedListener and just putting the code in the onPrepared method, but this is bringing back the screen flicker issue and leaves the mediacontroller unattached. This behaviour is also shown by the Log info in LogCat, ONPREP tag shows but never the ONSIZECHANGE. On my N7 both show, obviously cause the background color is changed.
This is the code that sets and starts the video:
videoview.setOnPreparedListener(MainActivity.this);
mc = new MediaController(MainActivity.this);
mc.setMediaPlayer(videoview);
videoview.setVideoURI(videourI);
videoview.start();
So why is it called on one device and not on another?
Edit:
Tested it on my Note 2, same issue as on the SGS2.

onVideoSizeChanged is triggered by an event from the low-level player implementation, which can be different on different platforms. It is perfectly legal for the player to call onVideoSizeChanged before onPrepared.
Actually VideoView is setting its own OnVideoSizeChangedListener callback for managing surface size, so you probably shouldn't use it at all. From your example it is not clear to me why you are using it, setMediaController should be called in onPrepared, not onVideoSizeChanged (which can be called multiple times).

This seems to be a bug with the samsung devices.
I noticed in my application that if the videoview size is 0x0 (probably also if it is invisible/gone) the onVideoSizeChange is not called. Once I set the videoview size to a non 0x0 value the onVideoSizeChange is called correctly.

Related

Playing video in surfaceview

My app plays a video inside a surfaceview. And I stop the mediaplayer from playing when onPause(). However, when I return from another activity, the surfaceview is all black and starts to show frames only if I start playing video again. I want to know if there is a method to preserve the last frame when it stops. Thanks a lot.
I used 2 SurfaceViews, first for playing videos and second surf for displaying pictures.
when I return from pause, if mediaplayer is not playing, I get current frame of video by this part of code:
try {
mediaMDRetriever.setDataSource(mediaPath);
picBitmap = mediaMDRetriever.getFrameAtTime();
} catch (Exception e) {
}
and draw it on the second surfview.
you can clear second SurfaceView if needed by the following code:
Canvas surfCanvas = surfHolder2.lockCanvas();
surfCanvas.drawColor( 0, PorterDuff.Mode.CLEAR );
surfHolder2.unlockCanvasAndPost( surfCanvas );

AudioRecord in Android live wallpaper

I am working on a live wallpaper that uses a AudioRecord object to listen to the phone's mic. This works fine until I start using the wallpaper and then also view the preview wallpaper in the wallpaper selection menu. When this happens, I start having problems starting and stopping the AudioRecord object. Currently this is handled by onVisibilityChanged:
#Override
public void onVisibilityChanged(boolean visible) {
if (this.visible == visible)
return;
this.visible = visible;
if (visible) {
recorder.startRecording();
handler.post(drawRunner);
} else {
recorder.stop();
handler.removeCallbacks(drawRunner);
}
}
Is there some solution to this? I have tried several things, but nothing has worked out. This solution does (rarely) work, and as far as I can tell this is because the two instances of the wallpaper engine update their visibility in an unpredictable order. Sometimes the main wallpaper sets itself to not be visible before the preview sets itself to be visible, and everything is fine. Sometimes this order is reversed and everything breaks.
So, I found an answer and it was stupidly simple. I swear I tried this already and it didn't work, but everything is working fine now. All I had to do was move the AudioRecord object outside of the engine class and into the service class. I then create the AudioRecord object in onCreateEngine only if it isn't already instantiated. That way, all engine instances use the same AudioRecord object.

Kindle Fire Tablet 2.3.4: Videoview getting black in android on touching other part of the screen.

I have a RelativeLayout inside that added an imageview and have added a videoview to this relative layout. I am running the video from my sd card path. Its all working perfectly fine on all other devices like Samsung tab 7inch, 10inch but I am available with Kindle Fire with android version 2.3.4. The moment I touch and drag my finger on anywhere on the screen (including videoview area and other area) it turns into black color and if I again do the same thing it comes back, though i can still hear the sound. Any Help is really appreciated. Thanks in advance.
Here is the code
play.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.v("Layer Data draw", "ONPREPARE");
//if(getAutoplay())
play.start();
SystemClock.sleep(300); // parent.removeView(frame);
Toast.makeText(con.getApplicationContext(), "The val is :: ",
Toast.LENGTH_SHORT).show(); frame.setVisibility(View.GONE);
} });

AndEngine - lagging when playing sound

I'm creating a simple game with AndEngine. A ball is dropped toward a floor, and whenever it collides with the floor, I want to play a short colliding sound. In method onUpdate(), I check for collision and play sound accordingly.
I use class Sound for playing sound (as in the SoundExample of AndEngine). Testing on Samsung Galaxy S2.
The problem is the program gets lagged when the sound is played. And it even affects game physics (sometimes the ball bounces higher than the highest point when disabling sound).
This is the code:
public void onUpdate(float pSecondsElapsed) {
// mSound.play();
if (this.mSprite.collidesWith(ball.getSprite())) {
if (!colliding && mSound != null){ // play sound for first collision only
mSound.play();
colliding = true;
}
}
else{
colliding = false;
}
}
If I remove mSound.play() or keep playing sound (remove comment at line 2), the program works smoothly.
Does anyone encounter the same problem? And have a solution to get rid of the lag? Many thanks!
as that you mentioned that it works smoothly when you keep playing the sound .. then the problem is not with the sound
the collidesWith() method is probably your culprit, remember that onUpdate gets called every frame .. maybe you'll have to redesign your code or limit the number of frames per second [change your engine options to use a FixedStepEngine to achieve that]

VideoView and step play (frame by frame)

I would like to play frame by frame with the videoview.
I have this:
mVideoView.seekTo(mVideoView.getCurrentPosition()+1);
But after this I do not see the frame until I click play... I do not want that, I just want to see the next frame.
Also - can I do the same for previous frame?
Thanks in advance.
It will not seek while paused. But you can always pause right after seeking, if you use a SurfaceView and a MediaPlayer because VideoView doesn't have the onSeekComplete callback.
public void seek() {
mediaplayer.start();
mediaplayer.seekTo(mediaplayer.getCurrentPosition()+1);
}
public void onSeekComplete(MediaPlayer mp) {
mediaplayer.pause();
}
This will seek and display something. Unfortunately you will find that this does not allow you to play frame by frame.
You can use MediaMetadataRetriever to select the frame you want and next get it as a BitMap using MediaMetadataRetriever.getFrameAtTime(long timeUs). It will work much better than using a MediaPlayer.
I hope it is useful.

Categories

Resources