void processVideo() { if (videostate == VideoState.VIDEO_READY) { videostate = VideoState.VIDEO_STARTED; MLog("Starting video"); MLCamera.StartVideoCapture(filepath); MLog("Started video"); count = 0; } count++; if (count > 240 && videostate == VideoState.VIDEO_STARTED) { videostate = VideoState.VIDEO_ENDED; MLog("Stopping video"); MLCamera.StopVideoCapture(); MLog("Stopped video"); } if (videostate == VideoState.VIDEO_ENDED) { sendVideo(); } }
/// <summary> /// Start capturing video to input filename. /// </summary> /// <param name="fileName">File path to write the video to.</param> public void StartCapture(string fileName) { if (!_isCapturing && MLCamera.IsStarted && _isCameraConnected) { // Check file fileName extensions string extension = System.IO.Path.GetExtension(fileName); if (string.IsNullOrEmpty(extension) || !extension.Equals(_validFileFormat, System.StringComparison.OrdinalIgnoreCase)) { Debug.LogErrorFormat("Invalid fileName extension '{0}' passed into Capture({1}).\n" + "Videos must be saved in {2} format.", extension, fileName, _validFileFormat); return; } string pathName = System.IO.Path.Combine(Application.persistentDataPath, fileName); MLResult result = MLCamera.StartVideoCapture(pathName); if (result.IsOk) { _isCapturing = true; _captureStartTime = Time.time; _captureFilePath = pathName; OnVideoCaptureStarted.Invoke(); } else { Debug.LogErrorFormat("Failure: Could not start video capture for {0}. Error Code: {1}", fileName, MLCamera.GetErrorCode().ToString()); } } else { Debug.LogErrorFormat("Failure: Could not start video capture for {0} because '{1}' is already recording!", fileName, _captureFilePath); } }
async UniTask DoStart() { // Get a WiFi privilege await MLUtils.RequestPrivilege(MLPrivilegeId.LocalAreaNetwork); _credentials.ClearStorage(); _client = new TWClient(_credentials, _credentials); _onApplicationResume = new Subject <Unit>(); ClearLines(); AppendLine("You may use a dummy Twitter account for this demo."); AppendLine("Press Trigger button to start..."); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); ClearLines(); AppendLine("You'll be prompted to open Helio shortly..."); // Initiate 3-legged authentication. // Callback URL must be your app's URI configured in the manifest, // also must be registered in your Twitter app's "Callback URLs" list. // See https://forum.magicleap.com/hc/en-us/community/posts/360042601671 // and https://developer.twitter.com/en/docs/basics/apps/guides/callback-urls string authUrl = await _client.GetUserAuthenticationUrl(_credentials.CallbackUrl); // Open the URL on Helio and let user log in to Twitter MLDispatcher.TryOpenAppropriateApplication(authUrl).ThrowIfFail(); ClearLines(); AppendLine("Waiting for a redirect from Helio..."); AppendLine("(if you'd like to retry, please exit all apps)"); /* Magic Leap's dispatcher is generally immature and * you'll find MANY causes of failure here * but for this demo I just let user initialize everything in such cases * for the simplicity of this code... */ // Wait for getting redirected from Helio await _onApplicationResume.ToUniTask(useFirstValue : true); // Authorize using the redirect URL sent from Helio var redirectUrl = Environment.GetCommandLineArgs()[0]; await _client.AuthorizeUser(redirectUrl); ClearLines(); AppendLine("Authorized with your Twitter account!"); AppendLine("Press Trigger button to tweet on your account"); AppendLine("Please end this demo if your account is public)"); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); // Tweet a sample text (note that all characters survive URL encoding) await _client.UpdateStatus("Tweeting from #MagicLeap using MLTwitter https://github.com/ryo0ka/MLTwitter"); // Get the authorized user's latest status (which is the tweet above) var user = await _client.VerifyCredentials(); // Present the tweet to user await _statusView.Show(user); ClearLines(); AppendLine("Check out your new tweet!"); AppendLine("Press Trigger button to move on to the next demo..."); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); // Hide the twitter ui await _statusView.Hide(); ClearLines(); AppendLine("Press Trigger button to start video capture & upload..."); AppendLine("(Tweet will contain a video captured from now."); AppendLine("Please end this demo if your privacy is concerned)"); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); // Make a file path for the video string videoFilePath = Path.Combine(Application.temporaryCachePath, "video.mp4"); // Update privileges just in case await MLUtils.RequestPrivilege(MLPrivilegeId.LocalAreaNetwork); await MLUtils.RequestPrivilege(MLPrivilegeId.CameraCapture); await MLUtils.RequestPrivilege(MLPrivilegeId.AudioCaptureMic); // Start video recording MLCamera.Start().ThrowIfFail(); MLCamera.Connect().ThrowIfFail(); MLCamera.StartVideoCapture(videoFilePath); ClearLines(); AppendLine("Press Trigger button to STOP video capture and upload it on Twitter..."); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); // Stop video recording MLCamera.StopVideoCapture().ThrowIfFail(); ClearLines(); AppendLine("Stoped video capture. Encoding..."); // Wait until encoding is over await MLUtils.OnCaptureCompletedAsObservable().ToUniTask(useFirstValue: true); // Stop capture service MLCamera.Disconnect().ThrowIfFail(); MLCamera.Stop(); ClearLines(); AppendLine("Finished encoding. Uploading to Twitter..."); // Read the video file byte[] video = File.ReadAllBytes(videoFilePath); // Upload the video to Twitter (this is just a media upload; not a tweet) string videoMediaId = await _client.UploadVideo(video, (upload, encode) => { ClearLines(); AppendLine($"Uploading: {upload * 100:0}% done, encoding: {encode * 100:0}% done..."); }); // Tweet the video await _client.UpdateStatus("Uploading a video capture from #MagicLeap using MLTwitter", videoMediaId); // Present the tweet to user user = await _client.VerifyCredentials(); await _statusView.Show(user); ClearLines(); AppendLine("Check out your new tweet!"); AppendLine(""); AppendLine("You've reached the end of this demo."); AppendLine("Press Trigger button to exit the app..."); // Wait for a trigger by user await MLUtils.OnTriggerUpAsObservable().ToUniTask(useFirstValue: true); // Cool animation await _statusView.Hide(); Application.Quit(); }