Shruthi Kadam

Shruthi Kadam

  • 2k
  • 69
  • 1.1k

to enable audio controls in cefsharp Browser

Feb 15 2021 7:01 AM
Hello Sir,
I have changed default web broswer to cefsharp chromium web browser but the thing is chrome is not supporting audio with in my browser when i upload html pages . I have tried this
  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Media.Imaging;  
  4. using Syncfusion.Windows.Shared;  
  5. using Microsoft.Win32.SafeHandles;  
  6. using VISL.Entity;  
  7. using VISL.Common;  
  8. using VISL.BoxedApp;  
  9. using VISL.DAL;  
  10. using System.IO;  
  11. using System.Runtime.InteropServices;  
  12. using System.Windows.Interop;  
  13. using VISL.Subjects;  
  14. using System.Windows.Input;  
  15. using System.Collections.Generic;  
  16. using System.Text.RegularExpressions;  
  17. using System.Windows.Controls;  
  18. using System.Windows.Navigation;  
  19. using System.Reflection;  
  20. using System.IO.Compression;  
  21. using CefSharp;  
  22. using CefSharp.Wpf;  
  23. namespace VISL.Subjects  
  24. {  
  25. /// <summary>  
  26. /// Interaction logic for CefTest.xaml  
  27. /// </summary>  
  28. public partial class CefTest : Window  
  29. {  
  30. Window prevWindow;  
  31. ContentMasterDTO content;  
  32. string tempDirPath = string.Empty;  
  33. //DisplayRestriction displayRestriction = new DisplayRestriction();  
  34. VideoPlayerDAL videoPlayerDAL = new VideoPlayerDAL();  
  35. PreviousSelection prevSelection;  
  36. #region Print Screen and Other Restrictions  
  37. [DllImport("user32.dll")]  
  38. static extern bool SetWindowDisplayAffinity(IntPtr hwnd, DisplayAffinity affinity);  
  39. [DllImport("user32.dll")]  
  40. static extern IntPtr GetActiveWindow();  
  41. #endregion  
  42. public CefTest()  
  43. {  
  44. InitializeComponent();  
  45. this.Icon = new BitmapImage(new Uri(CommonProperties.AppIcon));  
  46. this.WindowState = WindowState.Maximized;  
  47. this.WindowState = System.Windows.WindowState.Maximized;  
  48. this.WindowStyle = WindowStyle.SingleBorderWindow;  
  49. }  
  50. public CefTest(ContentMasterDTO obj) : this()  
  51. {  
  52. content = obj;  
  53. }  
  54. public CefTest(ContentMasterDTO obj, Window _prevWindow) : this()  
  55. {  
  56. content = obj;  
  57. prevWindow = _prevWindow;  
  58. }  
  59. public CefTest(ContentMasterDTO obj, PreviousSelection prev) : this()  
  60. {  
  61. content = obj;  
  62. prevSelection = prev;  
  63. }  
  64. private void Window_Loaded(object sender, EventArgs e)  
  65. {  
  66. if (prevWindow != null)  
  67. {  
  68. prevWindow.Hide();  
  69. }  
  70. //string htmlString = System.IO.File.ReadAllText(CommonProperties.ExecutablePath + @"\Contents\Agriculture\Index.htm");  
  71. //htmlString = ImageDisplay(htmlString);  
  72. //webBrowser.NavigateToString(htmlString);  
  73. //string path = @"E:\VISL\VISL\bin\x86\Debug\Contents\Agriculture\Index.html";  
  74. //webBrowser.Navigate(path);  
  75. //webBrowser.Navigated += new NavigatedEventHandler(webBrowser_Navigated);  
  76. //InitHTMLPlayer();  
  77. //var hwnd = new WindowInteropHelper(this).Handle;  
  78. //SetWindowDisplayAffinity(hwnd, DisplayAffinity.Monitor);  
  79. }  
  80. public void InitHTMLPlayer()  
  81. {  
  82. try  
  83. {  
  84. #region Display HTML  
  85. BoxedAppSDK.NativeMethods.BoxedAppSDK_SetContext("29c3c554-6033-4eb5-95fe-cadc013c229e"); // <-- set the key here  
  86. BoxedAppSDK.NativeMethods.BoxedAppSDK_Init();  
  87. //content.ContentPath = @"E:\Visual studio projects\VISL\bin\Debug\Contents\MicroOrganism_#8EEIOP7#V1W07I.zip.Pro";  
  88. //EncryptionKeyDTO obj = videoPlayerDAL.GetEncryptionKey(11);  
  89. EncryptionKeyDTO obj = videoPlayerDAL.GetEncryptionKey(content.IdEncryptionKey);  
  90. CommonUtils.Key = Convert.FromBase64String(obj.EncryptionKEY);  
  91. CommonUtils.IV = Convert.FromBase64String(obj.EncryptionIV);  
  92. using (SafeFileHandle fileHandle =  
  93. new SafeFileHandle(  
  94. BoxedAppSDK.NativeMethods.BoxedAppSDK_CreateVirtualFileBasedOnIStream(  
  95. @"virtualZipFile.zip"// name of the pseudo file  
  96. BoxedAppSDK.NativeMethods.EFileAccess.GenericWrite,  
  97. BoxedAppSDK.NativeMethods.EFileShare.Read,  
  98. IntPtr.Zero,  
  99. BoxedAppSDK.NativeMethods.ECreationDisposition.New,  
  100. BoxedAppSDK.NativeMethods.EFileAttributes.Normal,  
  101. IntPtr.Zero,  
  102. new VirtualFileStream(content.ContentPath)  
  103. ),  
  104. true  
  105. )  
  106. )  
  107. {  
  108. // We use "using" to close the allocated handle  
  109. // The virtual file will still exist  
  110. }  
  111. //// Load a html in HTMLPlayer.  
  112. tempDirPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\OfflineHtml";  
  113. //tempDirPath = "D:\"  
  114. //tempDirPath = @"C:\OfflineHtml";  
  115. if (!Directory.Exists(tempDirPath))  
  116. Directory.CreateDirectory(tempDirPath);  
  117. else  
  118. DeleteDirectory(tempDirPath);  
  119. Directory.CreateDirectory(tempDirPath);  
  120. ReadWriteStream();  
  121. string zipPath = tempDirPath + "\\tempVirtualFile.zip";  
  122. System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, tempDirPath);  
  123. File.Delete(zipPath);  
  124. foreach (string d in Directory.GetDirectories(tempDirPath))  
  125. {  
  126. tempDirPath = d + "\\index.html";  
  127. }  
  128. //webBrowser.Navigate(new Uri(tempDirPath, UriKind.RelativeOrAbsolute));  
  129. web.Address = tempDirPath;  
  130. // webBrowser.URL = tempDirPath;  
  131. // webBrowser.Source = new Uri(tempDirPath);  
  132. //tempDirPath = System.IO.Path.GetTempPath() + "OfflineHtml";  
  133. //if (!Directory.Exists(tempDirPath))  
  134. // Directory.CreateDirectory(tempDirPath);  
  135. //Stream a = File.Open(@"virtualZipFile.html", FileMode.Open);  
  136. //Stream b = File.Open(tempDirPath + "\\tempVirtualFile.html", FileMode.Create);  
  137. //ReadWriteStream(a, b);  
  138. //tempDirPath = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;  
  139. //tempDirPath = tempDirPath + "\\virtualZipFile.html";  
  140. //Stream source = File.OpenRead(tempDirPath);  
  141. //webBrowser.NavigateToStream(source);  
  142. //webBrowser.Navigated += new NavigatedEventHandler(webBrowser_Navigated);  
  143. #endregion  
  144. }  
  145. catch (Exception ex)  
  146. {  
  147. MessageBox.Show(ex.Message);  
  148. }  
  149. }  
  150. private void ReadWriteStream()//Stream readStream, Stream writeStream  
  151. {  
  152. Stream readStream = File.Open(@"virtualZipFile.zip", FileMode.Open);  
  153. Stream writeStream = File.Open(tempDirPath + "\\tempVirtualFile.zip", FileMode.Create);  
  154. int Length = 1024 * 1024; //Convert.ToInt32(readStream.Length / 16);  
  155. Byte[] buffer = new Byte[1024 * 1024];  
  156. int bytesRead = 1; //readStream.Read(buffer, 0, Length);  
  157. // write the required bytes  
  158. while (bytesRead > 0)  
  159. {  
  160. bytesRead = readStream.Read(buffer, 0, Length);  
  161. writeStream.Write(buffer, 0, bytesRead);  
  162. }  
  163. readStream.Close();  
  164. writeStream.Close();  
  165. readStream.Dispose();  
  166. writeStream.Dispose();  
  167. GC.Collect();  
  168. }  
  169. private void DeleteDirectory(string path)  
  170. {  
  171. try  
  172. {  
  173. if (Directory.Exists(path))  
  174. {  
  175. string[] fileList = Directory.GetFiles(path);  
  176. //Delete all files from the Directory  
  177. foreach (string file in fileList)//Directory.GetFiles(path))  
  178. {  
  179. FileAttributes attr = File.GetAttributes(file);  
  180. //if (attr == FileAttributes.ReadOnly)  
  181. // File.SetAttributes(file, FileAttributes.Normal);  
  182. File.Delete(file);  
  183. }  
  184. //Delete all child Directories  
  185. foreach (string directory in Directory.GetDirectories(path))  
  186. {  
  187. DeleteDirectory(directory);  
  188. }  
  189. //Delete a Directory  
  190. Directory.Delete(path, true);  
  191. }  
  192. }  
  193. catch (Exception ex)  
  194. {  
  195. Console.Write(ex.Message);  
  196. }  
  197. }  
  198. private void webBrowser_Navigated(object sender, NavigationEventArgs e)  
  199. {  
  200. //SetSilent(webBrowser, true); // make it silent  
  201. }  
  202. public string ImageDisplay(string htmlString)  
  203. {  
  204. string part1 = " <img src=" + "\"" + CommonProperties.ExecutablePath + "\\App_Images\\";  
  205. string part2 = "\"" + " />";  
  206. string pattern1 = @"<(img)\b[^>]*>";  
  207. string pattern2 = @"</(img)\b[^>]*>";  
  208. GetImagesInHTMLString(htmlString, pattern1).ForEach(i =>  
  209. {  
  210. htmlString = htmlString.Replace(i, part1);  
  211. });  
  212. GetImagesInHTMLString(htmlString, pattern2).ForEach(i =>  
  213. {  
  214. htmlString = htmlString.Replace(i, part2);  
  215. });  
  216. return htmlString;  
  217. }  
  218. private List<string> GetImagesInHTMLString(string htmlString, string pattern)  
  219. {  
  220. List<string> images = new List<string>();  
  221. Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);  
  222. MatchCollection matches = rgx.Matches(htmlString);  
  223. for (int i = 0, l = matches.Count; i < l; i++)  
  224. {  
  225. images.Add(matches[i].Value);  
  226. }  
  227. return images;  
  228. }  
  229. public static void SetSilent(WebBrowser browser, bool silent)  
  230. {  
  231. if (browser == null)  
  232. throw new ArgumentNullException("browser");  
  233. // get an IWebBrowser2 from the document  
  234. IOleServiceProvider sp = browser.Document as IOleServiceProvider;  
  235. if (sp != null)  
  236. {  
  237. Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");  
  238. Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");  
  239. object webBrowser;  
  240. sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);  
  241. if (webBrowser != null)  
  242. {  
  243. webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });  
  244. }  
  245. }  
  246. }  
  247. private void btnBack_Click(object sender, RoutedEventArgs e)  
  248. {  
  249. //webBrowser = new WebBrowser();  
  250. //Cef.Shutdown();  
  251. DeleteDirectory(tempDirPath);  
  252. if (prevWindow != null)  
  253. {  
  254. this.Hide();  
  255. prevWindow.Show();  
  256. }  
  257. else  
  258. {  
  259. CefTest currentWindow = this;  
  260. MainWindow obj = new MainWindow(prevSelection);  
  261. obj.Show();  
  262. currentWindow.Visibility = Visibility.Hidden;  
  263. }  
  264. }  
  265. private void btnHome_Click(object sender, RoutedEventArgs e)  
  266. {  
  267. //webBrowser = new WebBrowser();  
  268. DeleteDirectory(tempDirPath);  
  269. List_standards frmList_standards = new VISL.List_standards(this);  
  270. frmList_standards.Show();  
  271. }  
  272. private void btnInfo_Click(object sender, RoutedEventArgs e)  
  273. {  
  274. //webBrowser = new WebBrowser();  
  275. DeleteDirectory(tempDirPath);  
  276. AbountUs frmAboutUs = new AbountUs(this);  
  277. frmAboutUs.Show();  
  278. }  
  279. private void Window_Closed(object sender, EventArgs e)  
  280. {  
  281. //webBrowser = new WebBrowser();  
  282. // Cef.Shutdown();  
  283. //webBrowser.Source = null;  
  284. //DeleteDirectory(tempDirPath);  
  285. /// Cef.Shutdown();  
  286. DeleteDirectory(tempDirPath);  
  287. }  
  288. }  
  289. }