android - How to do auto-rotation for video playback for an AR app in Unity and Vuforia written in C#? -


i working on unity version of video playback sample. have added video playback event handler script in inspecter , checked m full screen mode. default orientation landscape right. trying achieve auto rotation on full screen playback when turn device, changes orientation , on. have tried suggestions regarding problem, such tinkering player settings etc. didn't work.

my second problem button in navigation bar. when video playing, have press button twice go camera. want kill full screen playback press button. how this.

i have tried tinkering following code (videoplaybackuieventhandler.cs):

/*==============================================================================   * copyright (c) 2012-2014 qualcomm connected experiences, inc. rights reserved.   * ==============================================================================*/  using unityengine; using system.collections; using vuforia;  /// <summary> /// ui event handler class handles events generated user-tap actions /// on ui options menu /// </summary> public class videoplaybackuieventhandler : isampleappuieventhandler {      #region public_member_variables     public override event system.action closeview;     public override event system.action gotoaboutpage;     public bool mfullscreenmode;     #endregion public_member_variables      #region private_member_variables     private static bool sextendedtrackingisenabled;     private videoplaybackuiview mview;     private bool mcamerafacingfront;     #endregion private_member_variables      #region public_member_properties     public videoplaybackuiview view     {                 {             if (mview == null)             {                 mview = new videoplaybackuiview();                 mview.loadview();             }             return mview;         }     }      /// <summary>     /// currently, there no mechanism query sdk know whether or not extended tracking enabled/disabled.     /// therefore, needs handled @ app layer.     /// </value>     public static bool extendedtrackingisenabled     {                 {             return sextendedtrackingisenabled;         }     }     #endregion public_member_properties      #region public_methods     public override void updateview(bool tf)     {         this.view.updateui(tf);     }      public override void bind()     {         this.view.mextendedtracking.tappedon += ontappedtoturnontraking;         this.view.mcameraflashsettings.tappedon += ontappedtoturnonflash;         this.view.mautofocussetting.tappedon += ontappedtoturnonautofocus;         this.view.mcamerafacing.tappedonoption += ontappedtoturncamerafacing;         this.view.mclosebutton.tappedon += ontappedonclosebutton;         this.view.maboutlabel.tappedon += ontappedonaboutbutton;         this.view.mplayfullscreesettings.tappedon += ontappedonfullscreenbutton;          // register qcar started callback          qcarabstractbehaviour qcarbehaviour = (qcarabstractbehaviour)findobjectoftype(typeof(qcarabstractbehaviour));          if (qcarbehaviour)          {              qcarbehaviour.registerqcarstartedcallback(enablecontinuousautofocus);              qcarbehaviour.registeronpausecallback(onpause);          }     }      public override void unbind()     {         this.view.mextendedtracking.tappedon -= ontappedtoturnontraking;         this.view.mcameraflashsettings.tappedon -= ontappedtoturnonflash;         this.view.mautofocussetting.tappedon -= ontappedtoturnonautofocus;         this.view.mcamerafacing.tappedonoption -= ontappedtoturncamerafacing;         this.view.mclosebutton.tappedon -= ontappedonclosebutton;         this.view.maboutlabel.tappedon -= ontappedonaboutbutton;         this.view.mplayfullscreesettings.tappedon -= ontappedonfullscreenbutton;          // unregister qcar started callback          qcarabstractbehaviour qcarbehaviour = (qcarabstractbehaviour)findobjectoftype(typeof(qcarabstractbehaviour));          if (qcarbehaviour)          {              qcarbehaviour.unregisterqcarstartedcallback(enablecontinuousautofocus);              qcarbehaviour.unregisteronpausecallback(onpause);          }          this.view.unloadview();     }      //singletap gestures captured appmanager , calls method taptofocus     public override void triggerautofocus()     {         startcoroutine(triggerautofocusandenablecontinuousfocusifset());     }      #endregion public_methods      #region private_methods      /// <summary>     /// activating trigger autofocus mode unsets continuous focus mode (if enabled ui options menu)     /// so, wait second , turn continuous focus on (if options menu shows enabled)     /// </returns>     private ienumerator triggerautofocusandenablecontinuousfocusifset()     {         //triggers single autofocus operation          if (cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_triggerauto))         {             this.view.focusmode = cameradevice.focusmode.focus_mode_triggerauto;         }          yield return new waitforseconds(1.0f);          //continuous focus mode turned on if enabled options menu         if (this.view.mautofocussetting.isenabled)         {             if (cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_continuousauto))             {                 this.view.focusmode = cameradevice.focusmode.focus_mode_continuousauto;             }         }          debug.log(this.view.focusmode);      }        private void onpause(bool pause)      {          if (!pause && this.view.mautofocussetting.isenabled)          {              // set continous autofocus              cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_continuousauto);          }            //on hitting home button, app tends turn off flash          //so, setting ui reflect          this.view.mcameraflashsettings.enable(pause);      }      private void ontappedonaboutbutton(bool tf)     {         if (this.gotoaboutpage != null)         {             this.gotoaboutpage();         }     }      //we want autofocus enabled when app starts     private void enablecontinuousautofocus()     {         if (cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_continuousauto))         {             this.view.focusmode = cameradevice.focusmode.focus_mode_continuousauto;             this.view.mautofocussetting.enable(true);         }     }      private void ontappedtoturnontraking(bool tf)     {         if (!extendedtracking(tf))         {             this.view.mextendedtracking.enable(false);             videoplaybackuieventhandler.sextendedtrackingisenabled = false;         }         else         {             this.view.mextendedtracking.enable(tf);             videoplaybackuieventhandler.sextendedtrackingisenabled = tf;         }         ontappedtoclose();     }      private void ontappedtoturnonflash(bool tf)     {         if (tf)         {             if (!cameradevice.instance.setflashtorchmode(true) || mcamerafacingfront)             {                 this.view.mcameraflashsettings.enable(false);             }         }         else         {             cameradevice.instance.setflashtorchmode(false);         }          ontappedtoclose();     }      /// <summary>     /// if video playing on texture, enabling bring video full-screen     /// otherwise, video play on fullscreen next time user taps on texture.     /// </summary>     /// <param name="tf"></param>     private void ontappedonfullscreenbutton(bool tf)     {         mfullscreenmode = tf;         if (tf)         {             videoplaybackbehaviour video = pickvideo();             if (video != null)             {                 if (video.videoplayer.isplayablefullscreen())                 {                     //on android, use unity's built in player, unity application pauses before going fullscreen.                      //so have handle orientation within unity.  #if unity_android                     screen.orientation = screenorientation.portrait; #endif                     // pause video if playing                     video.videoplayer.pause();                      // seek video beginning();                     video.videoplayer.seekto(0.0f);                      // display busy icon                     video.showbusyicon();                      // play video full screen                     startcoroutine ( playvideo.playfullscreenvideoatendofframe(video) );                      //flash turns off automatically on fullscreen videoplayback mode, need update ui accordingly                     this.view.mcameraflashsettings.enable(false);                 }             }         }          ontappedtoclose();     }      private videoplaybackbehaviour pickvideo()     {         videoplaybackbehaviour[] behaviours = gameobject.findobjectsoftype(typeof(videoplaybackbehaviour)) videoplaybackbehaviour[];         videoplaybackbehaviour video = null;         foreach (videoplaybackbehaviour bhvr in behaviours)         {             if (bhvr.currentstate == videoplayerhelper.mediastate.playing)             {                 video = bhvr;             }         }          return video;     }      private void ontappedtoturnonautofocus(bool tf)     {         if (tf)         {             if (cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_continuousauto))             {                 this.view.focusmode = cameradevice.focusmode.focus_mode_continuousauto;             }             else             {                 this.view.mautofocussetting.enable(false);             }         }         else         {             if (cameradevice.instance.setfocusmode(cameradevice.focusmode.focus_mode_normal))             {                 this.view.focusmode = cameradevice.focusmode.focus_mode_normal;             }         }          ontappedtoclose();     }      private void ontappedtoturncamerafacing(int val)     {         if (val == 0)         {             //internally, flash turned off everytime tries switch front camera             //so updating ui options reflect that.             this.view.mcameraflashsettings.enable(false);              if (changecameradirection(cameradevice.cameradirection.camera_front))             {                 mcamerafacingfront = true;             }             else             {                 changecameradirection(cameradevice.cameradirection.camera_back);                 mcamerafacingfront = false;                 this.view.mcamerafacing.enableindex(1);             }         }         else         {             changecameradirection(cameradevice.cameradirection.camera_back);             mcamerafacingfront = false;         }          ontappedtoclose();     }      private bool stoprunningobjecttracker()     {         bool needsobjecttrackerrestart = false;          objecttracker objecttracker = trackermanager.instance.gettracker<objecttracker>();         if (objecttracker != null)         {             if (objecttracker.isactive)             {                 objecttracker.stop();                 needsobjecttrackerrestart = true;             }         }         return needsobjecttrackerrestart;     }      private bool restartrunningobjecttracker()     {         bool hasobjecttrackerrestarted = false;          objecttracker objecttracker = trackermanager.instance.gettracker<objecttracker>();         if (objecttracker != null)         {             if (!objecttracker.isactive)             {                 hasobjecttrackerrestarted = objecttracker.start();             }         }         return hasobjecttrackerrestarted;     }      private void resetcamerafacingtoback()     {         bool needsobjecttrackerrestart = stoprunningobjecttracker();          cameradevice.instance.stop();         cameradevice.instance.init(cameradevice.cameradirection.camera_back);         cameradevice.instance.start();         mcamerafacingfront = false;          if (needsobjecttrackerrestart)             restartrunningobjecttracker();     }      private bool changecameradirection(cameradevice.cameradirection direction)     {         bool directionsupported = false;          bool needsobjecttrackerrestart = stoprunningobjecttracker();          cameradevice.instance.stop();         cameradevice.instance.deinit();         if (cameradevice.instance.init(direction))         {             directionsupported = true;         }         cameradevice.instance.start();          if (needsobjecttrackerrestart)             restartrunningobjecttracker();          return directionsupported;     }      private void ontappedtoclose()     {         if (this.closeview != null)         {             this.closeview();         }     }      private void ontappedonclosebutton()     {         ontappedtoclose();     }      /// <summary>     /// method turns extended tracking on or off available targets.     /// extended tracking allows track targets when not in view.     /// returns true of extended tracking supported; false otherwise     /// </summary>     private bool extendedtracking(bool tf)     {         // statemanager gives access available trackablebehavours         statemanager statemanager = trackermanager.instance.getstatemanager();         // iterate on trackablebehaviours start or stop extended tracking targets represent.          bool extendedtrackingstatechanged = true;         foreach (var behaviour in statemanager.gettrackablebehaviours())         {             var imagebehaviour = behaviour imagetargetbehaviour;             if (imagebehaviour != null)             {                 if (tf)                 {                     //only if extended tracking supported                     if (!imagebehaviour.imagetarget.startextendedtracking())                     {                         extendedtrackingstatechanged = false;                     }                 }                 else                 {                     if (!imagebehaviour.imagetarget.stopextendedtracking())                     {                         extendedtrackingstatechanged = false;                     }                 }             }         }          if (!extendedtrackingstatechanged)         {             debug.logwarning("extended tracking failed!");         }          return extendedtrackingstatechanged;     }     #endregion private_methods } 

and code (playvideo.cs):

/*==============================================================================   * copyright (c) 2012-2014 qualcomm connected experiences, inc. rights reserved.   * ==============================================================================*/  using unityengine; using system.collections; using vuforia;  /// <summary> /// demonstrates how play video on texture , full-screen mode. /// single tapping on texture play video on texture (if 'play fullscreen' mode in uimenu turned off) /// or play full screen (if option enabled in uimenu) /// @ time during video playback, can brought full-screen enabling options uimenu. /// </summary> public class playvideo : monobehaviour {      private bool mvideoisplaying;     private videoplaybackbehaviour currentvideo;      #region unity_monobehaviour_methods      void start()     {         inputcontroller.singletapped += handlesingletap;         inputcontroller.doubletapped += handledoubletap;     }      void onapplicationpause(bool tf)     {         //when video finishes playing on fullscreen mode, unity application unpauses , that's when need switch potrait         //in order display ui menu options #if unity_android         if(!tf) {             screen.orientation = screenorientation.portrait;         } #endif     }      #endregion unity_monobehaviour_methods      #region private_methods      /// <summary>     /// in case device in other mode @ time user double taps bring ui menu, force go potrait     /// because ui menu supports potrait now.     /// </summary>     private void handledoubletap()     {         if (screen.orientation != screenorientation.portrait)         {             screen.orientation = screenorientation.portrait;         }     }     /// <summary>     /// handle single tap event     /// </summary>     private void handlesingletap()     {          if (qcarruntimeutilities.isplaymode())         {             if (pickvideo(input.mouseposition) != null)                 debug.logwarning("playing videos not supported in play mode.");         }          // find out video tapped, if         currentvideo = pickvideo(input.mouseposition);          if (currentvideo != null)         {             if (isfullscreenmodeenabled())             {                 if (currentvideo.videoplayer.isplayablefullscreen())                 {                     //on android, use unity's built in player, unity application pauses before going fullscreen.                      //so have handle orientation within unity.  #if unity_android                     screen.autorotatetoportrait = true;                     screen.autorotatetoportraitupsidedown = true;                     screen.orientation = screenorientation.autorotation;  #endif                     // pause video if playing                     currentvideo.videoplayer.pause();                      // seek video beginning();                     currentvideo.videoplayer.seekto(0.0f);                      // display busy icon                     currentvideo.showbusyicon();                      // play video full screen                     startcoroutine( playfullscreenvideoatendofframe(currentvideo) );                      updateflashsettingsinuiview();                 }             }             else             {                 if (currentvideo.videoplayer.isplayableontexture())                 {                     // video playable on texture, toggle playing/paused                      videoplayerhelper.mediastate state = currentvideo.videoplayer.getstatus();                     if (state == videoplayerhelper.mediastate.paused ||                         state == videoplayerhelper.mediastate.ready ||                         state == videoplayerhelper.mediastate.stopped)                     {                         // pause other videos before playing 1                         pauseothervideos(currentvideo);                          // play video on texture left off                         currentvideo.videoplayer.play(false, currentvideo.videoplayer.getcurrentposition());                     }                     else if (state == videoplayerhelper.mediastate.reached_end)                     {                         // pause other videos before playing 1                         pauseothervideos(currentvideo);                          // play video beginning                         currentvideo.videoplayer.play(false, 0);                     }                     else if (state == videoplayerhelper.mediastate.playing)                     {                         // video playing, pause                         currentvideo.videoplayer.pause();                     }                 }                 else                 {                     // display busy icon                     currentvideo.showbusyicon();                      // video cannot played on texture, play full screen                     startcoroutine( playfullscreenvideoatendofframe(currentvideo) );                 }             }         }     }      public static ienumerator playfullscreenvideoatendofframe(videoplaybackbehaviour video)     {         screen.orientation = screenorientation.autorotation;         screen.autorotatetoportrait = true;         screen.autorotatetoportraitupsidedown = true;         screen.autorotatetolandscapeleft = true;         screen.autorotatetolandscaperight = true;          yield return new waitforendofframe ();          // wait bit allow screenorientation.autorotation become effective         yield return new waitforseconds (0.3f);          video.videoplayer.play(true, 0);     }      //flash turns off automatically on fullscreen videoplayback mode, need update ui accordingly     private void updateflashsettingsinuiview()     {         videoplaybackuieventhandler handler = gameobject.findobjectoftype(typeof(videoplaybackuieventhandler)) videoplaybackuieventhandler;         if (handler != null)         {             handler.view.mcameraflashsettings.enable(false);         }     }      /// <summary>     /// checks see if 'play fullscreen' mode enabled/disabled in ui menu     /// </summary>     /// <returns></returns>     private bool isfullscreenmodeenabled()     {         videoplaybackuieventhandler handler = findobjectoftype(typeof(videoplaybackuieventhandler)) videoplaybackuieventhandler;         if (handler != null)         {             return handler.mfullscreenmode;         }          return false;     }      /// <summary>     /// find video object under screen point     /// </summary>     private videoplaybackbehaviour pickvideo(vector3 screenpoint)     {         videoplaybackbehaviour[] videos = (videoplaybackbehaviour[])                 findobjectsoftype(typeof(videoplaybackbehaviour));          gameobject go = qcarmanager.instance.arcameratransform.gameobject;         camera[] cam = go.getcomponentsinchildren<camera> ();         ray ray = cam[0].screenpointtoray(screenpoint);          raycasthit hit = new raycasthit();          foreach (videoplaybackbehaviour video in videos)         {             if (video.getcomponent<collider>().raycast(ray, out hit, 10000))             {                 return video;             }         }          return null;     }      /// <summary>     /// pause videos except 1     /// </summary>     private void pauseothervideos(videoplaybackbehaviour currentvideo)     {         videoplaybackbehaviour[] videos = (videoplaybackbehaviour[])                 findobjectsoftype(typeof(videoplaybackbehaviour));          foreach (videoplaybackbehaviour video in videos)         {             if (video != currentvideo)             {                 if (video.currentstate == videoplayerhelper.mediastate.playing)                 {                     video.videoplayer.pause();                 }             }         }     }       #endregion // private_methods  } 

in both of these codes, part you'll see #if unity_android in handlesingletap method , ontappedonfullscreen method, tried playing around screen.orientation in unity scripting. specific, tried following achieve auto-rotation didn't help:

screen.autorotatetoportrait = true;  screen.autorotatetoportraitupsidedown = true;  screen.autorotatetolandscapeleft = true;  screen.autorotatetolandscaperight = true;  screen.orientation = screenorientation.autorotation;  

when did 1 orientation, did in code, instance: screen.orientation = screenorientation.landscaperight, orientation changed. not wanted.

any help/tips appeciated!

thank you.

to address first problem, suggest have 2 separate orientation layouts landscape , portrait orientations. when that's done use script detect whether device landscape or portrait , accordingly setactive layout.

here's how sample code like:

using unityengine; using system.collections;   public class mmorient : monobehaviour {       public gameobject p;     public gameobject l;     // use initialization     void start () {         if (input.deviceorientation == deviceorientation.landscapeleft ||             input.deviceorientation == deviceorientation.landscaperight ) {              l.setactive(true);             p.setactive(false);          }         if (input.deviceorientation == deviceorientation.portrait ||             input.deviceorientation == deviceorientation.portraitupsidedown ) {              p.setactive(true);             l.setactive(false);          }     }      // update called once per frame     void update () {         if (input.deviceorientation == deviceorientation.landscapeleft ||             input.deviceorientation == deviceorientation.landscaperight ) {              l.setactive(true);             p.setactive(false);          }         if (input.deviceorientation == deviceorientation.portrait ||             input.deviceorientation == deviceorientation.portraitupsidedown ) {              p.setactive(true);             l.setactive(false);          }     } } 

to smoothen things out between scenes need make sure object assign next script not destroyed use:

void awake() {         dontdestroyonload(this.gameobject);     } 

and gameobject can go on use script contains 2 methods rest:

public class orientationmanger : monobehaviour {     private orientationtype currentorientation; ///////////////method 1 (enum)     int orientationtype = 1; ///////////////method 2 (or use integer 1,2)      //makes sure there once instance running     void awake ()     {         // singleton         if (findobjectsoftype (typeof(orientationmanger)).length > 1) {             debug.log ("orientationmanger exist");             destroyimmediate (gameobject); //destroy gameobject because exist         } else {             dontdestroyonload (this.gameobject); //wont destroy when loading level/scene         }     }      ///////////////method 1 set (enum)     public void setorientation (orientationtype orienttype)     {         currentorientation = orienttype;     }      ///////////////method 1 (enum)     public orientationtype getorientation ()     {         return currentorientation;     }      ///////////////method 2 set (or use integer 1,2)     public void setorientation2 (int orienttype)     {         if (orienttype == 1) {             orientationtype = orienttype;         } else if (orienttype == 2) {             orientationtype = orienttype;         }     }      ///////////////method 2 (or use integer 1,2)     public int getorientation2 ()     {         if (orientationtype == 1 || orientationtype == 2) {             return orientationtype;         } else {             return 0;         }     }  }  ///////////////method 1 (enum) public enum orientationtype {     uknown,     portrait,     landscape } ; 

choose 1 , should gold.

and on regarding multiple tap issue:-

check if there anyother gui nearby or overlapping. in question, had found out 2 p , l gameobjects had raycasters/colliders on top of each other, hence unity seemed flickering between two.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -