Android: Play next cued video in onVideoEnd() - java

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.

Related

How to get current duration of video in exo player?

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

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 do I implement interstitial ads to show after X amount of times a player switches to the gameover screens?

So, I am new to AdMob and I am trying to figure out how to display an interstitial ad after the player dies and goes to the game over screen X amount of times. I have my AdMob set up in my AndroidLauncher class, however, my other classes do not have the ad variables. this is what my AndroidLauncher class looks like currently. If it helps, my game is set up with game states, 0 is before game starts, 1 is currently playing, and 2 and 3 are both game over states that send the player to the game over screen.
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
ad = new InterstitialAd(this);
ad.setAdUnitId("ca-app-pub-2188258702xxxxxxxxxxx");
ad.loadAd(new AdRequest.Builder().build());
ad.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
if (ad.isLoaded()) {
ad.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
}
}); ```
I am not sure if this is the best way but I can explain my way of doing it. This may help.
1) Create an Interface for Adservice
public interface AdService {
void showOrLoadInterstital();
void showBanner();
void hideBanner();
void showRewardedAd();
boolean hasVideoLoaded();
boolean checkAdWatched();
}
2) Implement this interface from your AndroidLauncher class and have a constructor of your base Game Class with this ad interface. From your androidlauncher class, refer to this constructor. Also generate the interface classes. ( I am only posting relevant parts).
public class AndroidLauncher extends AndroidApplication implements AdService {
#Override
protected void onCreate(Bundle savedInstanceState) {
// rest of the code here
setUpInterstitial();
View gameView = initializeForView(new MainGameClass(this), config);
}
private void setUpInterstitial() {
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
loadIntersitialAd();
}
private void loadIntersitialAd() {
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest interstitialRequest = builder.build();
interstitialAd.loadAd(interstitialRequest);
}
#Override
public void showOrLoadInterstital() {
runOnUiThread(new Runnable() {
public void run() {
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
loadIntersitialAd();
}
#Override
public void onAdFailedToLoad(int i) {
loadIntersitialAd();
}
});
interstitialAd.show();
adWatched=false;
}
});
}
}
3) Call the ad when you want to show it.
if (MainGameClass.adService != null) {
RunnableAction playWooshAction = Actions.run(new Runnable() {
#Override
public void run() {
MainGameClass.adService.showOrLoadInterstital();
}
});
playWooshAction.run();
}
4) For showing ad after X amount of time, just set a timer.

how can i call a method from another class in libgdx

I want to call the method walo() from render() in classMyGdxGame:
public class MyGdxGame extends ApplicationAdapter{
public void render() {
walo();
}
}
public class AndroidLauncher extends AndroidApplication {
AndroidLauncher android =new AndroidLauncher();
public void walo() {
Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
Toast.LENGTH_LONG).show();
}
}
Create interface in core, implement this interface in AndroidLauncher and send it to game.
So you can call method or pass data to render.
Interface:
public interface SomeInterface {
public void walo();
}
AndroidLauncher:
public class AndroidLauncher implements SomeInterface{
#Override
protected void onCreate() {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGdxGame(this), config);
}
public void walo() {
Toast.makeText(getApplicationContext(), "You Don't Have Enough Money",
Toast.LENGTH_LONG).show();
}
}
In game class
public MyGdxgame(SomeInterface myinterface) {
this.myinterface=myinterface;
}
public render() {
myinterface.walo()
}
Here's example (my google play service interface) and a link to open source data of my libgdx game.
public interface PlayServices
{
public void signIn();
public void signOut();
public void rateGame();
public void unlockAchievement(String str);
public void submitScore(int highScore);
public void submitLevel(int highLevel);
public void showAchievement();
public void showScore();
public void showLevel();
public boolean isSignedIn();
public void showBannerAd();
public void hideBannerAd();
public void showInterstitialAd (Runnable then);
public void showRewardedVideo();
public boolean isRewardEarned();
}
You can see ads and video rewards implemented like this.
Github Connect Game

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