private void queueMedia(bool music_only = false) { int ni = 1; //wplayer.controls.stop(); //wplayer.currentPlaylist.clear(); // remove everything from current position onwards for (int i = 0; i < wplayer.currentPlaylist.count - 1; i++) { if (wplayer.controls.currentItem.sourceURL == wplayer.currentPlaylist.get_Item(i).sourceURL) { int j = i + 1; while (wplayer.currentPlaylist.count > j) { wplayer.currentPlaylist.removeItem(wplayer.currentPlaylist.get_Item(j)); } break; } } if (music_only) { for (ni = 1; ni < songPlayList.Count; ni++) { String url = songPlayList[ni]; wplayer.currentPlaylist.appendItem(wplayer.newMedia(url)); } } else { foreach (String p in podPlayList) { wplayer.currentPlaylist.appendItem(wplayer.newMedia(p)); if ((songPlayList.Count > 0) && (bool)configWindow.altMusicCheckBox.IsChecked) { String url = songPlayList[ni++]; if (ni >= songPlayList.Count) { ni = 0; //start again } wplayer.currentPlaylist.appendItem(wplayer.newMedia(url)); } } wplayer.controls.play(); startTime = DateTime.Now; } }
public void PlayListCreation(List <string> filesSelected) { for (int i = 0; i < filesSelected.Count; i++) { playlist.appendItem(wplayer.newMedia(filesSelected[i].Replace(@"\\", @"\"))); } wplayer.currentPlaylist = playlist; }
public static void sound() { while (true) { if (clients.Count > 0) { try { foreach (KeyValuePair <int, int> entry in clients) { int num = entry.Key; int ra = entry.Value; string donvi = num.ToString().Substring(3, 1); string chuc = num.ToString().Substring(2, 1); string tram = num.ToString().Substring(1, 1); string nghin = num.ToString().Substring(0, 1); WMPLib.WindowsMediaPlayer mp = new WMPLib.WindowsMediaPlayer(); WMPLib.IWMPPlaylist playlist = mp.playlistCollection.newPlaylist("customerCall"); WMPLib.IWMPMedia media, media1, media2, media3, media4, media5, media6; string moi = Directory.GetCurrentDirectory() + "\\Resources\\moi.mp3"; media = mp.newMedia(moi); string sokhachnghin = Directory.GetCurrentDirectory() + "\\Resources\\" + nghin + ".mp3"; media1 = mp.newMedia(sokhachnghin); string sokhachtram = Directory.GetCurrentDirectory() + "\\Resources\\" + tram + ".mp3"; media2 = mp.newMedia(sokhachtram); string sokhachchuc = Directory.GetCurrentDirectory() + "\\Resources\\" + chuc + ".mp3"; media3 = mp.newMedia(sokhachchuc); string sokhachdonvi = Directory.GetCurrentDirectory() + "\\Resources\\" + donvi + ".mp3"; media4 = mp.newMedia(sokhachdonvi); string vaocua = Directory.GetCurrentDirectory() + "\\Resources\\cuaso.mp3"; media5 = mp.newMedia(vaocua); string socua = Directory.GetCurrentDirectory() + "\\Resources\\" + ra + ".mp3"; media6 = mp.newMedia(socua); playlist.appendItem(media); playlist.appendItem(media1); playlist.appendItem(media2); playlist.appendItem(media3); playlist.appendItem(media4); playlist.appendItem(media5); playlist.appendItem(media6); mp.currentPlaylist = playlist; mp.controls.play(); clients.Remove(num); Thread.Sleep(7000); //sleep 10s đợi phát âm thanh gọi khách trước xong. } } catch { } } } }
public static void test2() { WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer(); WMPLib.IWMPMedia media = player.newMedia("ftp://90.184.75.15/OpenShare/Music/Morten/Deadmau5/Random Album Title/07 I Remember.mp3"); player.NewStream += new WMPLib._WMPOCXEvents_NewStreamEventHandler(player_NewStream); player.controls.playItem(media); }
/// <summary> /// 播放音效 /// </summary> /// <param name="key">按键</param> static void Play(string key) { //得到当前exe所在路径 var path = Environment.CurrentDirectory; //实例化播放器 var player = new WMPLib.WindowsMediaPlayer(); //设定播放器播放音乐文件,分别为1到7的文件名,扩展名为.wma player.URL = $"{path}/media/{key}.wma"; //获取音乐文件时长(秒) Console.WriteLine(player.newMedia($"{path}/media/{key}.wma").duration); }
private void PlaylistMidday(String folder, string extendsion) { WMPLib.WindowsMediaPlayer wplayer1 = new WMPLib.WindowsMediaPlayer(); WMPLib.IWMPPlaylist pl; WMPLib.IWMPPlaylistArray plItems; string myPlaylist = txtPlaylistname.Text; ListView musicList = new ListView(); plItems = wplayer1.playlistCollection.getByName(myPlaylist); if (plItems.count == 0) { pl = wplayer1.playlistCollection.newPlaylist(myPlaylist); } else { pl = plItems.Item(0); } DirectoryInfo dir = new DirectoryInfo(folder); FileInfo[] files = dir.GetFiles(extendsion, SearchOption.AllDirectories); foreach (FileInfo file in files) { string musicFile01 = file.FullName; string mName = file.Name; ListViewItem item = new ListViewItem(mName); musicList.Items.Add(item); WMPLib.IWMPMedia m1 = wplayer1.newMedia(musicFile01); pl.appendItem(m1); } wplayer1.currentPlaylist = pl; wplayer1.settings.setMode("shuffle", true); // this does the trick wplayer1.controls.play(); }
public void playListMusic(String path) { try { WMPLib.IWMPPlaylist playlist = wplayer.playlistCollection.newPlaylist("GirlPlay2"); DirectoryInfo dic = new DirectoryInfo(path); List <FileInfo> files = new List <FileInfo>(); files.AddRange(dic.EnumerateFiles()); foreach (FileInfo item in files) { WMPLib.IWMPMedia media; media = wplayer.newMedia(item.FullName); playlist.appendItem(media); } wplayer.currentPlaylist = playlist; wplayer.controls.play(); } catch (Exception ex) { MessageBox.Show("Error play music: "); } }
public void CreatePlayList() { WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); WMPLib.IWMPPlaylist playlist = wplayer.playlistCollection.newPlaylist("playlist"); //Find media source var medias = EnumMusicSource(); foreach (var m in medias) { try { //add media var media = wplayer .newMedia($"{Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)}\\{m}"); playlist.appendItem(media); } catch (Exception ex) { Console.WriteLine(ex); } } }
static void WalkDirectoryTree(System.IO.DirectoryInfo root) { System.IO.FileInfo[] files = null; System.IO.DirectoryInfo[] subDirs = null; WMPLib.WindowsMediaPlayer w = new WMPLib.WindowsMediaPlayer(); try { files = root.GetFiles("*.*"); } // This is thrown if even one of the files requires permissions greater // than the application provides. catch (UnauthorizedAccessException e) { log.Add(e.Message); } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); } if (files != null) { foreach (System.IO.FileInfo fi in files) { if (fi.Extension.ToLower() == ".mp3") { Console.WriteLine(fi.FullName); WMPLib.IWMPMedia m = w.newMedia(fi.FullName); if (m != null || m.duration != null) { if (m.duration >= dblMinDuration) { sourceMp3Files.Add(fi); } } } } // Now find all the subdirectories under this directory. subDirs = root.GetDirectories(); foreach (System.IO.DirectoryInfo dirInfo in subDirs) { // Resursive call for each subdirectory. WalkDirectoryTree(dirInfo); } } w.close(); }
static void Main() { //add play state change event handler player.PlayStateChange += player_PlayStateChange; //attempt to read the list of registered folders from the stored configuration. try { int x = Properties.Settings.Default.RegisteredFolders.Count; } catch { Properties.Settings.Default.RegisteredFolders = new StringCollection(); } //error handling if the list is empty discoveredfiles = fileSearch.PerformScan(); //perform inital scan for media if (Properties.Settings.Default.ArduinoAutostart) { screen.Activate(); //autostart the screen if we need to } for (; ;) { Console.Write(">"); string[] command = CommandLineToArgs(Console.ReadLine()); if (command.Length == 0) { continue; } string commandupper = command[0].ToUpper(); if (commandupper == "HELP" || commandupper == "H") { foreach (string line in new string[] { "List of commands:", "\tplay / pl", "\tstop / s", "\tpause / pa", "\tloop / l", "", "\tpos / ?", "\tgoto / g", "", "\tlist / ls", "\tmanage_folders / mf", "\tsettings / se", "\thelp / h", "\tquit / q" }) { Console.WriteLine(line); } } else if (commandupper == "LIST" || commandupper == "LS") { if (discoveredfiles.Count == 0) { functions.Error("No music is in your library. :(\nType \"manage_folders\" to add a folder containing music."); continue; } Console.Write("Filename"); Console.Write(String.Concat(Enumerable.Repeat(" ", 50 - 8))); Console.WriteLine("Artist name"); foreach (string file in discoveredfiles) { var media = player.newMedia(file); string filename = TextCaseConverter.ToTitleCase(Path.GetFileName(file)); string line = ""; for (int i = 0; i <= 50; i++) { //print the first 20 characters of the song name and pad any extra space try { line += filename[i]; } catch { line += " "; } } line += " "; //padding between title and authour line += media.getItemInfo("Author"); Console.WriteLine(line); } } else if (commandupper == "PLAY" || commandupper == "PL") { if (command.Length == 1) { player.controls.play(); } else { string medianame = command[1]; //get media name parameter if (fileSearch.IsElementInList(medianame, discoveredfiles)) //check if the file exists { string filepath = fileSearch.FindMediaPath(medianame, discoveredfiles); Console.WriteLine("Playing " + filepath + "..."); try { player.URL = filepath; player.controls.play(); } catch (Exception err) { functions.Error("Error playing file: " + err.Message); } } else { functions.Error("Error playing file: File doesn't exist."); } } } else if (commandupper == "STOP" || commandupper == "S") { player.controls.stop(); } else if (commandupper == "PAUSE" || commandupper == "PA") { player.controls.pause(); } else if (commandupper == "LOOP" || commandupper == "L") { LoopEnabled = !LoopEnabled; //toggle LoopEnabled. player.settings.setMode("loop", LoopEnabled); //tell the player to loop the media. //output result Console.Write("Looping is "); if (LoopEnabled) { Console.WriteLine("ENABLED."); } else { Console.WriteLine("DISABLED."); } } else if (commandupper == "POS" || commandupper == "?") { if (functions.IsMediaSelected()) { Console.WriteLine(functions.CreateBarFromMediaInfo()); } else { functions.Error("No media is loaded."); } } else if (commandupper == "INFO" || commandupper == "I") { if (player.currentMedia == null) { functions.Error("No media is loaded."); continue; } for (int i = 0; i < player.currentMedia.attributeCount; i++) { var name = player.currentMedia.getAttributeName(i); var value = player.currentMedia.getItemInfo(name); if (value.Length == 0) { continue; } Console.WriteLine(name + ": " + value); } } else if (commandupper == "GOTO" || commandupper == "G") { string syntax = "Syntax: [hh:][mm:]ss"; if (!functions.IsMediaSelected()) { functions.Error("No media is loaded."); continue; } if (command.Length < 2) { functions.Error(syntax); continue; } string[] strtimeargs = command[1].Split(Char.Parse(":")); if (strtimeargs.Length < 1 || strtimeargs.Length > 3) { functions.Error(syntax); continue; } List <double> timeargs = new List <double>(); timeargs = functions.ConvertTimeargsToInt(strtimeargs); //parse into seconds float seconds = 0; int multiplier = 1; //multiply the item by this foreach (int unit in timeargs) { seconds += unit * multiplier; multiplier *= 60; //multiple the multipler by 60 (sequence is 1,60,3600) } //is this longer than the length of the song? if (seconds > player.currentMedia.duration) { functions.Error("Timecode exceeds song length."); continue; } //or is it smaller than 0 ? if (seconds < 0) { functions.Error("Timecode is below 0 seconds."); continue; } //go to the position and draw a progress bar. player.controls.currentPosition = seconds; Console.WriteLine(functions.CreateBarFromMediaInfo()); } else if (commandupper == "MANAGE_FOLDERS" || commandupper == "MF") { Console.WriteLine("Entering folder manager...\nType help for list of commands."); for (; ;) { Console.Write(":"); command = CommandLineToArgs(Console.ReadLine()); if (command.Length == 0) { continue; } commandupper = command[0].ToUpper(); if (commandupper == "HELP" || commandupper == "H") { foreach (string line in new string[] { "List of commands:", "\tlist / l / ls", "\tadd \"[folderpath]\" / a \"[folderpath]\"", "\tdel [folderid] / d [folderid]", "\treg_app_dir / x", "", "\thelp / h", "\texit / e / q", "**Changes are saved upon executing the \"exit\" command**" }) { Console.WriteLine(line); } } else if (commandupper == "LIST" || commandupper == "L" || commandupper == "LS") { Console.WriteLine("List of registered folders:"); ListRegisteredFolders(); if (Properties.Settings.Default.RegisterAppFolder) { Console.WriteLine("* " + AppDomain.CurrentDomain.BaseDirectory); } } else if (commandupper == "ADD" || commandupper == "A") { string folderpath = ""; //check if a folder parameter was specified if (command.Length > 1) { //there is - attempt to add it. folderpath = command[1]; Console.WriteLine("Adding '" + folderpath + "'"); } else { //no parameter was specified; query for folderpath to add Console.Write("Enter the full filepath to the folder below.\nMake sure to include the drive letter.\n?"); folderpath = Console.ReadLine(); } if (!(Directory.Exists(folderpath))) { functions.Error("Folder doesn't exist."); continue; } //add the path if it doesn't already exist. try { if (Properties.Settings.Default.RegisteredFolders.Contains(folderpath)) { functions.Error("Folder already registered."); } else { Properties.Settings.Default.RegisteredFolders.Add(folderpath); functions.Success("Successfully registered folder!"); } } catch { functions.Error("Internal error."); } } else if (commandupper == "DEL" || commandupper == "D") { string userinput = ""; int folderid; //has the user already specified the folder id as an argument? if not, ask them for the id. if (command.Length == 1) { ListRegisteredFolders(); Console.Write("Enter folder id to de-register: "); userinput = Console.ReadLine(); } else { userinput = command[1]; } try { folderid = Convert.ToInt16(userinput); } catch { functions.Error("Invalid input: Must be a number."); continue; } if (folderid > Properties.Settings.Default.RegisteredFolders.Count - 1) { functions.Error("Folder ID exceeds maximum ID."); } Properties.Settings.Default.RegisteredFolders.RemoveAt(folderid); functions.Success("De-registered folder #" + folderid); } else if (commandupper == "REG_APP_DIR" || commandupper == "X") { Console.Write("Use of the app directory for music is now "); bool enabled = !Properties.Settings.Default.RegisterAppFolder; Properties.Settings.Default.RegisterAppFolder = enabled; if (enabled) { functions.Success("enabled."); } else { functions.Error("disabled."); } } else if (commandupper == "EXIT" || commandupper == "E" || commandupper == "Q") { break; } else { functions.Error("Command unknown. Type 'help' for a list of commands."); } } functions.SaveConfiguration(); Console.WriteLine("Rescanning for media..."); discoveredfiles = fileSearch.PerformScan(); //perform another scan for media } else if (commandupper == "SETTINGS" || commandupper == "SE") { Console.WriteLine("Entering settings menu...\nType help for list of commands."); for (; ;) { Console.Write(":"); command = CommandLineToArgs(Console.ReadLine()); if (command.Length == 0) { continue; } commandupper = command[0].ToUpper(); if (commandupper == "HELP" || commandupper == "H") { foreach (string line in new string[] { "List of commands:", "\tset_serial_port / com", "\ttoggle_arduino / ta", "\tautostart_arduino / as", "\tset_back_colour / bc", "\tset_text_colour / tc", "", "\thelp / h", "\texit / e / q", "**Changes are saved upon executing the \"exit\" command**" }) { Console.WriteLine(line); } } else if (commandupper == "SET_SERIAL_PORT" || commandupper == "COM") { //for every com port, add it to the list of options. Console.WriteLine("List of COM ports:"); List <int> comPorts = new List <int>(); foreach (string item in System.IO.Ports.SerialPort.GetPortNames()) { comPorts.Add(Convert.ToInt16(item.Substring(3))); } comPorts.Sort(); foreach (int portid in comPorts) { Console.WriteLine("\tCOM" + portid); } Console.Write("Please select a COM port from the provided list: "); try { Properties.Settings.Default.ArduinoPort = Convert.ToInt16(Console.ReadLine().Replace("COM", "")); } catch (Exception err) { functions.Error("Invalid input. " + err.Message); continue; } //Properties.Settings.Default.ArduinoPort = ; } else if (commandupper == "TOGGLE_ARDUINO" || commandupper == "TA") { if (screen.serial.IsOpen) { screen.Enabled = !screen.Enabled; //invert state } else { screen.Activate(); //initial activation. } //output change Console.Write("Arduino interface is "); if (screen.Enabled) { functions.Success("enabled."); } else { functions.Error("disabled."); } } else if (commandupper == "AUTOSTART_ARDUINO" || commandupper == "AS") { Properties.Settings.Default.ArduinoAutostart = !Properties.Settings.Default.ArduinoAutostart; //invert state //output change Console.Write("Autostart of Arduino interface is "); if (Properties.Settings.Default.ArduinoAutostart) { functions.Success("enabled."); } else { functions.Error("disabled."); } } else if (commandupper == "SET_BACK_COLOUR" || commandupper == "BC") { } else if (commandupper == "SET_BACK_COLOUR" || commandupper == "TC") { } else if (commandupper == "EXIT" || commandupper == "E" || commandupper == "Q") { break; } else { functions.Error("Command unknown. Type 'help' for a list of commands."); } } } else if (commandupper == "TOGGLE_FILECHECK" || commandupper == "TF") { //just for fun: a hidden command to disable checking of existence of a file. //this could allow for lots of possible input, such as http addresses pointing to media. bypassfilecheck = !bypassfilecheck; //invert state if (bypassfilecheck) { functions.Warning("Warning: Filechecks have been disabled. This can result in unexpected behavior."); //obligatory warning message } else { functions.Success("Filechecks enabled."); } } else if (commandupper == "QUIT" || commandupper == "Q" || commandupper == "E") { Environment.Exit(0); } else { functions.Error("Command unknown. Type 'help' for a list of commands."); } } }
private int getAudioDuration(string filename) { int duration; WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer(); WMPLib.IWMPMedia m = wmp.newMedia(filename); duration = Convert.ToInt32(m.duration); duration = duration * 1000; return duration; }
public static void ReproduzirArquivo(object parameter) { idle = false; List<String> arquivos = (List<String>)parameter; _wplayer = new WMPLib.WindowsMediaPlayer(); var _playlist = _wplayer.playlistCollection.newPlaylist("playlist"); foreach (var arquivo in arquivos) { WMPLib.IWMPMedia media = _wplayer.newMedia(Path.Combine(Path.GetTempPath(), arquivo)); _playlist.appendItem(media); } _wplayer.currentPlaylist = _playlist; _wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(_wplayer_PlayStateChange); _wplayer.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(_wplayer_MediaError); _wplayer.controls.play(); }