سلام
من یه فایل صوتی دارم و میخوام که در زمان معینی اون فایل رو پخش کنم و وقتی هم که فایل صوتی پایان یافت فرم بسته بشه و یا اینکه اگه کاربر خواست بتونه با یه دکمه اون فایل رو وتوقف و قطعش کنه.مموون میشم کمک کنید.
Printable View
سلام
من یه فایل صوتی دارم و میخوام که در زمان معینی اون فایل رو پخش کنم و وقتی هم که فایل صوتی پایان یافت فرم بسته بشه و یا اینکه اگه کاربر خواست بتونه با یه دکمه اون فایل رو وتوقف و قطعش کنه.مموون میشم کمک کنید.
بخش زمان رو با یک تایمر تنظیم کنیدنقل قول:
پخش صدا
[PHP]System.Media.SoundPlayer Player = new System.Media.SoundPlayer();
Player.SoundLocation = @"c:\1.wav";
Player.Play();
//Player.Stop();[/PHP]
بله ممنون. راستش من محدودیت حجم فایل دارم و این کد هم برای پخش فایل های Wav هست.
که همونطور که میدونید حجم این فایل ها زیاد هست . در واقع من میخوام یک فایل MP3 رو پخش کنم.
ممنون میشم در این رابطه کمک کنید.
[PHP] [DllImport("winmm.dll")]نقل قول:
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
//play
mciSendString("open \"" + Path File + "\" type MPEGVideo alias Mp3File", null, 0, IntPtr.Zero);
mciSendString("play Mp3File", null, 0, IntPtr.Zero);
//pause
mciSendString("pause Mp3File", null, 0, IntPtr.Zero);[/PHP]
ممنون دوست عزیزنقل قول:
راستش من یکم برای تبدیل کد های سی شارپ به ویبی دات نت مشکل دارم.
مخصوصا اون خط اول برنامه . میشه یکم توضیح بدید که این بخش رو باید در کجا قرار بدم یا چه طور dll مربوطه رو فراخوانی کنم؟ توی رفرنس هم این dll رو پیدا نکردم.
من زیاد کد وی بی نمیزنم بخش وی بی هم کنار بخش دات نت هست اگر از دوستان اونجا سوال کنید بهتره
اینجا بخش .net Freamwork هست که هم سی شارپ رو شامل میشه و هم ویبی دات نت . پس جای این سوال همینجاست .
شما یه توضیح بدید که این دو خط اول رو در سی شارپ کجا قرار میدید من خودم یه کاری می کنم تا یکی از دوستان مدیر این بخش برای پاسخ از راه برسند
بنده نگفتم که دات نت و وی بی از هم جدا هستند عرض کردم چون بخش وی ی داریم اگر در اون قسمت سوال کنید سریعتر به پاسخ خواهید رسیدنقل قول:
دو خط اول رو در کلاس form و خارج از تابع قرار میدهید و خط های بعد رو در تابع یا رویدادهای کنترل قرار دهید
ممنون دوست عزیز .
دوستام میتونند از کلاس زیر هم استفاده کنند.
و سپس این کدکد:Public Class AudioFile
' Windows API Declarations
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Int32, ByVal hwndCallback As Int32) As Int32
''' Constructor: Location is the filename of the media to play. Wave files and Mp3 files are the supported formats.
Public Sub New(ByVal location As String)
Me.Filename = location
End Sub
''' Plays the file that is specified as the filename.
Public Sub Play()
If _filename = "" Or Filename.Length <= 4 Then Exit Sub
Select Case Right(Filename, 3).ToLower
Case "mp3"
mciSendString("open """ & _filename & """ type mpegvideo alias audiofile", Nothing, 0, IntPtr.Zero)
Dim playCommand As String = "play audiofile from 0"
If _wait = True Then playCommand += " wait"
mciSendString(playCommand, Nothing, 0, IntPtr.Zero)
Case "wav"
mciSendString("open """ & _filename & """ type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero)
mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero)
Case "mid", "idi"
mciSendString("stop midi", "", 0, 0)
mciSendString("close midi", "", 0, 0)
mciSendString("open sequencer!" & _filename & " alias midi", "", 0, 0)
mciSendString("play midi", "", 0, 0)
Case Else
Throw New Exception("File type not supported.")
Call Close()
End Select
IsPaused = False
End Sub
''' Pause the current play back.
Public Sub Pause()
mciSendString("pause audiofile", Nothing, 0, IntPtr.Zero)
IsPaused = True
End Sub
''' Resume the current play back if it is currently paused.
Public Sub [Resume]()
mciSendString("resume audiofile", Nothing, 0, IntPtr.Zero)
IsPaused = False
End Sub
''' Stop the current file if it's playing.
Public Sub [Stop]()
mciSendString("stop audiofile", Nothing, 0, IntPtr.Zero)
End Sub
''' Close the file.
Public Sub Close()
mciSendString("close audiofile", Nothing, 0, IntPtr.Zero)
End Sub
Private _wait As Boolean = False
''' Halt the program until the .wav file is done playing. Be careful, this will lock the entire program up until the
''' file is done playing. It behaves as if the Windows Sleep API is called while the file is playing (and maybe it is, I don't
''' actually know, I'm just theorizing). :P
Public Property Wait() As Boolean
Get
Return _wait
End Get
Set(ByVal value As Boolean)
_wait = value
End Set
End Property
''' Sets the audio file's time format via the mciSendString API.
ReadOnly Property Milleseconds() As Integer
Get
Dim buf As String = Space(255)
mciSendString("set audiofile time format milliseconds", Nothing, 0, IntPtr.Zero)
mciSendString("status audiofile length", buf, 255, IntPtr.Zero)
buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up
If buf = "" Then
Return 0
Else
Return CInt(buf)
End If
End Get
End Property
''' Gets the status of the current playback file via the mciSendString API.
ReadOnly Property Status() As String
Get
Dim buf As String = Space(255)
mciSendString("status audiofile mode", buf, 255, IntPtr.Zero)
buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up
Return buf
End Get
End Property
''' Gets the file size of the current audio file.
ReadOnly Property FileSize() As Integer
Get
Try
Return My.Computer.FileSystem.GetFileInfo(_filename).Length
Catch ex As Exception
Return 0
End Try
End Get
End Property
''' Gets the channels of the file via the mciSendString API.
ReadOnly Property Channels() As Integer
Get
Dim buf As String = Space(255)
mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)
If IsNumeric(buf) = True Then
Return CInt(buf)
Else
Return -1
End If
End Get
End Property
''' Used for debugging purposes.
ReadOnly Property Debug() As String
Get
Dim buf As String = Space(255)
mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)
Return Str(buf)
End Get
End Property
Private _isPaused As Boolean = False
''' Whether or not the current playback is paused.
Public Property IsPaused() As Boolean
Get
Return _isPaused
End Get
Set(ByVal value As Boolean)
_isPaused = value
End Set
End Property
Private _filename As String
''' The current filename of the file that is to be played back.
Public Property Filename() As String
Get
Return _filename
End Get
Set(ByVal value As String)
If My.Computer.FileSystem.FileExists(value) = False Then
Throw New System.IO.FileNotFoundException
Exit Property
End If
_filename = value
End Set
End Property
End Class
کد:Dim audio As New AudioFile("C:\Temp\YourMP3File.mp3")
audio.Play()