private void StopService() { _searchThread?.Abort(); _searchThread = null; _globalTimer.Stop(); _socketService?.StopService(); _socketService = null; _socketThread?.Abort(); _socketThread = null; if (LyricForm.Instance.Opacity != 0) { LyricForm.Instance.Close(); } // <<< btnListen.Text = "开始监听"; cbbInterface.Enabled = true; // 网络监听 numPort.ReadOnly = false; btnQrcode.Enabled = false; lblDuration.Text = "未监听..."; // 当前歌曲 btnAdjustOffset.Enabled = false; lblTitle.Visible = lblTitleHint.Visible = false; lblArtist.Visible = lblArtistHint.Visible = false; lblAlbum.Visible = lblAlbumHint.Visible = false; lblLink.Visible = lblLinkHint.Visible = false; ckbPauseIgnore.Enabled = false; // 下方按钮 ckbPauseIgnore.Checked = false; btnShowLyric.Enabled = false; btnVisitLink.Enabled = false; Global.IsListening = false; Global.Pausing = false; Global.CurrentMetadata = null; Global.CurrentState = null; Global.CurrentPosition = 0; Global.CurrentMusicId = -1; }
private void StartService() { _socketService = new SocketService { listenCallback = (ok) => Invoke(new Action(() => { if (!ok) { MessageBox.Show("监听失败,可能因为端口被占用。", "监听", MessageBoxButtons.OK, MessageBoxIcon.Error); StopService(); return; } Properties.Settings.Default.NetworkInterface = (string)cbbInterface.SelectedItem; Properties.Settings.Default.Port = (int)numPort.Value; Properties.Settings.Default.Save(); Global.IsListening = true; Global.Pausing = false; Global.CurrentMetadata = null; Global.CurrentState = null; Global.CurrentPosition = 0; Global.CurrentMusicId = -1; // <<< btnListen.Text = "结束监听"; cbbInterface.Enabled = false; // 网络监听 numPort.ReadOnly = true; btnQrcode.Enabled = true; lblDuration.Text = "正在等待歌曲..."; // 当前歌曲 btnAdjustOffset.Enabled = false; lblTitle.Text = "未知标题"; lblAlbum.Text = "未知专辑"; lblArtist.Text = "未知歌手"; lblLink.Text = "未知链接"; lblTitle.Visible = lblTitleHint.Visible = false; lblArtist.Visible = lblArtistHint.Visible = false; lblAlbum.Visible = lblAlbumHint.Visible = false; lblLink.Visible = lblLinkHint.Visible = false; ckbPauseIgnore.Enabled = true; // 下方按钮 ckbPauseIgnore.Checked = false; btnShowLyric.Enabled = false; btnVisitLink.Enabled = false; })), pingCallback = () => Invoke(new Action(() => { Console.WriteLine("pong"); Utils.CloseBitmapForm(); })), metadataCallback = (o) => Invoke(new Action(() => SocketMetadataCallback(o))), playbackStateCallback = (o) => Invoke(new Action(() => SocketPlaybackStateCallback(o))), sessionDestroyedCallback = () => Invoke(new Action(() => { if (ckbAutoDisconnect.Checked) { StopService(); MessageBox.Show("安卓端的连接已经断开。", "监听", MessageBoxButtons.OK, MessageBoxIcon.Information); } })), }; int port = (int)numPort.Value; _socketThread = new Thread(() => _socketService.StartAndRunService(port)) { IsBackground = true }; _socketThread.Start(); }