Android SurfaceView show image after video (clear surface) - android

My task - play video on SurfaceView, after stop show image in SurfaceView
My code for play
mPlayer.setOnCompletionListener(onCompletionListener);
mPlayer.setDisplay(view.getHolder());
mPlayer.reset();
mPlayer.setDataSource(file.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
all ok video playing
in onCompletion
mPlayer.reset();
mPlayer.setDisplay(null);
view.setBackgroundDrawable(Drawable.createFromPath(file.toString()));
but image is transparend and on background i see last video frame - its no good. How clear surface?
I try use
holder.lockCanvas()
but this return null, why?

I have a same problem and i fixed it by using these two lines after player release.
surfaceViewHolder.setFormat(PixelFormat.TRANSPARENT);
surfaceViewHolder.setFormat(PixelFormat.OPAQUE);

Related

How can i Implement a video player that handle video streaming in Android App

In my Android Application I want to reproduce the streaming videos.
For now I currently use the video view but It might works better.. I have this problems :
some files it doesnt execute -> error (1 ,-1005)
video streaming buffering starts after long times
What do you suggest to use instead of videoview ?
I see more Applications that play streaming video inside the app without this problems, but I don't understand what they using for that result. Maybe webview with javascript player ???
you can try surfaceView :
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mediaPlayer.setDataSource(STREAM_URL);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.start();
}
If your min sdk is 16 and above use Exo player
You will find detailed instructions here.

Android-fullscreen seamless video loop in background

I am created an Android app thta needs to have a fullscreen seemless video loop playing in the background. By 'in the background' I mean that there will be buttons on top of the video.
I've read these threadw already
playback video full screen
Integrating video file in android app as app background
but I'm still confused about the following
1 Is the mediaplayer needed for video playback?
2 Will using OnCompletionListener create a 'seamless' loop or will there be a 'hiccup' as the video loops?
Use the setOnPreparedListener to tell the MediaPlayer to loop and start
videoview.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
mp.start();
}
});
This is seemless on some devices, but can cause a frame or two of stutter on others :/

Attach second instance of MediaPlayer to SurfaceView (Error on Android Gingerbread)

I have got an error concerning MediaPlayer in combination with a surfaceview:
I am playing a video in a surfaceview by Streaming it from the Internet as follows:
mPreview = (SurfaceView) findViewById(R.id.surface);
videoFL = (FrameLayout) findViewById(R.id.videoFL);
SurfaceHolder holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Lateron I try to attach my first MediaPlayer to it:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(videos.get(position));
mediaPlayer.prepareAsync();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
While Streaming the first video I am already buffering the second one to save my users time. To attach the second already prepared MediaPlayer instance to my SurfaceView I do the following an release the old one:
mediaPlayer.release();
mediaPlayer = null;
....
mediaPlayer2.setDisplay(holder);
On ICS and Jelly Bean this solution works fine but on Gingerbread it doesn't work as planned. The video is being played but not displayed (black screen and only once a picture is shown somewhere within the video) which I recognized via:
mediaPlayer2.getCurrentPosition();
where I always get the correct position on my SeekBar. I have tried it on an HTC Wildfire S.
Has anyone possibly a guess why this error could happen on Gingerbread?
With regards,
tsemann
I did reduce my version now to one MediaPlayer because as I stated in my question it is true that some 2.x devices do not have the capability to use a second MediaPlayer instance in the same Activity.
So I always reinstantiate my MediaPlayer by calling the prepare()-method for the upcoming video.

Android - changing/replaying video in videoView

App I'm developing contains many short (1-2 sec) videos.
The videos are displayed in one activity. User can either replay video (possibly while video is beeing played) or change actual video.
Part of code changing video:
String videoPath = getVideoPath();
videoView.setVideoPath(videoPath);
videoView.start();
Those 3 lines already causes app to load new video and play it.
Problem starts after video is completed. From this point loading new video causes many problems (Like sometimes for half a movie only sound is played while screen is black blank). There are similar problems with replaying video (which I end up with calling 3 lanes from above).
It seems like android after completing movie releases resources or something like this (and that's why I am settings same path, when I want to replay video).
Ideally I would want video to simply pause and seekTo to beggining of movie after finished playing (but I cannot do this in OnCompletedListener, since it already changed state to stopped...).
Can I somehow achieve this? (By this I mean -> after completed video pauses and seekTo to beginning)
I already tried all combinations of pausing vidoes, suspending them, setting OnPreparedListener, setting OnCompletedListener.
Thx!
Try something like
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mVideoView.setVideoPath(file.getAbsolutePath());
mVideoView.start();
}
});
stop the playback, change video path, start video
videoView.stopPlayback();
videoView.setVideoPath(newVideoPath);
videoView.start();
I refactored my code in a following way: Everytime I need new video I reload whole activity. It works most of times, but now instead of video blackness at the beginning of playing I sometimes get "Cannot open media file" error.
Has it something to do with android resource managment? I release mediaPlayer in onCompletionListener.
Has anyone had such problem with playing many videos from external storage?
You can do something like this:
videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener { mp ->
mp.seekTo(0);//go to second 0
mp.start()// start again
})
I discover in this link:
https://developer.android.com/reference/android/media/MediaPlayer
Do this.
videoView.setOnPreparedListener { mp ->
mp.isLooping = true
)

Video playback gives black screen but with sound

I'm stumped.
I'm trying to play video with the Media Player but while the audio plays, all I get is a black window.
I've seen other posts about this problem but I haven't been to find a solution. I have tried to follow their suggestions.
The mediaplayer is prepared before playback. The surface holder was created and set to the media player's display before playback.
Tested on a Samsung Galaxy Tab and a Samsung Galaxy S.
I'm compiling against API level 7.
The video itself can be played in device's video application from the sdcard, so it should be compatible.
The surface view is not the same size as the video. So that might be an issue. Do I need to do something about that or is stretching handled automatically?
Here's what I have in my initialization:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.leftMargin = x;
lp.topMargin = y;
mSurfaceView = new SurfaceView(mActivity);
mSurfaceView.requestFocus();
mSurfaceView.setZOrderOnTop(true);
mSurfaceView.getHolder().addCallback(player);
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mLayout.addView(mSurfaceView, lp);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mMediaPlayer.setOnErrorListener(player);
mMediaPlayer.setOnPreparedListener(player);
mMediaPlayer.prepare();
and here are my callbacks:
public void onPrepared(MediaPlayer mp)
{
mMediaPlayer.start();
}
public void surfaceCreated (SurfaceHolder holder)
{
mMediaPlayer.setDisplay(holder);
}
What's frustrating is that a version of the code was working properly a while ago, but now it's not.
I had a similar problem, and was related to the video format (codec MP4, WMV, AVI, etc).
Try running the video on default player of the Android, see if that works. If not works, then it may be problem in video codec.
Do not try to run the video on players like VLC or Player MX, they have embedded codec.

Categories

Resources