// Returns a currently playing object with all of the available information on the currently playing track private currentlyplaying get_Currently_Playing() { if (oAuthToken != "") { // Create an api request var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.spotify.com/v1/me/player/currently-playing?market=ES"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Accept = "application/json"; httpWebRequest.Method = "GET"; httpWebRequest.Headers.Add("Authorization", "Bearer " + txt_accessToken.Text); // oAuthToken); try { var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { // Parse the returned json to a c# object string response = streamReader.ReadToEnd(); currentlyplaying currentlyplaying_ = Newtonsoft.Json.JsonConvert.DeserializeObject <currentlyplaying>(response); return(currentlyplaying_); } } catch (Exception ex) { MessageBox.Show(ex.Message); return(null); } } else { MessageBox.Show("Access token required."); return(null); } }
private void update_currently_playing() { // Get curretly playing object currentlyplaying_ = get_Currently_Playing(); if (currentlyplaying_ != null) { if (currentlyplaying_.item != null) { // Set Track name label to track name lbl_cTrackName.Invoke((MethodInvoker) delegate { lbl_cTrackName.Text = currentlyplaying_.item.name; }); // Put list of artists into a string and set as text for Track artists label lbl_cTrackArtist.Invoke((MethodInvoker) delegate { lbl_cTrackArtist.Text = String.Join(", ", currentlyplaying_.item.artists.Select(p => p.name).ToArray()); }); try { // Display cover in picture box pic_Cover.Invoke((MethodInvoker) delegate { pic_Cover.Load(currentlyplaying_.item.album.images[0].url); }); } catch (Exception) { throw; } } } }
// Inserts song into database public void insertSong(currentlyplaying currentlyplaying_, string path_, bool downloaded_, DateTime dateTime_) { var client = new MongoClient("mongodb://localhost:27017"); var db = client.GetDatabase(database_name); var coll = db.GetCollection <song>(collection_name); var document = new song(); document.song_info = currentlyplaying_; document.local_Info = new song.Local_info(); document.local_Info.path = path_; document.local_Info.downloaded = downloaded_; document.local_Info.time = dateTime_; coll.InsertOne(document); }
// Method to convert wav to mp3 and tag the mp3 and also delete original wav file void convertTag(string path, string filename, currentlyplaying cp) { // This converts the wav to mp3 var proc = new Process { StartInfo = new ProcessStartInfo { // C:\ffmpeg.exe -i 0A5gdlrpAuQqZ2iFgnqBFW.wav 0A5gdlrpAuQqZ2iFgnqBFW.mp3 FileName = @"C:\Windows\SysWOW64\cmd.exe", Arguments = @"/c C:\ffmpeg.exe -i " + path + "\\" + filename + ".wav " + path + "\\" + filename + ".mp3", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true, WorkingDirectory = path } }; proc.Start(); proc.WaitForExit(); proc.Close(); // Delete the original wav file File.Delete(path + "\\" + filename + ".wav"); // Tag the mp3 TagLib.Id3v2.Tag.DefaultVersion = 3; TagLib.Id3v2.Tag.ForceDefaultVersion = true; TagLib.File file = TagLib.File.Create(path + "\\" + filename + ".mp3"); SetAlbumArt(cp.item.album.images[0].url, file, path, filename); file.Tag.Title = cp.item.name; // Get artists string[] artists = new string[cp.item.artists.Count]; int i = 0; foreach (currentlyplaying.Artist2 artist in cp.item.artists) { artists[i] = artist.name; i++; } file.Tag.Performers = artists; file.Tag.Album = cp.item.album.name; file.Tag.Track = (uint)cp.item.track_number; file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(cp.item.album.release_date, @"(\d)(\d)(\d)(\d)").Value); // MessageBox.Show(cp.item.album.release_date.Split('-')[0]); file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk); file.Save(); if (useLocalDB) { DB_Handler.insertSong(cp, path + "\\" + filename + ".mp3", true, DateTime.Now); } }
void convertTagAsynch(string path, string filename, currentlyplaying cp) { Task.Run(() => convertTag(path, filename, cp)); }
void loopRecord() { // Get current window title of active window string title = GetWindowTitle(); // Wait for the title to change, check 10 times per second while (title == GetWindowTitle() || GetWindowTitle() == "Advertisement" || GetWindowTitle() == "Spotify") { Thread.Sleep(100); } updateWindowNameDisplay(); btn_toggleRecord.Invoke((MethodInvoker) delegate { btn_toggleRecord.Text = "Recording..."; btn_toggleRecord.BackColor = Color.LightGreen; btn_toggleRecord.ForeColor = Color.White; }); while (!stopRecording) { using (WasapiCapture capture = new WasapiLoopbackCapture()) { currentlyplaying cp = null; while (cp == null) { cp = get_Currently_Playing(); } if (cp.item != null) { string filename = cp.item.id; // GetWindowTitle(); //rtxt_songlist.Invoke((MethodInvoker)delegate { // // Running on the UI thread // rtxt_songlist.Text += filename + "\n"; //}); // rtxt_songlist.Text += filename + "\n"; //foreach (char c in System.IO.Path.GetInvalidFileNameChars()) //{ // filename = filename.Replace(c, '_'); //} //initialize the selected device for recording capture.Initialize(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //create a wavewriter to write the data to using (WaveWriter w = new WaveWriter(path + "\\" + filename + ".wav", capture.WaveFormat)) { //setup an eventhandler to receive the recorded data capture.DataAvailable += (s, E) => { //save the recorded audio w.Write(E.Data, E.Offset, E.ByteCount); }; //start recording capture.Start(); //for (int i = 0; i < 100; i++) //{ // Thread.Sleep(time / 100); // prog_recording.Value = 1 * i; //} // Get current window title of active window string newTitle = GetWindowTitle(); // Wait for the title to change, check 10 times per second while (newTitle == GetWindowTitle()) { Thread.Sleep(100); updateWindowNameDisplay(); } //stop recording capture.Stop(); updateWindowNameDisplay(); while (GetWindowTitle() == "Advertisement" || GetWindowTitle() == "Spotify") { Thread.Sleep(100); updateWindowNameDisplay(); } convertTagAsynch(path, filename, cp); // Thread.Sleep(time); } } } if (title == GetWindowTitle()) { stopRecording = true; } } btn_toggleRecord.Invoke((MethodInvoker) delegate { btn_toggleRecord.Text = "Record"; btn_toggleRecord.BackColor = Color.FromArgb(30, 30, 30); btn_toggleRecord.ForeColor = Color.White; }); }