音楽プレイヤーでリストボックスの曲に戻る時におかしくなります
初めまして質問させていただきます。
音楽プレイヤーを作ってます。
リストボックスで前の曲に戻る時おかしくなりました。
###原因となっているコード
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try ListBox2.SelectedIndex += -1 AxWindowsMediaPlayer2.Ctlcontrols.previous() AxWindowsMediaPlayer2.Ctlcontrols.play() ProgressBar1.Value = 0 Timer1.Start() Timer2.Start() Catch ex As Exception MessageBox.Show("曲がありません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) AxWindowsMediaPlayer2.Ctlcontrols.stop() Timer1.Stop() Timer2.Stop() End Try End Sub
###作成中のform全体
Imports System.IO Imports System Public Class Music00b Private history As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk Dim files = OpenFileDialog1.FileNames Dim conflict = files.Where(AddressOf history.Contains) If conflict.Any() Then e.Cancel = True Dim fileNames = conflict.Select(AddressOf System.IO.Path.GetFileName) MessageBox.Show("同じファイルがあります。" & vbCrLf & String.Join(vbCrLf, fileNames), "競合", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else Array.ForEach(files, AddressOf history.Add) End If End Sub Private Sub Music00b_Load(sender As Object, e As EventArgs) Handles MyBase.Load AxWindowsMediaPlayer2.Hide() TextBox1.ReadOnly = True TextBox2.ReadOnly = True TextBox3.ReadOnly = True TextBox2.ReadOnly = True PictureBox1.Enabled = False PictureBox2.Enabled = False PictureBox3.Enabled = False PictureBox4.Enabled = False PictureBox5.Enabled = False ListBox2.Hide() Label1.Hide() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick ProgressBar1.Value = 0 TextBox2.Text = TimeSpan.FromSeconds(AxWindowsMediaPlayer2.Ctlcontrols.currentPosition).ToString("mm\:ss") Me.Timer1.Enabled = False Me.Timer2.Enabled = True End Sub Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick Dim length As Integer Dim current As Integer length = If(AxWindowsMediaPlayer2.currentMedia?.duration, 0) current = AxWindowsMediaPlayer2.Ctlcontrols.currentPosition ProgressBar1.Value = current ProgressBar1.Maximum = length If ProgressBar1.Value = 100 Then ProgressBar1.Value = 0 End If End Sub Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick TextBox2.Text = TimeSpan.FromSeconds(AxWindowsMediaPlayer2.Ctlcontrols.currentPosition).ToString("mm\:ss") End Sub Dim Open00a As String() Dim file As String() Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click AxWindowsMediaPlayer2.settings.autoStart = False OpenFileDialog1.FileName = "" OpenFileDialog1.Multiselect = True OpenFileDialog1.Title = "開くファイルを指定" OpenFileDialog1.Filter = "MP3(*.mp3)|*.mp3|全てのファイル (*.*)|*.*" OpenFileDialog1.FilterIndex = 1 OpenFileDialog1.RestoreDirectory = True If OpenFileDialog1.ShowDialog() = DialogResult.OK Then file = OpenFileDialog1.SafeFileNames Open00a = OpenFileDialog1.FileNames For b As Integer = 0 To file.Length - 1 ListBox2.Items.Add(file(b)) AxWindowsMediaPlayer2.currentPlaylist.appendItem(AxWindowsMediaPlayer2.newMedia(b)) Next ListBox2.SelectedIndex = 0 TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem) End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click '全てクリアされる history.Clear() ListBox2.Items.Clear() TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() Button2.Enabled = True AxWindowsMediaPlayer2.Ctlcontrols.stop() AxWindowsMediaPlayer2.currentPlaylist.clear() Timer1.Stop() Timer2.Stop() Timer3.Stop() ProgressBar1.Value = 0 MessageBox.Show("リストクリアーしました。", "報告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click AxWindowsMediaPlayer2.Ctlcontrols.play() TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem) Timer1.Start() Timer2.Start() Timer1.Enabled = True End Sub Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click AxWindowsMediaPlayer2.Ctlcontrols.pause() End Sub Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click AxWindowsMediaPlayer2.Ctlcontrols.stop() ProgressBar1.Value = 0 End Sub Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click Try ListBox2.SelectedIndex += -1 AxWindowsMediaPlayer2.Ctlcontrols.previous() AxWindowsMediaPlayer2.Ctlcontrols.play() ProgressBar1.Value = 0 Timer1.Start() Timer2.Start() Catch ex As Exception MessageBox.Show("曲がありません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) AxWindowsMediaPlayer2.Ctlcontrols.stop() Timer1.Stop() Timer2.Stop() End Try End Sub Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click Try ListBox2.SelectedIndex += +1 AxWindowsMediaPlayer2.Ctlcontrols.next() AxWindowsMediaPlayer2.Ctlcontrols.play() ProgressBar1.Value = 0 Timer1.Start() Timer2.Start() Catch ex As Exception MessageBox.Show("曲がありません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Try End Sub Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll AxWindowsMediaPlayer2.settings.volume = HScrollBar1.Value Lb2.Text = "" & HScrollBar1.Value End Sub Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer2.PlayStateChange Try Dim wa As Integer wa = ListBox2.SelectedIndex If AxWindowsMediaPlayer2.currentMedia.isIdentical(AxWindowsMediaPlayer2.currentPlaylist.Item(wa)) Then End If Timer1.Start() Timer2.Start() ProgressBar1.Value = 0 Catch ex As Exception End Try Select Case e.newState Case WMPLib.WMPPlayState.wmppsPlaying '再生中 TextBox3.Text = AxWindowsMediaPlayer2.currentMedia.durationString Timer3.Start() ProgressBar1.Maximum = AxWindowsMediaPlayer2.currentMedia.duration ProgressBar1.Value = 0 Case e.newState = WMPLib.WMPPlayState.wmppsMediaEnded '再生終了 Timer3.Stop() ProgressBar1.Value = 0 Case WMPLib.WMPPlayState.wmppsMediaEnded ' 再生が全て終了した時() AxWindowsMediaPlayer2.Ctlcontrols.stop() Case Else Debug.Print(e.newState.ToString()) End Select End Sub Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged If ListBox2.SelectedItem <> "" Then AxWindowsMediaPlayer2.URL = Open00a(ListBox2.SelectedIndex) TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem) AxWindowsMediaPlayer2.Ctlcontrols.play() End If End Sub End Class
###試した結果
前の曲に戻ると2回押さないとメッセージボックスが出ます。
###教えてほしい事
1回押すだけで前の曲に戻って
メッセージボックスが出るようにするにはどうすればいいでしょうか?
教えていただけますでしょうか、お願い申し上げます。
###使ってるバージョン
Visual Studio 2017のVBです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/02 10:23 編集
2018/04/03 00:23 編集