Playing a video between two times in Android - android

I need for an android app the ability to play a video from a time t1 to an other time t2. Basically, this video is a kind of "sprite" in my app (it's an .mp4 video h264 baseline encoded).
So, I tried the following. A button "next" plays the video between t1 and t2. To check if the video has arrived to t2, I use an handler with a postDelayed call every 20 ms. If the video current position is bigger than t2, I stop the video. Otherwise, I check again in 20ms.
But it does not work. For a reason I don't understand, the video current position I read in postDelayed goes suddenly from a time to a few seconds later. On my screen, the video plays normally.
Some code:
// MainActivity
// setting up the video
vidView = (VideoView) findViewById(R.id.myVid);
vidView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/myvid"));
// wiring up Bt next
Button nextBt = (Button) findViewById(R.id.next);
nextBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playVideo();
}
});
And the playVideo method:
private void playVideo() {
// t1 and t2 are defined within MainActivity class
t1 = (int) (Math.random() * (80000));
t2 = t1 + (int) (Math.random() * (5000));
// we start at t1
vidView.seekTo(t1);
// We check video position in 20 ms
mHandler.postDelayed(new Runnable() {
public void run() {
int currentTime = vidView.getCurrentPosition();
Log.i("currentTime", String.valueOf(currentTime));
Log.i("T2", String.valueOf(t2));
// Ok, we can pause the video
if(currentTime > t2 - 20) {
vidView.pause();
}
// We check again in 20ms
else {
mHandler.postDelayed(this, 20);
}
}
}, 20);
vidView.start();
}
The logCat gives me (in this example, T1 = 47286 and T2 = 51478)
10-14 11:31:56.603: I/currentTime(3359): 47286 // first call to postDelayed, fine
10-14 11:31:56.603: I/T2(3359): 51478
10-14 11:31:56.623: I/currentTime(3359): 47286 // second call to postDelayed, still ok
10-14 11:31:56.623: I/T2(3359): 51478
10-14 11:31:56.653: I/currentTime(3359): 50000 // What? How the video current time can already be 3 seconds later?
10-14 11:31:56.653: I/T2(3359): 51478
Can you tell me what's wrong with my code?
Thanks!

Instead of constantly checking why don't you just set a handler to stop it at a specific later time? You don't need to check if it has "arrived" at T2 - you know when you want to stop it, so just schedule to stop it then.
Runnable stopVideoTask = new Runnable(){
#Override
public void run() {
vidView.pause();
}
};
int t2 = (int) (Math.random() * 5000);
vidView.seekTo(t1);
vidView.start();
mHandler.postDelayed(stopVideoTask, t2);
The video will stop after Math.random() * 5000 seconds of playing.

Related

Control the playback time of a video

I'm playing a video in ExoPlayer. Now I want to distribute a specific time for that video to the user so that the user chooses.
For example, I only want to display 30 seconds to 50 seconds.
In AndroidStudio and JAVA language.
Thanks for helping me
It's not related to ExoPlayer! You can handle it on your own.
So use this snippet to solve this problem:
At the same time with starting video:
videoview.seekTo(SPECIFIED_START_TIME);
Then run this handler:
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
if(videoview.getCurrentPosition() >= DESTINATION_TIME){ // After reaching destination time
videoview.seekTo(SPECIFIED_START_TIME);
videoview.pause(); // or stop();
}
}
};
handler.postDelayed(runnable, 0);

Pause Exoplayer every 100 ms, resume after 500 ms

I would like to pause video playback with Exoplayer every 100 ms, then resume playback after 500 ms. I have not found any examples.
[EDIT]
The following code does the job:
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
#Override
public void run()
{
if (!player.getPlayWhenReady()) {time += 500; player.setPlayWhenReady(true); h.postDelayed(this, 100);}
else {time += 100; player.setPlayWhenReady(false); h.postDelayed(this, 500);}
}
}, 100);
Just use Handlers or build a mechanism for the delays (100 and 500 ms) -
When you want to play use player.setPlayWhenReady(true); and player.setPlayWhenReady(false); for pausing.
You can also use the callback public void onStateChanged(boolean playWhenReady, int playbackState) for states changing coming when ExoPlayer out-of-the-box.

MediaPlayer doesn't start when CountDownTimer is finished

I have a CountDownTimer like this:
cuentaAtras = new CountDownTimer(mili, 1000) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
if (seconds <= TIEMPO3){
if (tic1.isPlaying()){
tic1.stop();
tic2.start();
}
} else if (seconds <= TIEMPO && seconds > TIEMPO2 + 10){
tic1.start();
}
}
public void onFinish() {
if (tic2.isPlaying())
{
tic2.stop();
}
ticExp.start();
tic1.reset();
tic2.reset();
ticExp.reset();
}
}.start();
Variables tic1, tic2 y ticExp type is MediaPlayer :
tic1 = MediaPlayer.create(getApplicationContext(), R.raw.tic1);
tic1.setLooping(true);
tic2 = MediaPlayer.create(getApplicationContext(), R.raw.tic3);
tic2.setLooping(true);
ticExp = MediaPlayer.create(getApplicationContext(), R.raw.exp);
When I execute onFinish method, the MediaPlayer ticExp should be played, but this doesn't occur.
I tested write ticExp.start() in method onTick and this works, so the file is not corrupted or something like this.
I don't have idea why in onTick method works, but in onFinish no.
(But the others sentences of code in onFinish are executed without problems)
Definitively, I want that ticExp will be played in onFinish method.
Sorry for my English. Thanks.
You are doing ticExp.reset(); after ticExp.start(); in onFinish(), That means you are actually starting and also resetting(making it idle). Just remove ticExp.reset(); in onFinish().
For the Complete State of MediaPlayer, you can refer this
And one more thing, Your else condition in onTick method will never execute.

Android play beep sound, every n milliseconds

I created an Android application that is like a metronome.
Actually I want to play a beep sound every n milliseconds.
I use MediaPlayer and timer for this.
My code is like this:
Soloution 1:
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
When I run this code, everything is OK. But after 15 times loop, line of Log works fine, but sound is muted. And occasionally every 15, or 20 Log, sound plays and stop.
Solution 2:
I move this line:
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
out of TimerTask (like this):
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
This code is OK too until 500 ms for repeating. when period time decrease to 400 or 300 ms, every 2 Log, one sound beeps.
How to repair this code that work fine.
SoundPool is better for playing short sounds that are loaded from memory. I have also had problems implementing MediaPlayer, SoundPool has just been a much easier and lower latency experience.
If you set .setMaxStreams() to a number above 1 you shouldn't get any muted sounds. Try and experiment.

Increase the sound in each 5 seconds through Media Player

I want to increase the sound by one level up in each and every 5 seconds meanwhile the sound clip plays.Below is my code:-
MediaPlayer player;
player=MediaPlayer.create(this, R.raw.alarm);
player.setLooping(true);
Is there any method in which i can track each and every 5 minutes when over.How can i achieve this?
I think you can achieve this with java TimerTask, this is an example code, not tested but should work with no big modifications :
Basically you start a task every 5 seconds, in the run() function of the TimerTask you level up your sound level, and when you reach the maxSoundLevel you call cancel() to stop the task.
//Put this in global
int REFRESH_INTERVAL = 5 * 1000; //5 seconds
int maxSoundLevel = 10; // Number of loop to get to max level
int curSoundLevel = 0; //Start at 0 volume level
//Put this after you started the sound
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, REFRESH_INTERVAL);
//Put this after your method
private class MyTimerTask extends TimerTask{
public void run() {
if(curSoundLevel < maxSoundLevel)
{
float logLevel = (float)(Math.log(maxSoundLevel-curSoundLevel)/Math.log(maxSoundLevel));
yourMediaPlayer.setVolume(1-logLevel);
curSoundLevel ++;
}
else {
this.cancel();
}
}
}
If you have more questions feel free to ask me.

Categories

Resources