Pause Exoplayer every 100 ms, resume after 500 ms - android

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.

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);

Android - Youtube Player Stop an video at specified time

I'm trying to play an youtube video using Youtube Video Player in my android app from given start_time and end_time.
I used player.loadVideo("_wcs7ixyDbY", 12000), so that my video starts playing after 12 seconds. But I want to end my video at 20 seconds.
I used
player.loadVideo(videoID, 12000);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(player.getCurrentTimeMillis() <= 20000) {
handler.postDelayed(this, 1000);
} else {
handler.removeCallbacks(this);
player.pause();
}
}
}, 1000);
This option pause my video after 20 seconds.
How to end my video after 20 seconds (not pause)
Thank You
The Android YouTube API provides a method to stop / release the video also:
abstract void release()
Stop any loading or video playback and release any system resources used by this YouTubePlayer.
https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer

android - how to delay an audio file so it starts after a few seconds

I've been wondering if there's a way with either mp or soundpool to delay a sound and make it start exactly after 10 seconds (I have a countdown and want to add a sound effect saying 3,2,1 go! after 10 seconds.)
maybe with a handler?
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
playSound();
}
}, 10000);

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.

how to find the start and end time of the music application in android?

I am developing an application which should capture the start and end time of Audio(FM /Music). I surfed a lot and got to know this can be achieved through audioFocusListener. I have implemented the listener but i am not getting the values properly. i.e This is not giving proper value, when the audio is gain the focus and lost the focus.
OnAudioFocusChangeListener is not executed when the audio is played and paused?
Is there any alternative way to implement the same?
public class AudioManagerExample extends Activity implements OnAudioFocusChangeListener{
AudioReceiver adreceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN)
}
#Override
public void onAudioFocusChange(int focusChange) {
Log.d("AudioManager", "Inside on audio focus change");
if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
Log.d("AudioManager", "audio focus gained");
if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
Log.d("AudioManager", "audio focus lossed");
/* Here i am always getting value -1 */
}
}
Thanks in advance
i am pasting some code here so you can get idea, do as per your requirement..
hope this will help you,
public void updateProgressBar()
{
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable()
{
public void run()
{
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};

Categories

Resources