Visual Basic 中学校 掲示板 投稿の管理
タグのない投稿を抽出
統計
RSS
Visual Basic 中学校
投稿一覧
音楽について
この投稿へのリンク
https://keijiban.umayadia.com/ThreadDetail.aspx?ThreadId=9699#CommentId15238
この投稿の削除
削除パスワード
削除する
コメント本文
投稿者
YAS
 (社会人)
投稿日時
2010/7/19 21:55:56
ファイルを開くダイアログを追加したサンプルは次のようになると思います。
Option Explicit On
Option Strict On
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.IO
Public Class Form1
<DllImport("winmm.dll", CharSet:=CharSet.Auto)> _
Private Shared Function mciSendString( _
ByVal command As String, _
ByVal buffer As StringBuilder, _
ByVal bufferSize As Integer, _
ByVal hwndCallback As IntPtr) As Integer
End Function
Dim WithEvents Button1 As New Button
Dim WithEvents Button2 As New Button
Dim WithEvents Button3 As New Button
Dim WithEvents TrackBar1 As New TrackBar
Dim WithEvents Timer1 As New Timer
Dim WithEvents OpenFileDialog1 As New OpenFileDialog
Dim FileName As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button1.Text = "再生"
Me.Button2.Text = "停止"
Me.Button3.Text = "開く"
Me.Button2.Location = New Point(80, 0)
Me.Button3.Location = New Point(160, 0)
Me.TrackBar1.Location = New Point(0, 30)
Me.TrackBar1.Width = Me.ClientSize.Width
Me.Controls.AddRange(New Control() {Me.Button1, Me.Button2, Me.Button3, Me.TrackBar1})
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If File.Exists(FileName) Then
Me.Text = Path.GetFileName(FileName) & "を再生中..."
Dim Command As String
Command = String.Format("open ""{0}"" alias {1}", FileName, "MySound")
mciSendString(Command, Nothing, 0, IntPtr.Zero)
Command = String.Format("status {0} {1}", "MySound", "length")
Dim Res As New StringBuilder(256)
mciSendString(Command, Res, Res.Capacity, IntPtr.Zero)
TrackBar1.Maximum = Integer.Parse(Res.ToString)
Command = "play MySound"
mciSendString(Command, Nothing, 0, IntPtr.Zero)
Me.Timer1.Interval = 500
Me.Timer1.Start()
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Stop()
Me.Text = Path.GetFileName(FileName) & "を停止中..."
Dim Command As String
Command = String.Format("stop {0}", "MySound")
mciSendString(Command, Nothing, 0, IntPtr.Zero)
Command = String.Format("close {0}", "MySound")
mciSendString(Command, Nothing, 0, IntPtr.Zero)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.OpenFileDialog1.Filter = "サウンドファイル(*.wav)|*.wav"
Me.OpenFileDialog1.InitialDirectory = Application.StartupPath
If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
FileName = Me.OpenFileDialog1.FileName
Me.Text = Path.GetFileName(FileName)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Command As String = String.Format("status {0} {1}", "MySound", "position")
Dim Res As New StringBuilder(256)
mciSendString(Command, Res, Res.Capacity, IntPtr.Zero)
Me.TrackBar1.Value = Integer.Parse(Res.ToString)
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim TrackBar As TrackBar = DirectCast(sender, TrackBar)
Dim Command As String
Command = String.Format("seek {0} to {1}", "MySound", TrackBar.Value)
mciSendString(Command, Nothing, 0, IntPtr.Zero)
Command = String.Format("play {0}", "MySound")
mciSendString(Command, Nothing, 0, IntPtr.Zero)
End Sub
End Class