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.
Related
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.
Hi I wanna make a application by eclipse and I need to pause a Clip in a special time for example I wanna pause a clip in 2:30 and i don't know how can I do that please help me
Add following
videoView.postDelayed(new Runnable() {
#Override
public void run() {
videoView.pause();
}
}, 150000);
In my program when user clicks on imagepreview video starts running.Video is played in same actvity in which images are.When video is played images are made invisible.I want that when video stop it should disappear and images should be visible.I tried using isPlaying() method but doesn't work out as expected.If I put isPlaying() method inside onClick() then the action inside isPlaying() is performed even before the video is started and if i place it ouside setOnClickListener() then this method doesn't execute when video is stopped.Plzz help me with code
imgPreview.setOnClickListener(new View.OnClickListener() { //clicking on first imageView
#Override
public void onClick(View v) {
imgPreview.setVisibility(View.GONE);
imgPreview2.setVisibility(View.GONE);
videoPreview2.setVisibility(View.VISIBLE);
videoPreview2.setVideoPath(fileUri.getPath());
videoPreview2.start();
}
});
if(videoPreview2.isPlaying()==false){
imgPreview.setVisibility(View.VISIBLE);
imgPreview2.setVisibility(View.VISIBLE);
videoPreview2.setVisibility(View.GONE);
}
Have you tried videoview's setcompletion listener. See the docs.
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//make image visible here
}
});
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.
I have an application with a VideoView, in order to make the video play on a loop I use an onCompletionListner to call setVideoPath() again, like this:
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//I have a log statment here, so I can see that it is making it this far.
mp.reset(); // <--- I added this recently to try to fix the problem
mVideoView.setVideoPath(file.getAbsolutePath());
}
});
This setup works well on all devices I've come across so far, I never had any trouble with it not repeating.
However the Motorola Xoom that I am testing on was recently upgraded to ICS. Now that it is on ICS this will work for a while and loop the video. But eventually (I've added a counter and some Logs, there does not appear to be any pattern to how many times it successfully loops before stopping) it will quit looping and just sit on a freeze frame of the first frame in the movie.
Does anyone know what could cause this not to loop properly any more? OR does anyone know of another way to get a VideoView to loop properly that does work under ICS still?
If you have only one video to play you can setLooping(true) in your on prepared listener.
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.setLooping(true);
}
});
and you're done.
So far this:
mp.reset();
inside the onComplete callback seems to fix it. Would be very interested if anyone can explain what is going on with it.