Imports DataBase.DataBase
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectSound
Imports Microsoft.DirectX.AudioVideoPlayback
Imports Microsoft.DirectX.DirectPlay
Public Class frmSong
#Region "Metodi personalizzati"
Public Structure Song
Dim Autore, Genere, Gruppo As String
Dim Anno, Note, Nome As String
Dim Valutazione As Int16
Dim UltimoAscolto As Date
Dim Path As String
End Structure
Private Sub AddItem(ByVal F1 As String, ByVal f2 As String)
Dim L As ListViewItem
Dim S(1) As String
S(0) = F1
S(1) = f2
L = New ListViewItem(S)
lstInfo.Items.Add(L)
End Sub
Public Sub ResetLabel(ByVal Brano As Song)
lstInfo.Items.Clear()
AddItem("Nome", Brano.Nome)
AddItem("Autore", Brano.Autore)
AddItem("Genere", Brano.Genere)
AddItem("Gruppo", Brano.Gruppo)
AddItem("Anno di pubblicazione", Brano.Anno)
AddItem("Valutazione", Brano.Valutazione)
AddItem("Utimo ascolto", Brano.UltimoAscolto)
AddItem("Note personali", Brano.Note)
lstInfo.Visible = True
imgVideo.Visible = False
If MultimediaType(MainDB(lstSongs.SelectedIndex).Path) = MultimediaFile.AudioFile Then
cmdAllScreen.Visible = False
lblPos.Location = New Drawing.Point(240, Me.Height - 122)
Else
cmdAllScreen.Visible = True
lblPos.Location = New Drawing.Point(349, Me.Height - 122)
End If
End Sub
Public Shared Sub Save()
Dim W As New IO.StreamWriter(Application.StartupPath + "\Songs.dat")
Dim S As Song
Dim i As Int32 = 0
frmSong.lblStatus.Text = "Salvataggio in corso..."
For Each S In MainDB
W.WriteLine("@" + S.Nome)
W.WriteLine(S.Path)
W.WriteLine(S.Anno)
W.WriteLine(S.Autore)
W.WriteLine(S.Genere)
W.WriteLine(S.Gruppo)
W.WriteLine(S.Nome)
W.WriteLine(S.UltimoAscolto)
W.WriteLine(S.Valutazione)
i += 1
frmSong.lblStatus.Text = "Salvataggio in corso: " & (i * 100 / MainDB.Count) & "%"
Next
W.Close()
W = Nothing
frmSong.lblStatus.Text = "Salvataggio completato"
End Sub
Public Shared Function QuickSearch(ByVal ToSeek As String, ByVal InWhichFind As String) As Int16
Dim S As String = ""
For i As Int16 = 0 To ToSeek.Length - 1
S = S & InWhichFind.Chars(i)
Next
For i As Int64 = ToSeek.Length To InWhichFind.Length - 1
If S = ToSeek Then
Return i
End If
S = S.Remove(0, 1)
S = S & InWhichFind.Chars(i)
Next
End Function
Public Shared Function MultimediaType(ByVal FileName As String) As MultimediaFile
Dim Ext As String = IO.Path.GetExtension(FileName)
If Ext = ".wav" Or Ext = ".mp3" Or Ext = ".wma" Then
Return MultimediaFile.AudioFile
Else
If Ext = ".wmv" Or Ext = ".mpg" Or Ext = ".avi" Then
Return MultimediaFile.VideoFile
Else
Return MultimediaFile.NoMultimediaFile
End If
End If
End Function
Public Sub SetFormat(ByVal Value As MultimediaFile)
CF = Value
If Value = MultimediaFile.AudioFile Then
lstInfo.Visible = True
imgVideo.Visible = False
End If
If Value = MultimediaFile.VideoFile Then
lstInfo.Visible = False
imgVideo.Visible = True
End If
End Sub
Public Sub CenterVideo(ByVal Size As Drawing.Size)
Dim W As UInt32 = Size.Width
Dim H As UInt32 = Size.Height
Dim IW As UInt32 = lstInfo.Size.Width
Dim IH As UInt32 = lstInfo.Size.Height
If Size = lstInfo.Size Then
Exit Sub
End If
If W * H < IW * IH Then
Dim X, Y As UInt32
X = lstInfo.Location.X + Math.Abs(IW - W) / 2
Y = lstInfo.Location.Y + Math.Abs(IH - H) / 2
imgVideo.Location = New Drawing.Point(X, Y)
Exit Sub
End If
If W * H > IW * IH Then
Me.Height += (H - IH)
Me.Width += (W - IW)
Exit Sub
End If
End Sub
Public Sub SetLastListen(ByVal Index As Int32)
Dim C As Song = MainDB(Index)
C.UltimoAscolto = Date.Now
MainDB(Index) = C
End Sub
Public Sub InitVideo()
MainF = New Video(MainDB(lstSongs.SelectedIndex).Path)
MainF.Owner = imgVideo
MainF.FullScreen = False
imgVideo.Location = lstInfo.Location
CenterVideo(MainF.DefaultSize)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
cmdAllScreen.Visible = True
lblPos.Location = New Drawing.Point(349, Me.Height - 122)
End Sub
Public Sub InitAudio()
cmdAllScreen.Visible = False
lblPos.Location = New Drawing.Point(240, Me.Height - 122)
MainF = New Audio(MainDB(lstSongs.SelectedIndex).Path)
End Sub
#End Region
#Region "Varabili, Costanti"
Public Enum MultimediaFile
AudioFile
VideoFile
NoMultimediaFile
End Enum
Public CanSeek As Boolean = True
Public Shared CurrentSound As SecondaryBuffer
Public Shared MainF As Object
Public Shared MainDB As New ArrayList
Private CF As MultimediaFile
Private Sequence As Int16 = 0
#End Region
Private Sub frmSong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim r As New IO.StreamReader("Songs.dat")
Dim S As String
Dim C As Song = Nothing
lblStatus.Text = "Caricamento playlist in corso..."
While Not r.EndOfStream
S = r.ReadLine
If S(0) = "@" Then
C.Nome = S.Remove(0, 1)
C.Path = r.ReadLine
C.Anno = r.ReadLine
C.Autore = r.ReadLine
C.Genere = r.ReadLine
C.Gruppo = r.ReadLine
C.Note = r.ReadLine
Try
C.UltimoAscolto = r.ReadLine
Catch ICE As InvalidCastException
C.UltimoAscolto = Nothing
End Try
C.Valutazione = r.ReadLine
MainDB.Add(C)
lstSongs.Items.Add(C.Nome)
End If
End While
lblStatus.Text = "Caricamento completato"
Catch ex As System.IO.FileNotFoundException
Dim w As New IO.StreamWriter("Songs.dat")
w.Close()
w = Nothing
End Try
End Sub
Private Sub lstSongs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSongs.SelectedIndexChanged
If CanSeek Then
ResetLabel(MainDB(lstSongs.SelectedIndex))
End If
End Sub
Private Sub strAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileMultimedialeToolStripMenuItem.Click
If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim NS As New frmNewSong(FOpen.FileName, "ADD")
NS.ShowDialog()
End If
End Sub
Private Sub strModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strModify.Click
If lstSongs.SelectedIndex >= 0 Then
Dim NS As New frmNewSong(MainDB(lstSongs.SelectedIndex).Path, "MOD", lstSongs.SelectedIndex)
NS.ShowDialog()
Else
MsgBox("Nessun elemento selezionato!", MsgBoxStyle.Exclamation)
End If
End Sub
Private Sub strRemoveNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveNormal.Click
If lstSongs.SelectedIndex >= 0 Then
If MsgBox("Rimuovere questo brano?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
CanSeek = False
MainDB.RemoveAt(lstSongs.SelectedIndex)
lstSongs.Items.RemoveAt(lstSongs.SelectedIndex)
CanSeek = True
End If
End If
End Sub
Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub strMoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDown.Click
If lstSongs.SelectedIndex < 0 Or lstSongs.SelectedIndex = lstSongs.Items.Count - 1 Then
MsgBox("Impossibile spostare elemento!", MsgBoxStyle.Exclamation)
Exit Sub
End If
CanSeek = False
Dim Temp As Song = MainDB.Item(lstSongs.SelectedIndex + 1)
Dim S As String = lstSongs.Items.Item(lstSongs.SelectedIndex + 1)
'Sposta elemento avanti
MainDB.Item(lstSongs.SelectedIndex + 1) = MainDB.Item(lstSongs.SelectedIndex)
'Porta l'elemento sostituito indietro
MainDB.Item(lstSongs.SelectedIndex) = Temp
lstSongs.Items.Item(lstSongs.SelectedIndex + 1) = lstSongs.Items.Item(lstSongs.SelectedIndex)
lstSongs.Items.Item(lstSongs.SelectedIndex) = S
lstSongs.SelectedIndex += 1
CanSeek = True
End Sub
Private Sub strMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strUp.Click
If lstSongs.SelectedIndex <= 0 Then
MsgBox("Impossibile spostare elemento!", MsgBoxStyle.Exclamation)
Exit Sub
End If
CanSeek = False
Dim Temp As Song = MainDB.Item(lstSongs.SelectedIndex - 1)
Dim S As String = lstSongs.Items.Item(lstSongs.SelectedIndex - 1)
'Sposta elemento avanti
MainDB.Item(lstSongs.SelectedIndex - 1) = MainDB.Item(lstSongs.SelectedIndex)
'Porta l'elemento sostituito indietro
MainDB.Item(lstSongs.SelectedIndex) = Temp
lstSongs.Items.Item(lstSongs.SelectedIndex - 1) = lstSongs.Items.Item(lstSongs.SelectedIndex)
lstSongs.Items.Item(lstSongs.SelectedIndex) = S
lstSongs.SelectedIndex -= 1
CanSeek = True
End Sub
Private Sub strSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSave.Click
Save()
End Sub
Private Sub frmSong_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not MainF Is Nothing Then
MainF.Stop()
End If
If MsgBox("Salvare cambiamenti applicati?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
Save()
End If
End Sub
Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPLay.Click
If lstSongs.SelectedIndex < 0 Then
MsgBox("Nessun elemento selezionato!", MsgBoxStyle.Exclamation)
Exit Sub
End If
SetFormat(MultimediaType(MainDB(lstSongs.SelectedIndex).Path))
SetLastListen(lstSongs.SelectedIndex)
cmdAllScreen.Enabled = False
Try
If MainF.Paused Then
MainF.Play()
cmdStop.Enabled = True
cmdPause.Enabled = True
Else
If MainF.State = StateFlags.Running Then
MainF.Stop()
End If
If CF = MultimediaFile.AudioFile Then
InitAudio()
Else
InitVideo()
End If
MainF.Play()
cmdStop.Enabled = True
trkSound.Maximum = MainF.Duration
End If
tmrControl.Enabled = True
Catch NRE As NullReferenceException
If CF = MultimediaFile.AudioFile Then
InitAudio()
Else
InitVideo()
End If
MainF.Play()
cmdStop.Enabled = True
tmrControl.Enabled = True
trkSound.Maximum = MainF.Duration
End Try
End Sub
Private Sub cmdStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStop.Click
MainF.Stop()
trkSound.Value = 0
MainF.CurrentPosition = 0
MainF = Nothing
cmdStop.Enabled = False
tmrControl.Enabled = False
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Me.MaximizeBox = True
cmdAllScreen.Enabled = True
End Sub
Private Sub tmrControl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrControl.Tick
Dim TimeElapsed, TimeTotal As TimeSpan
TimeElapsed = TimeSpan.FromSeconds(CInt(MainF.CurrentPosition))
TimeTotal = TimeSpan.FromSeconds(CInt(MainF.Duration))
trkSound.Value = MainF.CurrentPosition
lblPos.Text = TimeElapsed.ToString & " secondi riprodotti su " & TimeTotal.ToString
End Sub
Private Sub trkSound_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkSound.Scroll
If tmrControl.Enabled Then
MainF.CurrentPosition = trkSound.Value
End If
End Sub
Private Sub strSeekInPlaylist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSeek.Click
Dim ToSeek As String = InputBox("Nome o parte del nome della canzone da cercare:", "Cerca nella playlist")
Dim S As String
If lstSongs.SelectedIndex < 0 Then
lstSongs.SelectedIndex = 0
End If
For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
S = lstSongs.Items(I)
If S.Contains(ToSeek) Then
lstSongs.SelectedIndex = I
Exit For
End If
Next
End Sub
Private Sub strSeekInComputer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CercaNelComputerToolStripMenuItem.Click
Dim S As New frmSeek
S.ShowDialog()
End Sub
Private Sub strRemoveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveAll.Click
If MsgBox("Rimuovere veramente tutti i brani?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
lstSongs.Items.Clear()
MainDB.Clear()
End If
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
MainF.Pause()
cmdPause.Enabled = False
tmrControl.Enabled = False
End Sub
Private Sub strRemoveChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveChecked.Click
If MsgBox("Procedere con la cancellazione degli elementi spuntati?", MsgBoxStyle.Question + vbYesNo, "Conferma") = MsgBoxResult.No Then
Exit Sub
End If
Dim ToDel As New ArrayList
Dim S As Song
Dim A As Int16
For Each A In lstSongs.CheckedIndices
ToDel.Add(MainDB(A))
Next
For Each S In ToDel
lstSongs.Items.Remove(S.Nome)
MainDB.Remove(S)
Next
End Sub
Private Sub cmdAllScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAllScreen.Click
cmdPlay_Click(Me, New System.EventArgs)
MainF.FullScreen = True
End Sub
Private Sub strSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSearch.Click
If lstSongs.SelectedIndex < 0 Then
lstSongs.SelectedIndex = 0
End If
If lstSongs.SelectedIndex > 0 And lstSongs.SelectedIndex < lstSongs.Items.Count - 1 Then
lstSongs.SelectedIndex += 1
End If
If strSearchMode.SelectedIndex < 0 Then
MsgBox("Nessuna modalità di ricerca selezionata!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If strSearchKey.Text = "" Then
MsgBox("Nessuna chiave di ricerca inserita!", MsgBoxStyle.Exclamation)
Exit Sub
End If
lblStatus.Text = "Ricerca in corso..."
Select Case strSearchMode.SelectedIndex
Case 0, 1, 2, 3, 4, 6
Dim Key As String = strSearchKey.Text
Dim S As String = ""
For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
Select Case strSearchMode.SelectedIndex
Case 0
S = MainDB(I).Nome
Case 1
S = MainDB(I).Autore
Case 2
S = MainDB(I).Genere
Case 3
S = MainDB(I).Gruppo
Case 4
S = MainDB(I).Anno
Case 6
S = MainDB(I).Note
End Select
If S.Contains(Key) Then
lstSongs.SelectedIndex = I
Exit Sub
End If
Next
Case Else
Dim Key As Int32 = strSearchKey.Text
Dim S As Int32
For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
S = MainDB(I).Valutazione
If S = Key Then
lstSongs.SelectedIndex = I
Exit Sub
End If
Next
End Select
lblStatus.Text = "Ricerca terminata"
End Sub
Private Sub strSearchKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSearchKey.Click
If strSearchKey.Text = "[Chiave di ricerca]" Then
strSearchKey.Text = Nothing
End If
End Sub
End Class