Пример #1
0
        static void Main(string[] args)
        {
            MusicFile[] music = new MusicFile[3];

            music[0] = new MusicFile("ButterCup", "SID", 2011, 300);
            music[1] = new MusicFile("Circles", "Post Malone ", 2019, 300);
            music[2] = new MusicFile("Sunflower", "Post Malone", 2011, 160);
            Ipod ipod = new Ipod();

            ipod.Switch(true);
            ipod.Retune(99.6);
            ipod.SetVolume(99);
            ipod.ChangeChannel();

            Console.WriteLine("Switch is " + ipod.On);
            Console.WriteLine("Frequency is " + ipod.Frequency);
            Console.WriteLine("Volume is " + ipod.Loudness);



            for (int i = 0; i < 3; i++)
            {
                ipod.AddMusic(music[i]);
            }
            for (int i = 0; i < 3; i++)
            {
                ipod.PlayNext();
            }

            for (int i = 0; i < 3; i++)
            {
                ipod.PlayPrevious();
            }
        }
Пример #2
0
        public static async Task <MusicFile> FromFileAsync(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }
            var isShortcut = IsShortcut(path);

            if (isShortcut)
            {
                path = ResolveShortcut(path);
            }

            var mf = new MusicFile()
            {
                Filepath = path
            };

            try {
                mf.MetadataFile = await Task.Run(() => TagLib.File.Create(mf.Filepath));

                return(mf);
            } catch {
                // Fails if file from shortcut does not exist
                return(null);
            }
        }
Пример #3
0
        public async void UseSearchQuery(SearchQuery sq)
        {
            LastSearchQuery  = sq;
            SearchInput.Text = sq.ToString();
            SongSource.Clear();

            foreach (var file in await MusicFile.ExecuteSearchQuery(sq))
            {
                SongSource.Add(file);
            }
        }
Пример #4
0
 public void AddMusic(MusicFile music)
 {
     if (this.indexOfMusicFile < 500)
     {
         this.musicFile[this.indexOfMusicFile] = music;
         this.indexOfMusicFile++;
     }
     else
     {
         Console.WriteLine("Not Possible!");
     }
 }
Пример #5
0
        public override async Task OnInitialized()
        {
            MusicFile.FilePlayed          += file => { LastPlayedSong = file; };
            MusicFile.FileFinishedPlaying += async file => {
                if (!_runRadio)
                {
                    return;
                }

                var song = RandomSong;
                await song.PlayAsync();
            };
        }
Пример #6
0
        private async void PlayOrShowSongList(SearchQuery sq)
        {
            var files = await MusicFile.ExecuteSearchQuery(sq);

            if (files.Count == 1)
            {
                await files[0].PlayAsync();
                return;
            }

            UiThread.Invoke(() => {
                _instance.Show();
                _instance.UseSongList(sq, files);
            });
        }
Пример #7
0
        public async Task PlayAsync()
        {
            if (string.IsNullOrWhiteSpace(Filepath))
            {
                return;
            }

            await Task.Run(() => {
                var codec = codecFactory.GetCodec(Filepath);

                soundOut.Stop();
                soundOut.Initialize(codec);
                soundOut.Play();
            });

            LastFilePlayed = this;
            FilePlayed?.Invoke(this);
        }
Пример #8
0
        public static MusicFile FromFile(string path)
        {
            if (IsShortcut(path))
            {
                path = ResolveShortcut(path);
            }

            if (!File.Exists(path) || !ValidExtensions.Contains(Path.GetExtension(path)?.ToLower()))
            {
                return(null);
            }
            var mf = new MusicFile()
            {
                Filepath = path
            };

            mf.MetadataFile = TagLib.File.Create(mf.Filepath);
            return(mf);
        }
Пример #9
0
        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtFilter.Text == "")
            {
                dataGrid.ItemsSource = MusicQueue;
            }

            try
            {
                ListCollectionView collectionView = new ListCollectionView(MusicQueue);
                collectionView.Filter = (ex) =>
                {
                    MusicFile file = ex as MusicFile;
                    if (file.Title.IsMatch(txtFilter.Text))
                    {
                        dataGrid.ItemsSource = collectionView;
                        return(true);
                    }
                    return(false);
                };
            }
            catch (Exception ee) { MessageBox.Show(ee.Message); }
        }
Пример #10
0
        //Project Butler command hook
        public override async Task OnCommandRecieved(Command cmd)
        {
            if (_instance == null)
            {
                UiThread.Invoke(() => { _instance = new SongList(); });
                cmd.Respond("Loading songs..");
                await _instance.LoadAllSongs();

                cmd.Respond("Songs loaded!");
            }

            if (!_instance.Ready)
            {
                cmd.Respond("Music player not ready.");
                return;
            }

            if (cmd.LocalCommand == "List")
            {
                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(SearchQuery.Empty);
                }
                else
                {
                    var output = $"Songs:- \n\n\n";
                    for (var i = 0; i < MusicFile.LoadedFiles.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {MusicFile.LoadedFiles[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "Choose")
            {
                var numInt =
                    int.Parse(RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["choice"].Value);

                if (numInt > MusicFile.LoadedFiles.Count - 1 || numInt < 0)
                {
                    cmd.Respond($"Song #{numInt} does not exist.");
                    return;
                }

                await MusicFile.LoadedFiles[numInt].PlayAsync();
            }

            if (cmd.LocalCommand == "Random")
            {
                await RandomSong.PlayAsync();

                return;
            }

            if (cmd.LocalCommand == "Refresh")
            {
                cmd.Respond("Refreshing..");
                await _instance.RefreshSongs();

                cmd.Respond("Refreshed!");

                return;
            }

            if (cmd.LocalCommand == "Replay")
            {
                if (LastPlayedSong == null)
                {
                    return;
                }

                if (_runRadio)
                {
                    await MusicFile.ResetAnd(async() => { await LastPlayedSong.PlayAsync(); });

                    return;
                }

                await LastPlayedSong.PlayAsync();

                cmd.Respond($"Replaying {LastPlayedSong}");
                return;
            }

            if (cmd.LocalCommand == "Search")
            {
                var query = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["search"].Value;
                var sq    = SearchQuery.Generate(query);

                UiThread.Invoke(() => {
                    _instance.SetSearchQueryString(sq);
                });

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(sq);
                    return;
                }
                else
                {
                    var results = await MusicFile.ExecuteSearchQuery(sq);

                    if (results.Count == 0)
                    {
                        cmd.Respond($"No results found for {query}");
                    }
                    else
                    {
                        var mf = results[0];
                        cmd.Respond($"Playing {mf}");
                        await mf.PlayAsync();
                    }
                    return;
                }
            }

            if (cmd.LocalCommand == "Play Song")
            {
                var song = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["name"].Value.ToLower();

                var mf = MusicFile.LoadedFiles.FirstOrDefault(f => Regex.Match(f.Title.ToLower(), song).Success);
                if (mf == null)
                {
                    cmd.Respond("Song not found.");
                    return;
                }

                await mf.PlayAsync();

                return;
            }

            if (cmd.LocalCommand == "List Artist")
            {
                var artist = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["artist"].Value.ToLower();

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(new SearchQuery()
                    {
                        Artist = artist
                    });
                }
                else
                {
                    var output = $"Songs by {artist}:-\n";
                    var files  = await MusicFile.ExecuteSearchQuery(new SearchQuery()
                    {
                        Artist = artist
                    });

                    for (var i = 0; i < files.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {files[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "List Album")
            {
                var album = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["album"].Value.ToLower();

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(new SearchQuery()
                    {
                        Album = album
                    });
                }
                else
                {
                    var output = $"Songs on {album}:-\n";
                    var files  = await MusicFile.ExecuteSearchQuery(new SearchQuery()
                    {
                        Artist = album
                    });

                    for (var i = 0; i < files.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {files[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "Pause")
            {
                await LastPlayedSong?.PauseAsync();

                return;
            }

            if (cmd.LocalCommand == "Resume")
            {
                await LastPlayedSong?.ResumeAsync();

                return;
            }

            if (cmd.LocalCommand == "Stop")
            {
                await LastPlayedSong?.StopAsync();
            }

            if (cmd.LocalCommand == "Toggle")
            {
                await LastPlayedSong?.ToggleAsync();

                return;
            }

            if (cmd.LocalCommand == "Radio")
            {
                switch (RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["command"].Value.ToLower())
                {
                case "start":
                    await StartRadio();

                    cmd.Respond("Started radio");
                    break;

                case "stop":
                    StopRadio();
                    cmd.Respond("Stopped radio");
                    break;
                }
                return;
            }

            if (cmd.LocalCommand == "Skip Radio Song")
            {
                if (!_runRadio)
                {
                    return;
                }

                await MusicFile.ResetAnd(async() => {
                    var file = RandomSong;
                    await file?.PlayAsync();
                });

                cmd.Respond("Skipping currently playing song");
                return;
            }

            #region Sleep

            if (cmd.LocalCommand == "Sleep")
            {
                var seconds = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["seconds"].Value;
                int secs;
                if (string.IsNullOrWhiteSpace(seconds) || !int.TryParse(seconds, out secs))
                {
                    return;
                }

                _sleepTimer.Interval = secs * 1000;
                _sleepTimer.Elapsed += SleepTimerOnElapsedAsync;
                _sleepTimer.Start();
                cmd.Respond($"Sleeping after {secs} seconds");

                return;
            }

            if (cmd.LocalCommand == "Stop Sleep")
            {
                _sleepTimer?.Stop();
                cmd.Respond($"Stopped scheduled sleep");
                return;
            }

            if (cmd.LocalCommand == "Get Sleep")
            {
                var response = (_sleepTimer.Enabled == false)
                    ? @"Sleep timer is inactive."
                    : $"Remaining time: {_sleepTimer.RemainingTime}";
                cmd.Respond(response);
                return;
            }

            #endregion
        }
Пример #11
0
 private async void StopRadio()
 {
     _runRadio = false;
     await MusicFile.ResetAnd();
 }
Пример #12
0
 private async void SleepTimerOnElapsedAsync(object sender, ElapsedEventArgs elapsedEventArgs)
 {
     await MusicFile.ResetAnd();
 }
Пример #13
0
        private void btnAddTrack_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "";

            string[] fileDirectories;
            TimeSpan duration = new TimeSpan();

            if (Settings.Default.PreloadOnClick && firstOpen)
            {
                try
                {
                    PreloadSongs(Settings.Default.PreloadDirectory);
                    musicProgress.Maximum = MusicQueue[currentSong].Duration.TotalSeconds;
                    lblMaxTime.Content    = MusicQueue[currentSong].Duration.ToString(DefaultSettings.TimeSpanFormat);
                    dataGrid.ItemsSource  = MusicQueue;
                    player.Open(new Uri(MusicQueue[currentSong].FilePath));
                    firstOpen = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Loading Files", "File Loading Error");
                }
            }

            else
            {
                try
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Multiselect      = true;
                    ofd.Filter           = "MP3 Files (*.mp3)|*.mp3";
                    ofd.InitialDirectory = Settings.Default.OpenDirecory;
                    if (ofd.ShowDialog() == true)
                    {
                        fileDirectories = ofd.FileNames;
                        foreach (string fileDir in fileDirectories)
                        {
                            currentDirectory = fileDir;
                            fileName         = FormatFileName(fileDir);
                            duration         = GetMP3Time(fileDir);

                            MusicFile currentNew = new MusicFile(fileName, currentDirectory, duration);
                            MusicQueue.Add(currentNew);
                            musicProgress.Maximum = currentNew.Duration.TotalSeconds + DefaultSettings.Buffer;
                            lblMaxTime.Content    = currentNew.Duration.TotalSeconds.ToString(DefaultSettings.TimeSpanFormat);
                        }

                        dataGrid.SelectedIndex = currentSong;

                        if (firstOpen)
                        {
                            player.Open(new Uri(MusicQueue[currentSong].FilePath));
                        }
                        firstOpen = false;
                    }
                    dataGrid.ItemsSource = MusicQueue;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Пример #14
0
 public Playlist(string name, MusicFile file)
 {
     Name = name;
     MusicList.Add(file);
 }