Advertisement
fortsoft

TelephoneBell

Apr 10th, 2023 (edited)
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | Source Code | 0 0
  1. /**
  2.  * This is open-source software licensed under the terms of the MIT License.
  3.  *
  4.  * Copyright (c) 2023 Petr Červinka - FortSoft <[email protected]>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  **
  24.  * Version 1.0.0.0
  25.  */
  26.  
  27. using System;
  28. using System.Diagnostics;
  29. using System.IO;
  30. using System.Threading.Tasks;
  31. using System.Windows.Forms;
  32. using WMPLib;
  33.  
  34. namespace FortSoft.Tools {
  35.  
  36.     /// <summary>
  37.     /// Implements standard landline telephone ringing based on the Tesla P51p
  38.     /// telephone exchange. It requires an imported MP3 file in the resources
  39.     /// called 'Bell'. The ring duration in this file must be one second.
  40.     /// </summary>
  41.     public class TelephoneBell : IDisposable {
  42.  
  43.         /// <summary>
  44.         /// The name of the destination MP3 file where the telephone bell sound
  45.         /// will be saved and made available to Windows Media Player.
  46.         /// </summary>
  47.         private const string bellFileName = "Bell.mp3";
  48.  
  49.         /// <summary>
  50.         /// Field with the full path to the extracted MP3 file with the telephone
  51.         /// bell sound.
  52.         /// </summary>
  53.         private string bellFilePath;
  54.  
  55.         /// <summary>
  56.         /// Field with a Timer object.
  57.         /// </summary>
  58.         private System.Timers.Timer timer;
  59.  
  60.         /// <summary>
  61.         /// Field with Windows Media Player object.
  62.         /// </summary>
  63.         private WindowsMediaPlayer windowsMediaPlayer;
  64.  
  65.         /// <summary>
  66.         /// Initializes a new instance of the <see cref="TelephoneBell"/> class.
  67.         /// </summary>
  68.         public TelephoneBell() {
  69.             ExtractBellAsync();
  70.  
  71.             bellFilePath = Path.Combine(Path.GetDirectoryName(Application.LocalUserAppDataPath), bellFileName);
  72.  
  73.             windowsMediaPlayer = new WindowsMediaPlayer();
  74.  
  75.             timer = new System.Timers.Timer(5000);
  76.             timer.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) => {
  77.                 if (File.Exists(bellFilePath)) {
  78.                     windowsMediaPlayer.controls.play();
  79.                 }
  80.             });
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Extracts the telephone bell sound in the MP3 file imported in the
  85.         /// resources called 'Bell' and saves it to the application data folder
  86.         /// as an MP3 file.
  87.         /// </summary>
  88.         private static async void ExtractBellAsync() {
  89.             await Task.Run(new Action(() => {
  90.                 try {
  91.                     string bellFilePath = Path.Combine(Path.GetDirectoryName(Application.LocalUserAppDataPath), bellFileName);
  92.                     if (!File.Exists(bellFilePath)) {
  93.                         File.WriteAllBytes(bellFilePath, Properties.Resources.Bell);
  94.                     }
  95.                 } catch (Exception exception) {
  96.                     Debug.WriteLine(exception);
  97.                     ErrorLog.WriteLine(exception);
  98.                 }
  99.             }));
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Disposes the timer.
  104.         /// </summary>
  105.         public void Dispose() => timer.Dispose();
  106.  
  107.         /// <summary>
  108.         /// Starts ringing.
  109.         /// </summary>
  110.         public void Ring() {
  111.             windowsMediaPlayer.URL = bellFilePath;
  112.             timer.Start();
  113.         }
  114.  
  115.         /// <summary>
  116.         /// Stops ringing.
  117.         /// </summary>
  118.         public void Stop() => timer.Stop();
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement