예제 #1
0
        private async void Initialize(string filePath)
        {
            Workspace.Instance.SetStatus(TaskStatusType.Loading, $"Loading a file... ({filePath})"); // Set status.

            // Load file.
            string loadedFilePath = await FileLoadAsync(filePath);

            try
            {
                if (loadedFilePath == null)
                {
                    OnClose();
                    return;
                }
                else
                {
                    // Initialize AudioPlayback and Visualizations.
                    _visualizations        = ReflectionHelper.CreateAllInstancesOf <IVisualizationPlugin>().ToList();
                    _selectedVisualization = _visualizations.FirstOrDefault();

                    _audioPlayback = new AudioPlayback();
                    _audioPlayback.MaximumCalculated += OnAudioGraphMaximumCalculated;
                    _audioPlayback.FftCalculated     += OnAudioGraphFftCalculated;

                    string extension = Path.GetExtension(filePath).ToLower();

                    if (extension == ".ogg" || extension == ".rpgmvo" || extension == ".ogg_")
                    {
                        _audioPlayback.Load(loadedFilePath, true); // Load audio file using Ogg Vorbis wave reader.
                    }
                    else
                    {
                        _audioPlayback.Load(loadedFilePath);
                    }

                    // Initialize track bar.
                    _trackBarTimer          = new Timer();
                    _trackBarTimer.Interval = 500;
                    _trackBarTimer.Elapsed += new ElapsedEventHandler(OnTrackBarTimerElapsed);

                    _trackBarMaximumValue = _audioPlayback.GetTotalTime();

                    TimeSpan current = TimeSpan.Zero;
                    _trackBarString = $"{current.ToString(@"hh\:mm\:ss")} / {_trackBarMaximumValue.ToString(@"hh\:mm\:ss")}";

                    // Load file properties.
                    _fileProperties = new FileProperties(filePath);
                }
            }
            catch (Exception ex)
            {
                Workspace.Instance.Report.AddReportWithIdentifier($"{ex.Message}\r\n{ex.StackTrace}", ReportType.Warning);
            }
            finally
            {
                Workspace.Instance.SetStatus(TaskStatusType.Completed, $"Completed."); // Set status.
            }
        }
        public void NextAudioFile()
        {
            //Move to next audio file in selected folder
            //Since the audio file is changing the controller will need to be remade

            SelectedPreviousSimSoundFileIndex++;


            if (SelectedPreviousSimSoundFileIndex <= PreviousSimSoundFiles.Length - 1)
            {
                SelectedPreviousSimSoundFile = PreviousSimSoundFiles[SelectedPreviousSimSoundFileIndex];
            }
            else
            {
                SelectedPreviousSimSoundFileIndex = 0;
                SelectedPreviousSimSoundFile      = PreviousSimSoundFiles[SelectedPreviousSimSoundFileIndex];
            }

            //Reload the Audio controller since we have most likely changed sound file
            SetAudioController(SelectedPreviousSimSoundFile);
            audioPlayback.Load(SelectedPreviousSimSoundFile);
        }
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                Title      = _title;
                NeteaseUrl = "http://music.163.com/#/my/m/music/playlist?id=6435531";
                SongCollection.Add(new Song()
                {
                    Title   = new Title("Clover Heart's"),
                    Artists = new Artists(new string[] { "榊原ゆい", "榊原ゆい" }),
                    Album   = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"),
                });
                SongCollection.Add(new Song()
                {
                    Title   = new Title("Clover Heart's"),
                    Artists = new Artists(new string[] { "榊原ゆい" }),
                    Album   = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"),
                });
                SongCollection.Add(new Song()
                {
                    Title   = new Title("Clover Heart's"),
                    Artists = new Artists(new string[] { "榊原ゆい" }),
                    Album   = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"),
                });
                SelectedSong = SongCollection.First();
            }
            else
            {
                // Code runs "for real"
                Title                      = _title;
                NeteaseUrl                 = "http://music.163.com/playlist?id=6435531";
                Progress                   = 0;
                audioPlayback              = new AudioPlayback();
                audioPlayback.Volume       = Volume;
                audioPlayback.EndCallback += (handle, channel, data, user) =>
                {
                    int index = -1;
                    if ((index = SongCollection.IndexOf(CurrentPlaySong)) != -1)
                    {
                        ListenCommand.Execute(SongCollection.ElementAt((index + 1) % SongCollection.Count));
                    }
                    else
                    {
                        audioPlayback.Stop();
                        NowPlaying = "";
                        CurrentPlaySong.PlayProgress = 0;
                        CurrentPlaySong.PlayStatus   = PlayStatus.Play;
                        timer.Enabled = false;
                        timer.Stop();
                    }
                };

                timer.Tick += (sender, args) =>
                {
                    Title = string.Format("{0}/{1} - {2}", audioPlayback.CurrentLength, audioPlayback.TotalLength, _title);
                    RaisePropertyChanged("Title");
                    CurrentPlaySong.PlayProgress = audioPlayback.Progress;
                };

                GetSongsCommand = new RelayCommand(async() =>
                {
                    if (string.IsNullOrWhiteSpace(NeteaseUrl))
                    {
                        return;
                    }
                    var id      = "";
                    var urlType = "";
                    var reg     = new Regex(@".*/(.*?)\?id=(\d*)").Match(NeteaseUrl);
                    if (reg.Success)
                    {
                        urlType = reg.Groups[1].Value;
                        id      = reg.Groups[2].Value;
                        SongCollection.Clear();
                        switch (urlType)
                        {
                        case "album":
                            foreach (var song in await NeteaseUtil.GetSongsFromAlbum(id))
                            {
                                SongCollection.Add(song);
                                RaisePropertyChanged("TotalCount");
                            }
                            break;

                        case "artist":
                            foreach (var song in await NeteaseUtil.GetSongsFromArtist(id))
                            {
                                SongCollection.Add(song);
                                RaisePropertyChanged("TotalCount");
                            }
                            break;

                        case "playlist":
                            foreach (var song in await NeteaseUtil.GetSongsFromPlaylist(id))
                            {
                                SongCollection.Add(song);
                                RaisePropertyChanged("TotalCount");
                            }
                            break;

                        case "song":
                            foreach (var song in await NeteaseUtil.GetSongDetail(id))
                            {
                                SongCollection.Add(song);
                                RaisePropertyChanged("TotalCount");
                            }
                            break;
                        }
                    }
                    else
                    {
                        SongCollection.Clear();
                        foreach (var song in await NeteaseUtil.SearchSongs(NeteaseUrl))
                        {
                            SongCollection.Add(song);
                            RaisePropertyChanged("TotalCount");
                        }
                    }
                });

                PlaylistDownloadCommand = new RelayCommand <Song>((song) =>
                {
                    SelectedSong = song;
                    if (string.IsNullOrWhiteSpace(SongTrackUrl))
                    {
                        return;
                    }
                    var downloader = new DownloadUtils();
                    downloader.DownloadProgressChanged += (sender, args) =>
                    {
                        song.DownProgress   = args.ProgressPercentage;
                        BytesReceived       = args.BytesReceived.ToString();
                        TotalBytesToReceive = args.TotalBytesToReceive.ToString();
                    };
                    downloader.DownloadFileCompleted += (sender, args) =>
                    {
                        if (DownloadNext)
                        {
                            int index = -1;
                            if ((index = SongCollection.IndexOf(song)) != -1 && index + 1 < SongCollection.Count)
                            {
                                PlaylistDownloadCommand.Execute(SongCollection.ElementAt(index + 1));
                            }
                        }
                        else
                        {
                            BytesReceived       = "0";
                            TotalBytesToReceive = "0";
                        }
                    };
                    downloader.Get(SongTrackUrl, Path.Combine("music", FileUtils.GetSafeFileName(song.SongFileName)));
                });

                ListenCommand = new RelayCommand <Song>((song) =>
                {
                    SelectedSong = song;
                    if (string.IsNullOrWhiteSpace(SongTrackUrl))
                    {
                        return;
                    }
                    if (song.PlayStatus == PlayStatus.Play)
                    {
                        if (CurrentPlaySong != null && CurrentPlaySong != song)
                        {
                            audioPlayback.Stop();
                            CurrentPlaySong.PlayProgress = 0;
                            CurrentPlaySong.PlayStatus   = PlayStatus.Play;
                        }

                        audioPlayback.Load(SongTrackUrl);
                        audioPlayback.Play();

                        CurrentPlaySong = song;
                        NowPlaying      = string.Format("Now Playing {0} - {1}", song.Artists, song.Title);
                        timer.Enabled   = true;
                        timer.Start();
                        song.PlayProgress = 0;
                        song.PlayStatus   = PlayStatus.Stop;
                    }
                    else
                    {
                        audioPlayback.Stop();
                        NowPlaying        = "";
                        song.PlayStatus   = PlayStatus.Play;
                        song.PlayProgress = 0;
                        timer.Enabled     = false;
                        timer.Stop();
                    }
                });

                OpenUrlCommand = new RelayCommand <string>((link) =>
                {
                    System.Diagnostics.Process.Start(link);
                });

                CopyUrlCommand = new RelayCommand <string>((link) =>
                {
                    Clipboard.SetText(link);
                });

                WindowClosing = new RelayCommand(() =>
                {
                    Properties.Settings.Default.Save();
                });
            }
        }