PDA

View Full Version : سوال: پخش فایل mp3 به همراه تکرار



Hossis
دوشنبه 17 مهر 1391, 07:27 صبح
فرض کنید که لیستی از فایل های mp3 در لیست باکس داریم و می خواهیم آنها را به ترتیب یکی پس از دیگری پخش کنیم (یعنی یکی که تمام شد, دیگری خودکار شروع شود)
کسی تابحال در این موضوع کار کرده؟؟
خواهشا دستورالعمل های کلی ندید (که کامپوننت فلان و فلان رو کار کن) اگر واقعا این رو تست کردید و استفاده نمودید, راهنمایی کنید!

alimanam
دوشنبه 17 مهر 1391, 18:30 عصر
با سلام

کد زیر رو در رویداد PlayStateChange یا درون یک Timer قرار بدین :


If (mediaplayerControl.playState = WMPLib.WMPPlayState.wmppsStopped) Then
If (lstMusicFiles.SelectedIndex < lstMusicFiles.Items.Count - 1) Then
lstMusicFiles.SelectedIndex = lstMusicFiles.SelectedIndex + 1
mediaplayerControl.URL = lstMusicFiles.SelectedItem
mediaplayerControl.Ctlcontrols.play()
End If
End If

در این مثال فرض بر این گرفته شده که شما برای پخش فایل های MP3 دارین از کنترل COM ویندوز پلیر ویندوز استفاده میکنین .

موفق باشید./

Hossis
جمعه 21 مهر 1391, 23:00 عصر
خیلی عالی بود دستتون درد نکنه
فقط یک نکته دیگر
اگر وضعیت WMP رو در حالت استپ یا مکث بگذاریم (یعنی خودما این حالت ها رو با کلیک انتخاب کنیم نه این که فایل صوتی به پایان برسه و خودبخود روی حالت استپ بره) چطور این تغییر حالت رو تشخیص می دهیم (تا از پخش ادامه لیست منصرف بشه؟

مهرداد صفا
جمعه 28 مهر 1391, 16:59 عصر
فرض کنید که لیستی از فایل های mp3 در لیست باکس داریم و می خواهیم آنها را به ترتیب یکی پس از دیگری پخش کنیم (یعنی یکی که تمام شد, دیگری خودکار شروع شود)
کسی تابحال در این موضوع کار کرده؟؟
خواهشا دستورالعمل های کلی ندید (که کامپوننت فلان و فلان رو کار کن) اگر واقعا این رو تست کردید و استفاده نمودید, راهنمایی کنید!
با سلام
این کلاس از System.Windows.Media.MediaPlayer استفاده میکند که خودم معمولا از این کلاس استفاده میکنم. برای استفاده کافیست نام فایلهای خود را به صورت آرایه یا مجموعه به آن بدهید تا به ترتیب اجرا کند:

'in the name of god
Imports System.ComponentModel
Imports System.Windows.Media

Public Class presentationMediaPlayer

'<Browsable(False),EditorBrowsable(EditorBrowsableSt ate.Never),> _
Dim WithEvents player As New MediaPlayer
Dim NowPlaying As New List(Of String)
Dim CurrentTrackNumber As Integer
Dim status As PlayStatus
Public Sub AddToNowPlayingList(ByVal files() As String)
NowPlaying.AddRange(files)
End Sub

Public Sub AddToNowPlayingList(ByVal files As System.Collections.Generic.List(Of String))
NowPlaying.AddRange(files)
End Sub

Public Property balance As SByte
Get
Return player.Balance / 0.01
End Get
Set(ByVal value As SByte)
If value > 100 Or value < -100 Then Throw New Exception("value is out of range. the balance must be in -100 to 100.")
player.Balance = value * 0.01
End Set
End Property

Public ReadOnly Property CurrentFileName As String
Get
Return NowPlaying(CurrentTrack - 1)
End Get
End Property

Public Property CurrentPosition As ULong
Get
Return Convert.ToUInt64(player.Position.TotalMilliseconds )
End Get
Set(ByVal value As ULong)
player.Position = System.TimeSpan.FromMilliseconds(Convert.ToDouble( value))
End Set
End Property

Public ReadOnly Property CurrentTrack As Integer
Get
Return CurrentTrackNumber
End Get
End Property

Public ReadOnly Property Length As ULong
Get
Return Convert.ToUInt64(player.NaturalDuration.TimeSpan.T otalMilliseconds)
End Get
End Property

Public Sub [Next]()
player.Open(New Uri(NowPlaying(CurrentTrack)))
CurrentTrackNumber += 1
player.Play()
status = PlayStatus.Playing
End Sub

Public Property NowPlayingList As System.Collections.Generic.List(Of String)
Get
Return NowPlaying
End Get
Set(ByVal value As System.Collections.Generic.List(Of String))
NowPlaying.Clear()
NowPlaying.AddRange(value)
End Set
End Property

Public Sub Pause()
player.Pause()
status = PlayStatus.Paused
End Sub

Public Sub play()
player.Play()
status = PlayStatus.Playing
End Sub

Public Sub play(ByVal file As String)
player.Open(New Uri(file))
player.Play()
status = PlayStatus.Playing
NowPlaying.Clear()
NowPlaying.Add(file)
CurrentTrackNumber = 1
End Sub

Public Sub play(ByVal Files() As String)
player.Open(New Uri(Files(0)))
player.Play()



status = PlayStatus.Playing
NowPlaying.Clear()
NowPlaying.AddRange(Files)
CurrentTrackNumber = 1
End Sub

Public Sub play(ByVal files As System.Collections.Generic.List(Of String))
play(files.ToArray)
End Sub

Public Property PlayBackSpeed As SByte
Get
If player.SpeedRatio > 1 Then


Return (player.SpeedRatio - 1) * 20
Else
Return (player.SpeedRatio - 1) / 0.01
End If
End Get
Set(ByVal value As SByte)
If value < -100 Or value > 100 Then Throw New Exception("value is out of range. the speed must be in -100 to 100.")
If value > 0 Then
player.SpeedRatio = 1 + value / 20
Else


player.SpeedRatio = 1 + value * 0.01
End If
End Set
End Property

Public ReadOnly Property PlayStatus As PlayStatus
Get
Return status
End Get
End Property

Public Sub previous()
If CurrentTrack <= 1 Then
Throw New IndexOutOfRangeException("the track does not exist in Now Playing list")
Else
CurrentTrackNumber -= 1
player.Open(New Uri(NowPlaying(CurrentTrack - 1)))
player.Play()
status = PlayStatus.Playing

End If

End Sub

Public Sub [Stop]()
player.Stop()
status = PlayStatus.Stop
End Sub

Public Property volume As Byte
Get
Return Convert.ToByte(player.Volume / 0.01)
End Get
Set(ByVal value As Byte)
If value < 0 Or value > 100 Then Throw New Exception("value is out of range. the volume must be in 0 to 100.")
player.Volume = value * 0.01
End Set
End Property


Private Sub player_MediaEnded(ByVal sender As Object, ByVal e As System.EventArgs) Handles player.MediaEnded
status = PlayStatus.Stop
If CurrentTrack < NowPlaying.Count Then [Next]()
End Sub
End Class
Public Enum PlayStatus
Playing
Paused
[Stop]
End Enum

فقط PresentationCore.dll را به Reference اضافه کنید.
اگر مشکلی بود مطرح کنید.
موفق باشید.

مهرداد صفا
جمعه 28 مهر 1391, 17:27 عصر
این رو هم اضافه کنم گر چه از خود کد پیداست برای مقادیر Balance و PlayBackSpeed مقدار -100 تا 100 را استفاده کنید، پیشفرض هم در هر دو مورد 0 است.
برای Volume از 0 تا 100.
خصوصیت CurrentPosition هم بر حسب هزارم ثانیه از ابتدای فایل از 0 تا Length قابل تغییر است (برای به عقب یا جلو بردن بر اساس زمان)

Hossis
شنبه 29 مهر 1391, 23:21 عصر
فقط PresentationCore.dll را به Reference اضافه کنید.
موفق باشید.
من این کامپوننت رو در لیست کامپوننت ها و هیچ جای دیگه پیدا نکردم
اگه ممکنه اسم کاملش رو بدید تا بتونم اد کنم

محسن واژدی
یک شنبه 30 مهر 1391, 08:02 صبح
من این کامپوننت رو در لیست کامپوننت ها و هیچ جای دیگه پیدا نکردم
اگه ممکنه اسم کاملش رو بدید تا بتونم اد کنم

سلام علیکم
کامپوننت در فولدری مشابه %windir%\Microsoft.NET\Windows\v6.0.4030 قرار داره
از ضمیمه زیر هم میتوانید دریافت کنید

موفق باشید

Hossis
دوشنبه 01 آبان 1391, 13:22 عصر
یک سوال دیگه هم در این کلاس هست که چطوری میشه مقدار زمان کل فایل صوتی (برحسب دقیقه و ثانیه و حتی ساعت) و زمان کنونی (که در حال پخش هست) رو بدست آورد مثل:
15:25 Of 1:42:31

xxnagin
دوشنبه 01 آبان 1391, 19:23 عصر
سلام
این برای کنترل مدیا پلیر هستش


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
AxWindowsMediaPlayer1.URL = ofd.FileName
AxWindowsMediaPlayer1.Ctlcontrols.play()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = (Format(Int(AxWindowsMediaPlayer1.currentMedia.dur ation \ 60), "00") & ":" & Format(Int(AxWindowsMediaPlayer1.currentMedia.dura tion Mod 60), "00"))
End Sub
End Class


و برای کامپوننت های دیگه از tag id3 استفاده میشه که به جای m.TotalSeconds کد مربوط به خود tag id3 استفاده میشه یعنی تو هر tag کدش فرق میکنه
این برای IdSharp (http://www.idsharp.com/products/tagging.php)هستش

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
Dim m As IdSharp.AudioInfo.IAudioFile = IdSharp.AudioInfo.AudioFile.Create(ofd.FileName, True)
Label1.Text = Format(Int(m.TotalSeconds \ 60), "00") & ":" & Format(Int(m.TotalSeconds Mod 60), "00")
End Sub

مهرداد صفا
چهارشنبه 03 آبان 1391, 16:56 عصر
یک سوال دیگه هم در این کلاس هست که چطوری میشه مقدار زمان کل فایل صوتی (برحسب دقیقه و ثانیه و حتی ساعت) و زمان کنونی (که در حال پخش هست) رو بدست آورد مثل:
15:25 Of 1:42:31
سلام
با استفاده از خصوصیت Position شی Player استفاده شده در کلاس میتوانید به ثانیه و دقیقه و... دسترسی داشته باشید به هر حال کلاس کمی تغییر پیدا کرد؛
خصوصیات LengthTime و CurrentPositionTime زمان را به صورت TimeSpan بر میگردانند و PositionPercentage محل در حال پخش به صورت درصد است. دو رویداد برای زمانی که وضعیت تغییر میکند و زمانی track تغییر می کند هم اضافه شدند.
کد کلاس:


'in the name of god
Imports System.ComponentModel
Imports System.Windows.Media

Public Class presentationMediaPlayer

Event StatusUpdated()
Event TrackChanged(ByVal sender As Object, ByVal e As TrackChangedEventArgs)

'<Browsable(False),EditorBrowsable(EditorBrowsableSt ate.Never),> _
Dim WithEvents player As New MediaPlayer
Dim _NowPlayingList As New List(Of String)
Dim _CurrentTrack As Integer = 0
Dim _PlayStatus As PlayStatus = PlayStatus.Stop
Public Sub AddToNowPlayingList(ByVal files() As String)
Me.NowPlayingList.AddRange(files)
End Sub

Public Sub AddToNowPlayingList(ByVal files As System.Collections.Generic.List(Of String))
Me.NowPlayingList.AddRange(files)
End Sub

Public Property balance As SByte
Get
Return player.Balance / 0.01
End Get
Set(ByVal value As SByte)
If value > 100 Or value < -100 Then Throw New Exception("value is out of range. the balance must be in -100 to 100.")
player.Balance = value * 0.01
End Set
End Property

Public ReadOnly Property CurrentFileName As String
Get
Return Me.NowPlayingList(CurrentTrack - 1)
End Get
End Property

Public Property CurrentPosition As ULong
Get
Return Convert.ToUInt64(player.Position.TotalMilliseconds )
End Get
Set(ByVal value As ULong)
player.Position = System.TimeSpan.FromMilliseconds(Convert.ToDouble( value))
End Set
End Property

Property CurrentPositionTime As System.TimeSpan
Get
Return player.Position
End Get
Set(ByVal value As TimeSpan)
player.Position = value
End Set
End Property

Property CurrentPositionPercentage() As Byte
Get
Return Int((Me.CurrentPosition / Me.Length) * 100)
End Get
Set(ByVal value As Byte)
If value < 0 Or value > 100 Then
Throw New Exception("value out of range!")
ElseIf value = 0 Then
Me.CurrentPosition = 0
Else
Me.CurrentPosition = Me.Length / (100 / value)
End If
End Set
End Property

Public Property CurrentTrack As Integer
Get
Return _CurrentTrack
End Get
Set(ByVal NewTrackNumber As Integer)
If NewTrackNumber < 1 Or NewTrackNumber > Me.TracksCount Then
Throw New Exception("value out of range! the current track number does not exist.")
Else
Try
player.Close()
Catch
End Try
player.Open(New Uri(Me.NowPlayingList(NewTrackNumber - 1)))
_CurrentTrack = NewTrackNumber
RaiseEvent TrackChanged(Me, New TrackChangedEventArgs(NewTrackNumber, Me.CurrentFileName))
player.Play()
_PlayStatus = ClassLibrary1.PlayStatus.Playing
RaiseEvent StatusUpdated()
End If
End Set
End Property

ReadOnly Property TracksCount() As Integer
Get
If Not Me.NowPlayingList Is Nothing Then
Return NowPlayingList.Count
Else
Return 0
End If
End Get
End Property

Property Repeat As Boolean = False

Public ReadOnly Property Length As ULong
Get
Return Convert.ToUInt64(player.NaturalDuration.TimeSpan.T otalMilliseconds)
End Get
End Property

ReadOnly Property LengthTime As System.TimeSpan
Get
Return player.NaturalDuration.TimeSpan
End Get
End Property

Public Sub [Next]()
CurrentTrack += 1
End Sub

Public Property NowPlayingList As System.Collections.Generic.List(Of String)
Get
Return _NowPlayingList
End Get
Set(ByVal value As System.Collections.Generic.List(Of String))
Me.play(value)
End Set
End Property

Public Sub Pause()
player.Pause()
_PlayStatus = PlayStatus.Paused
RaiseEvent StatusUpdated()
End Sub

Public Sub play()
player.Play()
_PlayStatus = PlayStatus.Playing
RaiseEvent StatusUpdated()
End Sub

Public Sub play(ByVal file As String)
Me.NowPlayingList.Clear()
Me.NowPlayingList.AddRange(file)
CurrentTrack = 1
End Sub

Public Sub play(ByVal Files() As String)
Me.NowPlayingList.Clear()
Me.NowPlayingList.AddRange(Files)
CurrentTrack = 1
End Sub

Public Sub play(ByVal files As System.Collections.Generic.List(Of String))
Me.play(files.ToArray)
End Sub

Public Property PlayBackSpeed As SByte
Get
If player.SpeedRatio > 1 Then
Return (player.SpeedRatio - 1) * 20
Else
Return (player.SpeedRatio - 1) / 0.01
End If
End Get
Set(ByVal value As SByte)
If value < -100 Or value > 100 Then Throw New Exception("value is out of range. the speed must be in -100 to 100.")
If value > 0 Then
player.SpeedRatio = 1 + value / 20
Else
player.SpeedRatio = 1 + value * 0.01
End If
End Set
End Property

Public ReadOnly Property PlayStatus As PlayStatus
Get
Return _PlayStatus
End Get
End Property

Public Sub previous()
CurrentTrack -= 1
End Sub

Public Sub [Stop]()
player.Stop()
_PlayStatus = PlayStatus.Stop
RaiseEvent StatusUpdated()
End Sub

Public Property volume As Byte
Get
Return Convert.ToByte(player.Volume / 0.01)
End Get
Set(ByVal value As Byte)
If value < 0 Or value > 100 Then Throw New Exception("value is out of range. the volume must be in 0 to 100.")
player.Volume = value * 0.01
End Set
End Property

Private Sub player_MediaEnded(ByVal sender As Object, ByVal e As System.EventArgs) Handles player.MediaEnded
_PlayStatus = PlayStatus.Stop
RaiseEvent StatusUpdated()
If Not Repeat Then
If CurrentTrack < Me.TracksCount Then [Next]()
Else
Me.play()
End If
End Sub
End Class


Public Class TrackChangedEventArgs
Inherits System.EventArgs
Property NewTrackNumber As Integer
Property NewTrackName As String

Sub New(ByVal TrackNumber As Integer, ByVal FileName As String)
Me.NewTrackNumber = TrackNumber
Me.NewTrackName = FileName
End Sub
End Class

Public Enum PlayStatus
Playing
Paused
[Stop]
End Enum

لطفا اگر نظری داشتید یا خطایی در کار کلاس بود مطرح کنید.
موفق باشید.