Advertisement
urksiful

VideoPlayBack Unity Vuforia Fix (2016/August)

Aug 2nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1.  
  2. /*==============================================================================
  3. Copyright (c) 2012-2014 QUALCOMM Austria Research Center GmbH.
  4. All Rights Reserved.
  5.  
  6. This  Vuforia(TM) sample application in source code form ("Sample Code") for the
  7. Vuforia Software Development Kit and/or Vuforia Extension for Unity
  8. (collectively, the "Vuforia SDK") may in all cases only be used in conjunction
  9. with use of the Vuforia SDK, and is subject in all respects to all of the terms
  10. and conditions of the Vuforia SDK License Agreement, which may be found at
  11. <a href="https://developer.vuforia.com/legal/license">https://developer.vuforia.com/legal/license</a>.
  12.  
  13. By retaining or using the Sample Code in any manner, you confirm your agreement
  14. to all the terms and conditions of the Vuforia SDK License Agreement.  If you do
  15. not agree to all the terms and conditions of the Vuforia SDK License Agreement,
  16. then you may not retain or use any of the Sample Code in any manner.
  17. ==============================================================================*/
  18.  
  19. using UnityEngine;
  20. using System.Collections;
  21.  
  22. /// <summary>
  23. /// This class contains the logic to handle taps on VideoPlaybackBehaviour game objects
  24. /// and starts playing the according video. It also pauses other videos when a new one is
  25. /// started.
  26. /// </summary>
  27. public class VideoPlaybackController : MonoBehaviour
  28. {
  29.     #region PRIVATE_MEMBER_VARIABLES
  30.  
  31.     private Vector2 mTouchStartPos;
  32.     private bool mTouchMoved = false;
  33.     private float mTimeElapsed = 0.0f;
  34.  
  35.     private bool mTapped = false;
  36.     private float mTimeElapsedSinceTap = 0.0f;
  37.  
  38.     private bool mWentToFullScreen = false;
  39.  
  40.     public object QCARRuntimeUtilities { get; private set; }
  41.  
  42.     #endregion // PRIVATE_MEMBER_VARIABLES
  43.  
  44.  
  45.  
  46.     #region UNITY_MONOBEHAVIOUR_METHODS
  47.  
  48.     void Update()
  49.     {
  50.         // Determine the number of taps
  51.         // Note: Input.tapCount doesn't work on Android
  52.  
  53.         if (Input.touchCount > 0)
  54.         {
  55.             Touch touch = Input.touches[0];
  56.             if (touch.phase == TouchPhase.Began)
  57.             {
  58.                 mTouchStartPos = touch.position;
  59.                 mTouchMoved = false;
  60.                 mTimeElapsed = 0.0f;
  61.             }
  62.             else
  63.             {
  64.                 mTimeElapsed += Time.deltaTime;
  65.             }
  66.  
  67.             if (touch.phase == TouchPhase.Moved)
  68.             {
  69.                 if (Vector2.Distance(mTouchStartPos, touch.position) > 40)
  70.                 {
  71.                     // Touch moved too far
  72.                     mTouchMoved = true;
  73.                 }
  74.             }
  75.             else if (touch.phase == TouchPhase.Ended)
  76.             {
  77.                 if (!mTouchMoved && mTimeElapsed < 1.0)
  78.                 {
  79.                     if (mTapped)
  80.                     {
  81.                         // Second tap
  82.                         HandleDoubleTap();
  83.                         mTapped = false;
  84.                     }
  85.                     else
  86.                     {
  87.                         // Wait to see if this is a double tap
  88.                         mTapped = true;
  89.                         mTimeElapsedSinceTap = 0.0f;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.  
  95.         if (mTapped)
  96.         {
  97.             if (mTimeElapsedSinceTap >= 0.5f)
  98.             {
  99.                 // Not a double tap
  100.                 HandleTap();
  101.                 mTapped = false;
  102.             }
  103.             else
  104.             {
  105.                 mTimeElapsedSinceTap += Time.deltaTime;
  106.             }
  107.         }
  108.  
  109.         // special handling in play mode:
  110.        
  111.     }
  112.  
  113.     #endregion // UNITY_MONOBEHAVIOUR_METHODS
  114.  
  115.  
  116.  
  117.     #region PRIVATE_METHODS
  118.  
  119.     /// <summary>
  120.     /// Handle single tap event
  121.     /// </summary>
  122.     private void HandleTap()
  123.     {
  124.         // Find out which video was tapped, if any
  125.         VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);
  126.  
  127.         if (video != null)
  128.         {
  129.             if (video.VideoPlayer.IsPlayableOnTexture())
  130.             {
  131.                 // This video is playable on a texture, toggle playing/paused
  132.  
  133.                 VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
  134.                 if (state == VideoPlayerHelper.MediaState.PAUSED ||
  135.                     state == VideoPlayerHelper.MediaState.READY ||
  136.                     state == VideoPlayerHelper.MediaState.STOPPED)
  137.                 {
  138.                     // Pause other videos before playing this one
  139.                     PauseOtherVideos(video);
  140.  
  141.                     // Play this video on texture where it left off
  142.                     video.VideoPlayer.Play(false, video.VideoPlayer.GetCurrentPosition());
  143.                 }
  144.                 else if (state == VideoPlayerHelper.MediaState.REACHED_END)
  145.                 {
  146.                     // Pause other videos before playing this one
  147.                     PauseOtherVideos(video);
  148.  
  149.                     // Play this video from the beginning
  150.                     video.VideoPlayer.Play(false, 0);
  151.                 }
  152.                 else if (state == VideoPlayerHelper.MediaState.PLAYING)
  153.                 {
  154.                     // Video is already playing, pause it
  155.                     video.VideoPlayer.Pause();
  156.                 }
  157.             }
  158.             else
  159.             {
  160.                 // Display the busy icon
  161.                 video.ShowBusyIcon();
  162.  
  163.                 // This video cannot be played on a texture, play it full screen
  164.                 video.VideoPlayer.Play(true, 0);
  165.                 mWentToFullScreen = true;
  166.             }
  167.         }
  168.     }
  169.  
  170.  
  171.     /// <summary>
  172.     /// Handle double tap event
  173.     /// </summary>
  174.     private void HandleDoubleTap()
  175.     {
  176.         // Find out which video was tapped, if any
  177.         VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);
  178.  
  179.         if (video != null)
  180.         {
  181.             if (video.VideoPlayer.IsPlayableFullscreen())
  182.             {
  183.                 // Pause the video if it is currently playing
  184.                 video.VideoPlayer.Pause();
  185.  
  186.                 // Seek the video to the beginning();
  187.                 video.VideoPlayer.SeekTo(0.0f);
  188.  
  189.                 // Display the busy icon
  190.                 video.ShowBusyIcon();
  191.  
  192.                 // Play the video full screen
  193.                 video.VideoPlayer.Play(true, 0);
  194.                 mWentToFullScreen = true;
  195.             }
  196.         }
  197.     }
  198.  
  199.  
  200.     /// <summary>
  201.     /// Find the video object under the screen point
  202.     /// </summary>
  203.     private VideoPlaybackBehaviour PickVideo(Vector3 screenPoint)
  204.     {
  205.         VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
  206.                 FindObjectsOfType(typeof(VideoPlaybackBehaviour));
  207.  
  208.         Ray ray = Camera.main.ScreenPointToRay(screenPoint);
  209.         RaycastHit hit = new RaycastHit();
  210.  
  211.         foreach (VideoPlaybackBehaviour video in videos)
  212.         {
  213.             if (video.GetComponent<Collider>().Raycast(ray, out hit, 10000))
  214.             {
  215.                 return video;
  216.             }
  217.         }
  218.  
  219.         return null;
  220.     }
  221.  
  222.  
  223.     /// <summary>
  224.     /// Pause all videos except this one
  225.     /// </summary>
  226.     private void PauseOtherVideos(VideoPlaybackBehaviour currentVideo)
  227.     {
  228.         VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
  229.                 FindObjectsOfType(typeof(VideoPlaybackBehaviour));
  230.  
  231.         foreach (VideoPlaybackBehaviour video in videos)
  232.         {
  233.             if (video != currentVideo)
  234.             {
  235.                 if (video.CurrentState == VideoPlayerHelper.MediaState.PLAYING)
  236.                 {
  237.                     video.VideoPlayer.Pause();
  238.                 }
  239.             }
  240.         }
  241.     }
  242.  
  243.     #endregion // PRIVATE_METHODS
  244.  
  245.  
  246.  
  247.     #region PUBLIC_METHODS
  248.  
  249.     /// <summary>
  250.     /// One-time check for the Instructional Screen
  251.     /// </summary>
  252.     public bool CheckWentToFullScreen()
  253.     {
  254.         bool result = mWentToFullScreen;
  255.         mWentToFullScreen = false;
  256.         return result;
  257.     }
  258.  
  259.     #endregion // PUBLIC_METHODS
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement