How to avoid MediaPlayer's starting black screen? - android

So in my app MediaPlayer is based on SurfaceView by extending like:
public class VideoView extends SurfaceView {}
now, it uses MediaPlayer inside and what I want to achieve is to make starting color of screen white instead of black. VideoView is placed in white-backgrounded-layout by itself. It is white but atm it starts playing video the VideoView becomes black for a second. Video by itself has white background and its first frames are not black so I assume that its about MediaPlayer. When it starts playing it turns screen black by default. I had guessed that I could use transition like:
TransitionDrawable transition = new TransitionDrawable(new Drawable[]{new ColorDrawable(Color.WHITE), new ColorDrawable(Color.TRANSPARENT)});
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
this.setBackgroundDrawable(transition);
else
this.setBackground(transition);
transition.setCrossFadeEnabled(true);
transition.startTransition(1000);
However this doesn't help much. So is there a way to achieve my goal?
Forgot to mention that its local video file (from SD card) is being played. So black screen is not about a long HTTP-session establishing or other stream related things. And preparation is done via prepareAsync();

So I found a nasty solution finally. Oh well its better than nothing:
private TransitionDrawable transition;
private Animation videoViewFadeInAnimation;
#SuppressWarnings("deprecation")
public void start() {
if (videoViewFadeInAnimation == null){
videoViewFadeInAnimation = new AlphaAnimation(0, 1);
videoViewFadeInAnimation.setDuration(600);
videoViewFadeInAnimation.setFillAfter(true);
videoViewFadeInAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {}
#Override
public void onAnimationRepeat(Animation arg0) {}
#Override
public void onAnimationEnd(Animation arg0) {
if (DeviceInfo.hasAPI11())
setAlpha(1);
}
});
}
if (transition == null){
transition = new TransitionDrawable(new Drawable[]{new ColorDrawable(Color.WHITE), new ColorDrawable(Color.TRANSPARENT)});
transition.setCrossFadeEnabled(true);
if(DeviceInfo.hasAPI16())
this.setBackground(transition);
else
this.setBackgroundDrawable(transition);
}
if (isInPlaybackState()) {
if (mCurrentState!=STATE_PAUSED){
startAnimation(videoViewFadeInAnimation);
transition.startTransition(1);
}
hideVideoControls();
mMediaPlayer.start();
mCurrentState = STATE_PLAYING;
}
mTargetState = STATE_PLAYING;
}
its a method of my VideoView which starts video playing. Basically it makes 0 alpha for VideoView and a white bg for 1ms (doesn't work without bg transition) and then animates alpha from 0 to 1 for 600ms. If use value lt 600 then the annoying black screen becomes noticeable. However 600ms of no video visible also not the best thing cuz visually audio starts earlier. This solutions doesn't work always and don't know why.

Related

VideoView turn into black screen

I am developing multi player video app, so in that i created 9 views 3*3.
when i initialize all (3*3) videoview then working properly for few seconds and after some time video is goes to black screen, not show single video, i am not understand this issue is device oriented or android not supports more than 1 video in activity, anyone know how to resolve this issue, otherwise if video is turn into black then how to identify video is turned into black screen, if we found this then i will refresh view and again start video, i don't know it is correct way or not.
please anyone know about how to resolve this issue then please share information !
i use below code for show multiple video view in one activity
videoPlayer.setVideoPath("path");
videoPlayer.start();
videoPlayer.requestFocus();
videoPlayer.setKeepScreenOn(true);
set prepare listener
videoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
}
set completion listener
videoPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//play next
}
});
handle error listener
videoPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
// play next
return true;
}
});
when video play next before that i set some propertied to mediaplyer object
mediaPlayer.setDisplay(null);
mediaPlayer.reset();
mediaPlayer.setDisplay(videoPlayer.getHolder());
i share basic code of my project, please tell me if anything is missing from me or android is not supported multiple videos in same activity.

How to display a video with a transparent/masked background?

The wording on this is most likely wrong, but I want to play this video without having the background black. If you look at this webpage you will see that the video is on a white background which leads me to believe that it is cropped or masked. Which view would I use to do this? I have tried both TextureView and VideoView but both have a black background by default.
I solve it with this:
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.d(TAG, "onInfo, what = " + what);
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
// video started; hide the placeholder.
placeholder.setVisibility(View.GONE);
return true;
}
return false;
}
});
I think onPrepared just means the video is ready to play, but not means video started playing. If hide placeholder in onPrepared, the screen still show a black screen.
On my Note4 and Nexus, this solution works well.

Android, hide video without stopping audio playback

(Sorry for my english)
I 'm trying to show video using TextureView, but I need to know if there is a way to hide the video without stopping audio playback.
Thus I am doing:
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mySurface = new Surface(surface);
if(MyService.mMediaPlayer != null) MyService.mMediaPlayer.setSurface(mySurface);
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if(MyService.mMediaPlayer != null) MyService.mMediaPlayer.setSurface(null);
return false;
}
You can see that I'm trying using setSurface(null) but the audio does not remain when I try this.
EDIT:
I have a button in my main activity ( "show video") ; this button starts the second activity where I put the above code .
I need the audio keeps playing when I press the Back button or the home button
Set the visibility of your TextureView to INVISIBLE
mTextureView.setVisibility(View.INVISIBLE);

How to play sound in while performing certain Animation in Android

I am making an App which does certain processing for 5 seconds and then giving a result.
During that time I want to apply some Animation and play some sound to avoid user to sit idle watching whats happening.
I have applied the Animation and need some help in how to play sound for that particular time.
I have one sound file for 5 seconds.
Any ideas are very welcomed.
Animation myAnimation = AnimationUtils.loadAnimation(this, R.anim.myanimation);
animationFalling.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//here you can play your sound
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// here you can stop playing your sound
}
};
About how to play your sound, there is MediaPlayer or SoundPool in order to play them.
Try reading the documentation of:
http://developer.android.com/reference/android/media/SoundPool.html
This handles playing sounds on the OS.

Android VideoView, need to switch between videos quickly

I'm developing a new app and am using VideoView to display mpeg clips.
I need to switch between videos very quickly, however, loading a new clip to a VideoView seems to take about half a second of black screen.
The transition must be seamless, how would you go about solving this kind of problem?
I had a similar issue and resolved with a still-image (ImageView) transition:
build a FrameLayout with an ImageView over the VideoView
the ImageView shows the first frame of the video
initially the ImageView is visible
start the video, wait for an arbitrary time (e.g. 2-300ms)
hide the ImageView
to switch two videos:
- show the image
- switch the video
- hide the image
a bit hackish but worked for me
I faced the same problem, but I used mediaplayer, my code is:
Here I use a button to trigger the switch action, before switching, I have already loaded the video and prepare for switching. When you need to do so, you just need to stop and release the old mediaplayer and starting the new one.The key point for seamless switching is the sleep(), when the process is sleeping, actually the video is still going on, but give the system some time to prepare for the next one.
switch_button = (Button) findViewById(R.id.button_switch);
switch_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button switch_button = (Button) v;
VideoPlayer2 = new MediaPlayer();
try {
VideoPlayer2.setDataSource("rtsp://" + hostIP + ":1935/vod/Timer.mp4");
VideoPlayer2.prepare();
VideoPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
VideoPlayer.release();
VideoPlayer2.setDisplay(vidHolder);
VideoPlayer2.start();
}
});

Categories

Resources