/// <summary> /// Aborts the working thread. /// </summary> /// <remarks>Documented by Dev02, 2008-05-20</remarks> internal static void AbortWorkingThread() { if (WorkingThreadActive) { workerthread.Abort(); } //try to delete the temp directory (try multiple times in case it is still locked) Thread cleanthread = new Thread(delegate() { int tries = 0; while (tries++ < 10) { System.Threading.Thread.Sleep(1000); try { TempFiles.ForEach(f => f.Delete()); break; } catch { } } }); cleanthread.IsBackground = true; cleanthread.Start(); }
/// <summary> /// Generates the audio book. /// </summary> /// <param name="dictionary">The dictionary.</param> /// <param name="audiobook">The target audiobook file.</param> /// <param name="options">The options.</param> /// <param name="worker">The worker.</param> /// <param name="codecs">The codecs.</param> /// <remarks>Documented by Dev02, 2008-03-10</remarks> public static void GenerateAudioBook(IDictionary dictionary, FileInfo audiobook, AudioBookOptions options, Codecs codecs) { if (workerthread != null && workerthread.IsAlive) { AddLog("Error: Another operation is still active."); return; } workerthread = new Thread(delegate() { try { Dictionary <string, Codec> encodeCodecs = codecs.encodeCodecs; Dictionary <string, Codec> decodeCodecs = codecs.decodeCodecs; if (audiobook.Extension.ToLowerInvariant() != Resources.AUDIO_WAVE_EXTENSION.ToLowerInvariant() && !encodeCodecs.ContainsKey(audiobook.Extension.ToLowerInvariant())) { AddLog(string.Format("Specified extension ({0}) is not available => Check encoder settings", audiobook.Extension.ToLowerInvariant())); return; } List <MediaFieldFile> sourcefiles = new List <MediaFieldFile>(); //source files from dictionary Environment.CurrentDirectory = new FileInfo(dictionary.Connection).Directory.FullName; //fetch source audio files AddLog("Fetching source audio files"); int count = dictionary.Cards.Cards.Count; int index = 0; foreach (ICard card in dictionary.Cards.Cards) { sourcefiles.AddRange(GetCardMediaFiles(card, options.MediaFields)); ReportProgress(index++, count); } //search and generate a new temp folder DirectoryInfo tempfolder; int tempfolderindex = 0; do { tempfolder = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Settings.Default.TempFolderPath + tempfolderindex.ToString())); tempfolderindex++; } while (tempfolder.Exists); tempfolder.Create(); try { //convert all source files to wave where necessary AddLog("Converting source files"); bool foundfile = false; count = sourcefiles.Count; index = 0; for (int i = 0; i < sourcefiles.Count; i++) { if (sourcefiles[i].ContainsFile && decodeCodecs.ContainsKey(sourcefiles[i].Extension)) { //decode file string filename = sourcefiles[i].file.Name; sourcefiles[i].file = decodeCodecs[sourcefiles[i].Extension].Decode(sourcefiles[i].file, tempfolder, Settings.Default.ShowDecodingWindow, Settings.Default.MimimizeWindows); if (sourcefiles[i].ContainsFile) { foundfile = true; } else { AddLog(string.Format("Decoding ({0}) did not produce a file => Check decoder settings", filename)); } } else if (sourcefiles[i].ContainsFile && sourcefiles[i].Extension == Resources.AUDIO_WAVE_EXTENSION.ToLowerInvariant()) { foundfile = true; } else if (sourcefiles[i].ContainsFile) { AddLog(string.Format("Extension {0} not supported ({1}) => Check decoder settings", sourcefiles[i].Extension, sourcefiles[i].file.Name)); } ReportProgress(index++, count); } if (!foundfile) { AddLog("No supported audio files found in the selected fields of the learning module"); ReportProgress(0, 0); //disable progress reporting } else { //concatenate all wave files AddLog("Joining audio files"); FileInfo audiobookwave = new FileInfo(Path.Combine(tempfolder.FullName, Resources.AUDIOBOOK_DEFAULTNAME + Resources.AUDIO_WAVE_EXTENSION)); ABWaveCat wavecat = new ABWaveCat(); wavecat.Concatenate(sourcefiles, audiobookwave, options.Stereo); ReportProgress(0, 0); //disable progress reporting bool changeToWaveExtension = true; //fix for [MLA-1272] error message for file path without extension if (audiobook.Extension.ToLowerInvariant() != Resources.AUDIO_WAVE_EXTENSION.ToLowerInvariant()) { if (encodeCodecs.ContainsKey(audiobook.Extension.ToLowerInvariant())) { //convert audiobook to specified format AddLog("Converting audiobook"); FileInfo encodedfile = encodeCodecs[audiobook.Extension.ToLowerInvariant()].Encode(audiobookwave, tempfolder, Settings.Default.ShowEncodingWindow, Settings.Default.MimimizeWindows); if (encodedfile != null) { try { if (audiobook.Exists) { audiobook.Delete(); } encodedfile.MoveTo(audiobook.FullName); changeToWaveExtension = false; } catch (Exception e) { AddLog("Could not replace audio file: " + e.Message); return; } } else { AddLog(string.Format("Encoding ({0}) did not produce a file => Check encoder settings", audiobook.Name)); } } } if (changeToWaveExtension) //change file to wave extension and force wave generation { audiobook = new FileInfo(Path.ChangeExtension(audiobook.FullName, Resources.AUDIO_WAVE_EXTENSION)); } if (audiobook.Extension.ToLowerInvariant() == Resources.AUDIO_WAVE_EXTENSION.ToLowerInvariant()) { try { if (audiobook.Exists) { audiobook.Delete(); } audiobookwave.MoveTo(audiobook.FullName); } catch (Exception e) { AddLog("Could not replace audio file: " + e.Message); return; } } AddLog("Finished: " + audiobook.FullName); } } finally { //try to delete the temp directory (try multiple times in case it is still locked) Thread cleanthread = new Thread(delegate() { int tries = 0; while (tries++ < 10) { System.Threading.Thread.Sleep(1000); try { tempfolder.Delete(true); TempFiles.ForEach(f => f.Delete()); break; } catch { } } }); cleanthread.IsBackground = true; cleanthread.Start(); } } catch (Exception exp) { ReportProgress(0, 0); //disable progressbar if (exp is ThreadAbortException) { AddLog("Operation cancelled"); } else { AddLog("GenerateAudioBook Thread Exception: " + exp.ToString()); } } finally { //fire the working thread finished event in every case OnWorkingThreadFinished(EventArgs.Empty); } }); workerthread.IsBackground = true; workerthread.Start(); return; }