How to get current duration of video in exo player? - java

I'm using the ExoPlayer library for playing some videos from the online data sources. Everything is working fine.
Now I want to get current video duration/position on every second. I can't get any option in the default event listener. Anyone have any reference, I've searched about it but can't get anything. Please help me to get this.
Here's the event listener functions I'm getting,
#Override
public void onRepeatModeChanged(int repeatMode) {
}
#Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
#Override
public void onPositionDiscontinuity(int reason) {
}
#Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
#Override
public void onSeekProcessed() {
}
#Override
public void onPlaybackSuppressionReasonChanged(int playbackSuppressionReason) {
}
#Override
public void onIsPlayingChanged(boolean isPlaying) {
}

If you want to get current video position at every second , then run handler for every second and get the current position in seconds as shown below.
long videoWatchedTime =0;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Do your work
videoWatchedTime= player.getCurrentPosition()/1000;
}
},1000);

Related

How to run RecognitionListener at the background of the app?

everyone! I am developing an application for voice recognition, and the application is able to recognize the speech right now! However, I need to run this voice recognition code at the background of the app, it has to listen to the commands all the time. For my app, I wrote Handler().postDelayed function, it calculates the time when the user lands a new activity and it starts listening by delaying for 5 seconds. My problem is that it just listens 2-3 seconds, and it is not able to recognize and listen again. How can I run voice recognition at the background of the app when the app is running?
speechRecognizer1.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int error) {
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> matches1 =bundle.getStringArrayList(speechRecognizer1.RESULTS_RECOGNITION);
String string="";
if(matches1!=null) {
string = matches1.get(0);
textView3.setText(string);
Speak();
}
}
#Override
public void onPartialResults(Bundle bundle) {
}
#Override
public void onEvent(int eventType, Bundle params) {
}
});
mTTS1 = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
int result= mTTS1.setLanguage(Locale.ENGLISH);
if(result== TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS","Language Not Supported");
}
}
}
});
new Handler().postDelayed(new Runnable(){
public void run(){
speechRecognizer1.startListening(intentRecognizer1);
}
}, 5000);
Ok for this you need an android component called Service
we have three types of services in android :
1.Forground:
a foreground service do works that is noticeable to the user for example a music app with a notification about the song that is playing.
2.Background:
this service is not noticeable to the user
3.Bound:
and bound service which offers the user a client-server interaction
for your case you can use a foreground service.
to decide what is better and how to implement it or deciding between a thread and a service I recommend reading this document :
https://developer.android.com/guide/components/services

How to get play and stop event callback from chromecast android

I want to get callback event and perform some function when chromecast audio change there playback mode(play/stop)
How can I get the play/stop event to the application so I will do my work on that event.
Please check below logic I have implemented.
private void setupCastListener() {
mSessionManagerListener = new SessionManagerListener<CastSession>() {
#Override
public void onSessionEnded(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionResumed(CastSession session, boolean wasSuspended) {
onApplicationConnected(session);
}
#Override
public void onSessionResumeFailed(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionStarted(CastSession session, String sessionId) {
onApplicationConnected(session);
}
#Override
public void onSessionStartFailed(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionStarting(CastSession session) {
}
#Override
public void onSessionEnding(CastSession session) {
}
#Override
public void onSessionResuming(CastSession session, String sessionId) {
if(mCastSession!=null && isChromeCastConnected){
try {
if (session.isMute()) {
mStopPlayButton.setImageResource(R.drawable.ic_play);
isChromeCastPlay = false;
//mCastSession.setMute(!mCastSession.isMute());
} else {
mStopPlayButton.setImageResource(R.drawable.ic_stop);
isChromeCastPlay = true;
//mCastSession.setMute(!mCastSession.isMute());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onSessionSuspended(CastSession session, int reason) {
}
};
}
Please let me know. thanks
You need to read this documentation, and focus on RemoteMediaClient and the Listener interface there. The callback onStatusUpdated() will be called when there is a change in the playback status. Tutorials available on the first link above is very informative, so make sure you read about things there.
Finally I found the remote media play and pause mode call back by below MediaControlIntent.
Remote Playback Routes
mMediaRouter = MediaRouter.getInstance(this);
mSelector = new MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();

Is it possible to use multiple methods depending on the status in onResume methods android

I have 3 types status of so that my app will update these status in a TextView depending on the status
So here they are working fine when I run the application
But when minimise and maximise the app these status are not updating I am confused I what way I can show them in onResume()
Hence my 3 statements are in void I cant use returns
these are my status
#Override
public void onVLoading() {
mtc.post(new Runnable()
{
#Override
public void run() {
mtc.setText("Buffering");
}
});
}
#Override
public void onVStarted() {
mtc.post(new Runnable()
{
#Override
public void run() {
mtc.setText("Playing");
}
});
}
#Override
public void onVStopped() {
mtc.post(new Runnable()
{
#Override
public void run() {
mtc.setText("Passed");
}
});
}
So I want to show them at onResume in android
this is my onResume() method
#Override
protected void onResume() {
super.onResume();
mVMG.connect();
/*onVLoading()
onVStarted()
onVStopped()*/
}
Actually I want to show show these text-view status at onResume()
is there any suggestion on this kind please help..
Just create a function for showing the status.
private void showStatus(){
mtc.setText(statusText);
}
and update the statusText variable. Make statusText variable as global static variable.
#Override
public void onVLoading() {
mtc.post(new Runnable()
{
#Override
public void run() {
statusText = "Buffering";
}
});
}
#Override
public void onVStarted() {
mtc.post(new Runnable()
{
#Override
public void run() {
statusText ="Playing";
}
});
}
#Override
public void onVStopped() {
mtc.post(new Runnable()
{
#Override
public void run() {
statusText = "Passed";
}
});
}
#Override
protected void onResume() {
super.onResume();
showStatus();
mVMG.connect();
}

Android: Play next cued video in onVideoEnd()

I have a Youtube player within my app. I have set a setPlayerStateChangeListener for this player, and on the event of the video ending (onVideoEnd()) I wish to play the next cued video.
This is what I have so far...
YouTubeFragment
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean restored) {
...
player.setPlayerStateChangeListener(myPlayerStateChangeListener);
...
}
MyPlayerStateChangeListener
package x;
import android.util.Log;
import com.google.android.youtube.player.YouTubePlayer;
public final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
#Override
public void onAdStarted() {
}
#Override
public void onError(
com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
Log.d("onVideoEnded()", "Video has ended");
}
#Override
public void onVideoStarted() {
}
}
When the video ends, the log message within onVideoEnded() successfully displays. What I am struggling to get my head around is how I can run hasNext(), play() etc on my youtube player from within this PlayerStateChangeListener.
Actually you can use the following :
private static YouTubePlayer player;
In onInitializationSuccess() set this variable.
this.player = player;
Now in the callback onVideoEnd() use the static version of the player to perform the actions that you need like cueing a new video.

how to detect head movement via glass gdk api?

I write an app for google glass platform using the gdk.
how can I detect and react to head movement?
I don't find the proper listener not the Gesture enum (e.g. Gesture.SWIPE_UP)
gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
//do something
}
return true;
} else if (gesture == Gesture.SWIPE_UP) {
gestureDetector.setScrollListener(new ScrollListener() {
#Override
public boolean onScroll(float arg0, float arg1, float arg2) {
// TODO Auto-generated method stub
return false;
}
})
Take a look here: https://developers.google.com/glass/develop/gdk/location-sensors . It should help you out with the accelerator and other sensors that are accessible via the GDK. The code you have copied is for the touch pad, not for head movement.
take a look at this repo:
https://github.com/thorikawa/glass-head-gesture-detector
Usage:
public class MainActivity extends Activity implements OnHeadGestureListener {
private HeadGestureDetector mHeadGestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
…
mHeadGestureDetector = new HeadGestureDetector(this);
mHeadGestureDetector.setOnHeadGestureListener(this);
…
}
#Override
protected void onResume() {
…
mHeadGestureDetector.start();
}
#Override
protected void onPause() {
…
mHeadGestureDetector.stop();
}
#Override
public void onNod() {
// Do something
}
#Override
public void onShakeToLeft() {
// Do something
}
#Override
public void onShakeToRight() {
// Do something
}
}

Categories