From 9a196d0a884715658f98cee1028b60fdde1ca62b Mon Sep 17 00:00:00 2001 From: Manu Date: Tue, 7 Jan 2020 17:25:57 +0100 Subject: [PATCH] check some nulls errors. --- .../Scripts/DownloadYoutubeVideo.cs | 16 +++++++++- .../Scripts/YoutubeAudioDownload.cs | 31 ++++++++++++------ Assets/YoutubePlayer/Scripts/YoutubePlayer.cs | 32 +++++++++++++++++-- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/Assets/YoutubePlayer/Scripts/DownloadYoutubeVideo.cs b/Assets/YoutubePlayer/Scripts/DownloadYoutubeVideo.cs index 590f040..b6b864d 100644 --- a/Assets/YoutubePlayer/Scripts/DownloadYoutubeVideo.cs +++ b/Assets/YoutubePlayer/Scripts/DownloadYoutubeVideo.cs @@ -29,6 +29,11 @@ private void Start() public async void Download() { + if (string.IsNullOrEmpty(youtubePlayer.youtubeUrl)) + { + return; + } + YoutubePlayer.OnDownloading.Invoke(); try @@ -37,7 +42,7 @@ public async void Download() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/ExtractedVideo"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var videoDownloadTask = youtubePlayer.DownloadVideoAsync(v, null, this); @@ -73,6 +78,15 @@ private void Update() { if (downloadProgress) downloadProgress.fillAmount = progress; + + if (Input.GetKeyDown(KeyCode.F1)) + { + var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/ExtractedVideo/"; + if (!Directory.Exists(v)) + Directory.CreateDirectory(v); + + System.Diagnostics.Process.Start(Path.GetFullPath(v)); + } } } } \ No newline at end of file diff --git a/Assets/YoutubePlayer/Scripts/YoutubeAudioDownload.cs b/Assets/YoutubePlayer/Scripts/YoutubeAudioDownload.cs index 4fbddb5..af63afc 100644 --- a/Assets/YoutubePlayer/Scripts/YoutubeAudioDownload.cs +++ b/Assets/YoutubePlayer/Scripts/YoutubeAudioDownload.cs @@ -43,7 +43,7 @@ public void DownloadAudioAsync() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var link1 = new LinkInfo(youtubePlayer.youtubeUrl); @@ -91,7 +91,7 @@ public void DownloadAudioSync() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var link1 = new LinkInfo(youtubePlayer.youtubeUrl); @@ -147,7 +147,7 @@ public async void DownloadAudio() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"\Recordings\"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var link1 = new LinkInfo(youtubePlayer.youtubeUrl); @@ -210,7 +210,7 @@ void NewTest() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"\Recordings\"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var source = v; @@ -250,7 +250,7 @@ async void OldDownload() var bytes = await video.GetBytesAsync(); var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); var filePath = v + @"\" + video.FullName + ".mp3"; @@ -310,7 +310,7 @@ public async void DownloadVideoAndExtract() var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/ExtractedAudio/"; - if (Directory.Exists(v)) + if (!Directory.Exists(v)) Directory.CreateDirectory(v); try @@ -319,8 +319,14 @@ public async void DownloadVideoAndExtract() YoutubePlayer.OnEndDownload.Invoke(); string filename = Path.GetFileNameWithoutExtension(filePath); - //System.Diagnostics.Process.Start(Application.dataPath + "/convert.bat", $@"""{Application.dataPath}/MediaToolkit"" ""{v}{filename}.mp4"" ""{v}{filename}.mp3"""); - System.Diagnostics.Process p = new System.Diagnostics.Process(); + string audioname = $"{v}{filename}.mp3"; + if (File.Exists(audioname)) + { + File.Delete(audioname); + } + + //System.Diagnostics.Process.Start(Application.dataPath + "/convert.bat", $@"""{Application.dataPath}/MediaToolkit"" ""{v}{filename}.mp4"" ""{v}{filename}.mp3"""); + System.Diagnostics.Process p = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.CreateNoWindow = false; @@ -366,7 +372,14 @@ private void Update() if (downloadProgress) downloadProgress.fillAmount = progress; - + if (Input.GetKeyDown(KeyCode.F2)) + { + var v = (useDataPath ? System.IO.Directory.GetCurrentDirectory() : Environment.GetFolderPath(destination)) + @"/Recordings/ExtractedAudio/"; + if (!Directory.Exists(v)) + Directory.CreateDirectory(v); + + System.Diagnostics.Process.Start(Path.GetFullPath(v)); + } } } } \ No newline at end of file diff --git a/Assets/YoutubePlayer/Scripts/YoutubePlayer.cs b/Assets/YoutubePlayer/Scripts/YoutubePlayer.cs index 873de18..0351d0e 100644 --- a/Assets/YoutubePlayer/Scripts/YoutubePlayer.cs +++ b/Assets/YoutubePlayer/Scripts/YoutubePlayer.cs @@ -53,14 +53,26 @@ public class YoutubePlayer : MonoBehaviour public bool paused = false; public Text m_pauseTxt; public KeyCode exitKey = KeyCode.Escape; - public KeyCode pauseResumeKey = KeyCode.Space; + public KeyCode pauseResumeKey = KeyCode.S; public KeyCode playkey = KeyCode.Return; + public string defaultVideo = "https://www.youtube.com/watch?v=2oRxLgoNpRs"; private void Awake() { + if (string.IsNullOrEmpty(m_inputField.text)) + { + m_inputField.text = defaultVideo; + } + if (overrideURLWithInputField && !string.IsNullOrEmpty(m_inputField.text)) + { youtubeUrl = m_inputField.text; - + } + else + { + youtubeUrl = ""; + } + youtubeClient = new YoutubeClient(); videoPlayer = GetComponent(); _slider.value = PlayerPrefs.GetFloat("YoutubeVolume", 1.0f); @@ -97,7 +109,11 @@ private void OnApplicationQuit() private async void OnEnable() { - + if (string.IsNullOrEmpty(youtubeUrl)) + { + return; + } + if (videoPlayer.playOnAwake) await PlayVideoAsync(); } @@ -117,6 +133,11 @@ public void SetVolume() public async void Play() { + if (string.IsNullOrEmpty(youtubeUrl)) + { + return; + } + paused = false; await PlayVideoAsync(); } @@ -171,6 +192,11 @@ public void Restart() public void Pause() { + if (string.IsNullOrEmpty(youtubeUrl)) + { + return; + } + if (!paused) { m_pauseTxt.text = "Resume";