Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Aqua Player - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports DataBase.DataBase
  2. Imports Microsoft.DirectX
  3. Imports Microsoft.DirectX.DirectSound
  4. Imports Microsoft.DirectX.AudioVideoPlayback
  5. Imports Microsoft.DirectX.DirectPlay
  6. Public Class frmSong
  7. #Region "Metodi personalizzati"
  8.     Public Structure Song
  9.         Dim Autore, Genere, Gruppo As String
  10.         Dim Anno, Note, Nome As String
  11.         Dim Valutazione As Int16
  12.         Dim UltimoAscolto As Date
  13.         Dim Path As String
  14.     End Structure
  15.     Private Sub AddItem(ByVal F1 As String, ByVal f2 As String)
  16.         Dim L As ListViewItem
  17.         Dim S(1) As String
  18.         S(0) = F1
  19.         S(1) = f2
  20.         L = New ListViewItem(S)
  21.         lstInfo.Items.Add(L)
  22.     End Sub
  23.     Public Sub ResetLabel(ByVal Brano As Song)
  24.         lstInfo.Items.Clear()
  25.         AddItem("Nome", Brano.Nome)
  26.         AddItem("Autore", Brano.Autore)
  27.         AddItem("Genere", Brano.Genere)
  28.         AddItem("Gruppo", Brano.Gruppo)
  29.         AddItem("Anno di pubblicazione", Brano.Anno)
  30.         AddItem("Valutazione", Brano.Valutazione)
  31.         AddItem("Utimo ascolto", Brano.UltimoAscolto)
  32.         AddItem("Note personali", Brano.Note)
  33.         lstInfo.Visible = True
  34.         imgVideo.Visible = False
  35.         If MultimediaType(MainDB(lstSongs.SelectedIndex).Path) = MultimediaFile.AudioFile Then
  36.             cmdAllScreen.Visible = False
  37.             lblPos.Location = New Drawing.Point(240, Me.Height - 122)
  38.         Else
  39.             cmdAllScreen.Visible = True
  40.             lblPos.Location = New Drawing.Point(349, Me.Height - 122)
  41.         End If
  42.     End Sub
  43.     Public Shared Sub Save()
  44.         Dim W As New IO.StreamWriter(Application.StartupPath + "\Songs.dat")
  45.         Dim S As Song
  46.         Dim i As Int32 = 0
  47.         frmSong.lblStatus.Text = "Salvataggio in corso..."
  48.         For Each S In MainDB
  49.             W.WriteLine("@" + S.Nome)
  50.             W.WriteLine(S.Path)
  51.             W.WriteLine(S.Anno)
  52.             W.WriteLine(S.Autore)
  53.             W.WriteLine(S.Genere)
  54.             W.WriteLine(S.Gruppo)
  55.             W.WriteLine(S.Nome)
  56.             W.WriteLine(S.UltimoAscolto)
  57.             W.WriteLine(S.Valutazione)
  58.             i += 1
  59.             frmSong.lblStatus.Text = "Salvataggio in corso: " & (i * 100 / MainDB.Count) & "%"
  60.         Next
  61.         W.Close()
  62.         W = Nothing
  63.         frmSong.lblStatus.Text = "Salvataggio completato"
  64.     End Sub
  65.     Public Shared Function QuickSearch(ByVal ToSeek As String, ByVal InWhichFind As String) As Int16
  66.         Dim S As String = ""
  67.  
  68.         For i As Int16 = 0 To ToSeek.Length - 1
  69.             S = S & InWhichFind.Chars(i)
  70.         Next
  71.  
  72.         For i As Int64 = ToSeek.Length To InWhichFind.Length - 1
  73.             If S = ToSeek Then
  74.                 Return i
  75.             End If
  76.             S = S.Remove(0, 1)
  77.             S = S & InWhichFind.Chars(i)
  78.         Next
  79.     End Function
  80.     Public Shared Function MultimediaType(ByVal FileName As String) As MultimediaFile
  81.         Dim Ext As String = IO.Path.GetExtension(FileName)
  82.  
  83.         If Ext = ".wav" Or Ext = ".mp3" Or Ext = ".wma" Then
  84.             Return MultimediaFile.AudioFile
  85.         Else
  86.             If Ext = ".wmv" Or Ext = ".mpg" Or Ext = ".avi" Then
  87.                 Return MultimediaFile.VideoFile
  88.             Else
  89.                 Return MultimediaFile.NoMultimediaFile
  90.             End If
  91.         End If
  92.     End Function
  93.     Public Sub SetFormat(ByVal Value As MultimediaFile)
  94.         CF = Value
  95.         If Value = MultimediaFile.AudioFile Then
  96.             lstInfo.Visible = True
  97.             imgVideo.Visible = False
  98.         End If
  99.         If Value = MultimediaFile.VideoFile Then
  100.             lstInfo.Visible = False
  101.             imgVideo.Visible = True
  102.         End If
  103.     End Sub
  104.     Public Sub CenterVideo(ByVal Size As Drawing.Size)
  105.         Dim W As UInt32 = Size.Width
  106.         Dim H As UInt32 = Size.Height
  107.         Dim IW As UInt32 = lstInfo.Size.Width
  108.         Dim IH As UInt32 = lstInfo.Size.Height
  109.  
  110.         If Size = lstInfo.Size Then
  111.             Exit Sub
  112.         End If
  113.         If W * H < IW * IH Then
  114.             Dim X, Y As UInt32
  115.             X = lstInfo.Location.X + Math.Abs(IW - W) / 2
  116.             Y = lstInfo.Location.Y + Math.Abs(IH - H) / 2
  117.             imgVideo.Location = New Drawing.Point(X, Y)
  118.             Exit Sub
  119.         End If
  120.         If W * H > IW * IH Then
  121.             Me.Height += (H - IH)
  122.             Me.Width += (W - IW)
  123.             Exit Sub
  124.         End If
  125.     End Sub
  126.     Public Sub SetLastListen(ByVal Index As Int32)
  127.         Dim C As Song = MainDB(Index)
  128.         C.UltimoAscolto = Date.Now
  129.         MainDB(Index) = C
  130.     End Sub
  131.     Public Sub InitVideo()
  132.         MainF = New Video(MainDB(lstSongs.SelectedIndex).Path)
  133.         MainF.Owner = imgVideo
  134.         MainF.FullScreen = False
  135.         imgVideo.Location = lstInfo.Location
  136.         CenterVideo(MainF.DefaultSize)
  137.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
  138.         Me.MaximizeBox = False
  139.         cmdAllScreen.Visible = True
  140.         lblPos.Location = New Drawing.Point(349, Me.Height - 122)
  141.     End Sub
  142.     Public Sub InitAudio()
  143.         cmdAllScreen.Visible = False
  144.         lblPos.Location = New Drawing.Point(240, Me.Height - 122)
  145.         MainF = New Audio(MainDB(lstSongs.SelectedIndex).Path)
  146.     End Sub
  147. #End Region
  148. #Region "Varabili, Costanti"
  149.     Public Enum MultimediaFile
  150.         AudioFile
  151.         VideoFile
  152.         NoMultimediaFile
  153.     End Enum
  154.     Public CanSeek As Boolean = True
  155.     Public Shared CurrentSound As SecondaryBuffer
  156.     Public Shared MainF As Object
  157.     Public Shared MainDB As New ArrayList
  158.     Private CF As MultimediaFile
  159.     Private Sequence As Int16 = 0
  160. #End Region
  161.     Private Sub frmSong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  162.         Try
  163.             Dim r As New IO.StreamReader("Songs.dat")
  164.             Dim S As String
  165.             Dim C As Song = Nothing
  166.             lblStatus.Text = "Caricamento playlist in corso..."
  167.  
  168.             While Not r.EndOfStream
  169.                 S = r.ReadLine
  170.                 If S(0) = "@" Then
  171.                     C.Nome = S.Remove(0, 1)
  172.                     C.Path = r.ReadLine
  173.                     C.Anno = r.ReadLine
  174.                     C.Autore = r.ReadLine
  175.                     C.Genere = r.ReadLine
  176.                     C.Gruppo = r.ReadLine
  177.                     C.Note = r.ReadLine
  178.                     Try
  179.                         C.UltimoAscolto = r.ReadLine
  180.                     Catch ICE As InvalidCastException
  181.                         C.UltimoAscolto = Nothing
  182.                     End Try
  183.                     C.Valutazione = r.ReadLine
  184.  
  185.                     MainDB.Add(C)
  186.                     lstSongs.Items.Add(C.Nome)
  187.                 End If
  188.             End While
  189.  
  190.             lblStatus.Text = "Caricamento completato"
  191.         Catch ex As System.IO.FileNotFoundException
  192.             Dim w As New IO.StreamWriter("Songs.dat")
  193.             w.Close()
  194.             w = Nothing
  195.         End Try
  196.     End Sub
  197.     Private Sub lstSongs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSongs.SelectedIndexChanged
  198.         If CanSeek Then
  199.             ResetLabel(MainDB(lstSongs.SelectedIndex))
  200.         End If
  201.     End Sub
  202.     Private Sub strAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileMultimedialeToolStripMenuItem.Click
  203.         If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
  204.             Dim NS As New frmNewSong(FOpen.FileName, "ADD")
  205.             NS.ShowDialog()
  206.         End If
  207.     End Sub
  208.     Private Sub strModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strModify.Click
  209.         If lstSongs.SelectedIndex >= 0 Then
  210.             Dim NS As New frmNewSong(MainDB(lstSongs.SelectedIndex).Path, "MOD", lstSongs.SelectedIndex)
  211.             NS.ShowDialog()
  212.         Else
  213.             MsgBox("Nessun elemento selezionato!", MsgBoxStyle.Exclamation)
  214.         End If
  215.     End Sub
  216.     Private Sub strRemoveNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveNormal.Click
  217.         If lstSongs.SelectedIndex >= 0 Then
  218.             If MsgBox("Rimuovere questo brano?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
  219.                 CanSeek = False
  220.                 MainDB.RemoveAt(lstSongs.SelectedIndex)
  221.                 lstSongs.Items.RemoveAt(lstSongs.SelectedIndex)
  222.                 CanSeek = True
  223.             End If
  224.         End If
  225.     End Sub
  226.     Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  227.         Me.Close()
  228.     End Sub
  229.     Private Sub strMoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDown.Click
  230.         If lstSongs.SelectedIndex < 0 Or lstSongs.SelectedIndex = lstSongs.Items.Count - 1 Then
  231.             MsgBox("Impossibile spostare elemento!", MsgBoxStyle.Exclamation)
  232.             Exit Sub
  233.         End If
  234.         CanSeek = False
  235.  
  236.         Dim Temp As Song = MainDB.Item(lstSongs.SelectedIndex + 1)
  237.         Dim S As String = lstSongs.Items.Item(lstSongs.SelectedIndex + 1)
  238.         'Sposta elemento avanti
  239.         MainDB.Item(lstSongs.SelectedIndex + 1) = MainDB.Item(lstSongs.SelectedIndex)
  240.         'Porta l'elemento sostituito indietro
  241.         MainDB.Item(lstSongs.SelectedIndex) = Temp
  242.  
  243.         lstSongs.Items.Item(lstSongs.SelectedIndex + 1) = lstSongs.Items.Item(lstSongs.SelectedIndex)
  244.         lstSongs.Items.Item(lstSongs.SelectedIndex) = S
  245.         lstSongs.SelectedIndex += 1
  246.  
  247.         CanSeek = True
  248.     End Sub
  249.     Private Sub strMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strUp.Click
  250.         If lstSongs.SelectedIndex <= 0 Then
  251.             MsgBox("Impossibile spostare elemento!", MsgBoxStyle.Exclamation)
  252.             Exit Sub
  253.         End If
  254.         CanSeek = False
  255.  
  256.         Dim Temp As Song = MainDB.Item(lstSongs.SelectedIndex - 1)
  257.         Dim S As String = lstSongs.Items.Item(lstSongs.SelectedIndex - 1)
  258.         'Sposta elemento avanti
  259.         MainDB.Item(lstSongs.SelectedIndex - 1) = MainDB.Item(lstSongs.SelectedIndex)
  260.         'Porta l'elemento sostituito indietro
  261.         MainDB.Item(lstSongs.SelectedIndex) = Temp
  262.  
  263.         lstSongs.Items.Item(lstSongs.SelectedIndex - 1) = lstSongs.Items.Item(lstSongs.SelectedIndex)
  264.         lstSongs.Items.Item(lstSongs.SelectedIndex) = S
  265.         lstSongs.SelectedIndex -= 1
  266.  
  267.         CanSeek = True
  268.     End Sub
  269.     Private Sub strSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSave.Click
  270.         Save()
  271.     End Sub
  272.     Private Sub frmSong_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  273.         If Not MainF Is Nothing Then
  274.             MainF.Stop()
  275.         End If
  276.         If MsgBox("Salvare cambiamenti applicati?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
  277.             Save()
  278.         End If
  279.     End Sub
  280.     Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPLay.Click
  281.         If lstSongs.SelectedIndex < 0 Then
  282.             MsgBox("Nessun elemento selezionato!", MsgBoxStyle.Exclamation)
  283.             Exit Sub
  284.         End If
  285.  
  286.         SetFormat(MultimediaType(MainDB(lstSongs.SelectedIndex).Path))
  287.         SetLastListen(lstSongs.SelectedIndex)
  288.         cmdAllScreen.Enabled = False
  289.  
  290.         Try
  291.             If MainF.Paused Then
  292.                 MainF.Play()
  293.                 cmdStop.Enabled = True
  294.                 cmdPause.Enabled = True
  295.             Else
  296.                 If MainF.State = StateFlags.Running Then
  297.                     MainF.Stop()
  298.                 End If
  299.                 If CF = MultimediaFile.AudioFile Then
  300.                     InitAudio()
  301.                 Else
  302.                     InitVideo()
  303.                 End If
  304.                 MainF.Play()
  305.                 cmdStop.Enabled = True
  306.                 trkSound.Maximum = MainF.Duration
  307.             End If
  308.             tmrControl.Enabled = True
  309.         Catch NRE As NullReferenceException
  310.             If CF = MultimediaFile.AudioFile Then
  311.                 InitAudio()
  312.             Else
  313.                 InitVideo()
  314.             End If
  315.             MainF.Play()
  316.             cmdStop.Enabled = True
  317.             tmrControl.Enabled = True
  318.             trkSound.Maximum = MainF.Duration
  319.         End Try
  320.     End Sub
  321.     Private Sub cmdStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStop.Click
  322.         MainF.Stop()
  323.         trkSound.Value = 0
  324.         MainF.CurrentPosition = 0
  325.         MainF = Nothing
  326.         cmdStop.Enabled = False
  327.         tmrControl.Enabled = False
  328.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
  329.         Me.MaximizeBox = True
  330.         cmdAllScreen.Enabled = True
  331.     End Sub
  332.     Private Sub tmrControl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrControl.Tick
  333.         Dim TimeElapsed, TimeTotal As TimeSpan
  334.  
  335.         TimeElapsed = TimeSpan.FromSeconds(CInt(MainF.CurrentPosition))
  336.         TimeTotal = TimeSpan.FromSeconds(CInt(MainF.Duration))
  337.         trkSound.Value = MainF.CurrentPosition
  338.         lblPos.Text = TimeElapsed.ToString & " secondi riprodotti su " & TimeTotal.ToString
  339.     End Sub
  340.     Private Sub trkSound_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkSound.Scroll
  341.         If tmrControl.Enabled Then
  342.             MainF.CurrentPosition = trkSound.Value
  343.         End If
  344.     End Sub
  345.     Private Sub strSeekInPlaylist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSeek.Click
  346.         Dim ToSeek As String = InputBox("Nome o parte del nome della canzone da cercare:", "Cerca nella playlist")
  347.         Dim S As String
  348.  
  349.         If lstSongs.SelectedIndex < 0 Then
  350.             lstSongs.SelectedIndex = 0
  351.         End If
  352.  
  353.         For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
  354.             S = lstSongs.Items(I)
  355.             If S.Contains(ToSeek) Then
  356.                 lstSongs.SelectedIndex = I
  357.                 Exit For
  358.             End If
  359.         Next
  360.     End Sub
  361.     Private Sub strSeekInComputer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CercaNelComputerToolStripMenuItem.Click
  362.         Dim S As New frmSeek
  363.         S.ShowDialog()
  364.     End Sub
  365.     Private Sub strRemoveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveAll.Click
  366.         If MsgBox("Rimuovere veramente tutti i brani?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.Yes Then
  367.             lstSongs.Items.Clear()
  368.             MainDB.Clear()
  369.         End If
  370.     End Sub
  371.     Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
  372.         MainF.Pause()
  373.         cmdPause.Enabled = False
  374.         tmrControl.Enabled = False
  375.     End Sub
  376.     Private Sub strRemoveChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveChecked.Click
  377.         If MsgBox("Procedere con la cancellazione degli elementi spuntati?", MsgBoxStyle.Question + vbYesNo, "Conferma") = MsgBoxResult.No Then
  378.             Exit Sub
  379.         End If
  380.  
  381.         Dim ToDel As New ArrayList
  382.         Dim S As Song
  383.         Dim A As Int16
  384.  
  385.         For Each A In lstSongs.CheckedIndices
  386.             ToDel.Add(MainDB(A))
  387.         Next
  388.  
  389.         For Each S In ToDel
  390.             lstSongs.Items.Remove(S.Nome)
  391.             MainDB.Remove(S)
  392.         Next
  393.     End Sub
  394.     Private Sub cmdAllScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAllScreen.Click
  395.         cmdPlay_Click(Me, New System.EventArgs)
  396.         MainF.FullScreen = True
  397.     End Sub
  398.     Private Sub strSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSearch.Click
  399.         If lstSongs.SelectedIndex < 0 Then
  400.             lstSongs.SelectedIndex = 0
  401.         End If
  402.         If lstSongs.SelectedIndex > 0 And lstSongs.SelectedIndex < lstSongs.Items.Count - 1 Then
  403.             lstSongs.SelectedIndex += 1
  404.         End If
  405.         If strSearchMode.SelectedIndex < 0 Then
  406.             MsgBox("Nessuna modalità di ricerca selezionata!", MsgBoxStyle.Exclamation)
  407.             Exit Sub
  408.         End If
  409.         If strSearchKey.Text = "" Then
  410.             MsgBox("Nessuna chiave di ricerca inserita!", MsgBoxStyle.Exclamation)
  411.             Exit Sub
  412.         End If
  413.  
  414.         lblStatus.Text = "Ricerca in corso..."
  415.  
  416.         Select Case strSearchMode.SelectedIndex
  417.             Case 0, 1, 2, 3, 4, 6
  418.                 Dim Key As String = strSearchKey.Text
  419.                 Dim S As String = ""
  420.  
  421.                 For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
  422.                     Select Case strSearchMode.SelectedIndex
  423.                         Case 0
  424.                             S = MainDB(I).Nome
  425.                         Case 1
  426.                             S = MainDB(I).Autore
  427.                         Case 2
  428.                             S = MainDB(I).Genere
  429.                         Case 3
  430.                             S = MainDB(I).Gruppo
  431.                         Case 4
  432.                             S = MainDB(I).Anno
  433.                         Case 6
  434.                             S = MainDB(I).Note
  435.                     End Select
  436.                     If S.Contains(Key) Then
  437.                         lstSongs.SelectedIndex = I
  438.                         Exit Sub
  439.                     End If
  440.                 Next
  441.             Case Else
  442.                 Dim Key As Int32 = strSearchKey.Text
  443.                 Dim S As Int32
  444.  
  445.                 For I As Int32 = lstSongs.SelectedIndex To MainDB.Count - 1
  446.                     S = MainDB(I).Valutazione
  447.                     If S = Key Then
  448.                         lstSongs.SelectedIndex = I
  449.                         Exit Sub
  450.                     End If
  451.                 Next
  452.         End Select
  453.  
  454.         lblStatus.Text = "Ricerca terminata"
  455.     End Sub
  456.     Private Sub strSearchKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSearchKey.Click
  457.         If strSearchKey.Text = "[Chiave di ricerca]" Then
  458.             strSearchKey.Text = Nothing
  459.         End If
  460.     End Sub
  461. End Class