Programming guides for beginner...
Any comments are welcomed....
I hope it helps!!! Thanks for drop by...

Sunday, January 30, 2011

My first step-by-step tutorial on Simple Music Player (VS2008) Part II

 

     After the First Part of my step-by-step tutorial which is the Interface of the Music Player, now I will start with the coding.

     First of all, to use Microsoft.DirectX.AudioVideoPlayback reference that added in previous post, add this line:

“Imports Microsoft.DirectX.AudioVideoPlayback”

to the coding. To open the coding, press F7. The following picture shows the position of the line.


6


The line is added at the most top outside the class.


     After that, we will need to declare some variables for later use.


8


Comments are added besides the coding as to explain the use of each variable. "#Region" is use to separate coding into region and for this one, I use declaration as the region name. You can use any name you like as it is just to understand what the region use for.


Code to be copied:



#Region "declaration"
    Public WithEvents objAudio As Audio
    Private b_musicplaying As Boolean  'use to store music play state
    Private b_musicEnding As Boolean  'use to check end of music
    Private b_loadmusic As Boolean 'use to check loaded music
    Private int_currplayindex As Integer 'use to store current playing index
    Private str_filename As String 'use to store current filename
    Private str_filepath As String 'use to store current filepath
    Private clr_previous As Color 'use to store color for previouly played music
#End Region

Public WithEvents objAudio As Audio


use to set objAudio as Audio in the DirectX (obj means object - this will make user to clear about what we assign to objAudio. objAudio had been assigned with Audio with include the events.)


After declaration, I will continue with the method of drag in music files into the listview1.


 


9


10


11


I didn't explain much in this drag in or drag drop method. For any private sub that handles the object we drag into our interface, you can look at the top where it shows ListView1 in the above picture. If you move your mouse there, a tooltip balloon will show "Class Name" and beside it is "Method Name".


As I mention in the method, I actually modified someone method of looking for directory inside directory... Means look for files inside two layer of directories. I modified it so that when I drag in a folder which contains folders that having a lot of songs inside, it will add the filename into ListView1 and filepath into ListView2.


Code to be copied:


#Region "file_dragenter"


    'this method is being modified from someone else.
    'Sorry for forget his name.
    'This method will only works with two layer directories.
    'Will not works if the files is deep inside more than two layer directories
    'If anyone knows how to loop to find all files in deep, please share it with me. Thanks


    Private Sub ListView1_DragDrop(ByVal sender As Object, _
                                   ByVal e As System.Windows.Forms.DragEventArgs) _
                                   Handles ListView1.DragDrop
        Dim I, iMaxFiles As Integer
        Dim iMaxDir As Integer
        Dim oFileName As String
        Dim str_path As Array
        Dim colFiles() As String = e.Data.GetData(DataFormats.FileDrop, True)
        Dim filelength As Double
        iMaxFiles = colFiles.GetUpperBound(iMaxFiles)
        If iMaxFiles = 0 Then


        End If
        For I = 0 To iMaxFiles
            oFileName = System.IO.Path.GetFileName(colFiles(I))
            filelength = colFiles.Length
            Try
                System.IO.Directory.GetDirectories(colFiles(I))
                If System.IO.Directory.GetDirectories(colFiles(I)).Length > 0 Then
                    iMaxDir = System.IO.Directory.GetDirectories(colFiles(I)).GetUpperBound(iMaxDir)
                    str_path = System.IO.Directory.GetDirectories(colFiles(I))
                    For j = 0 To iMaxDir
                        Dim di As New IO.DirectoryInfo(str_path(j))
                        Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3") 'change here for other format of files
                        Dim dra As IO.FileInfo
                        For Each dra In diar1
                            ListView1.Items.Add(dra.ToString)
                            ListView2.Items.Add(str_path(j) & "\" & dra.ToString)
                        Next
                    Next
                Else
                    Dim di As New IO.DirectoryInfo(colFiles(I))
                    Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3") 'change here for other format of files
                    Dim dra As IO.FileInfo


                    For Each dra In diar1
                        ListView1.Items.Add(dra.ToString)
                        ListView2.Items.Add(colFiles(I) & "\" & dra.ToString)
                    Next
                End If
            Catch ex As Exception
                ListView1.Items.Add(oFileName)
                ListView2.Items.Add(colFiles(I))
            End Try
        Next
    End Sub



    Private Sub ListView1_DragEnter(ByVal sender As Object, _
                                    ByVal e As System.Windows.Forms.DragEventArgs) _
                                    Handles ListView1.DragEnter


        e.Effect = DragDropEffects.Copy


    End Sub
#End Region


Reminder:Remember to change your listview1 properties (allowdrop to true). Now you can try to drag and drop a folder into listview1. To do this, you will need to debug the program by pressing F5. Only mp3 files can be drop in if you didn't change the format at here:


Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3") 'change here for other format of files

 


Now, we will come to the construction part where the main coding works here.


2224252628


The above picture shows what happens when the form is loaded, button1,2,3,4 being clicked, listveiw1 being clicked and double clicked. The coding to be copied is provided as following:



#Region "construction"
    Private Sub SimpleMusicPlayer_Load(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles MyBase.Load


        'occurs when form load
        clr_previous = Color.DarkGray 'change previouly played music color here


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button1.Click


        'occurs when button1 (Play) clicked
        If b_loadmusic = False Then Exit Sub 'exit if no music loaded
        If b_musicplaying = True Then   'if music playing now
            objAudio.Pause()
            Button1.Text = "Play"   'change text from pause to play
            b_musicplaying = False  'set playing as false as music is paused
        ElseIf b_musicplaying = False Then
            Timer1.Start()  'start timer1 - use to check when music end.
            Try
                objAudio.Stop()
            Catch ex As Exception
            End Try
            objAudio.Play()         'play the music
            Button1.Text = "Pause"  'change text from play to pause
            b_musicplaying = True   'set playing as true as music started
        End If


    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button2.Click


        'occurs when button2 (Stop) clicked


        If b_loadmusic = False Then Exit Sub 'exit if no music loaded
        objAudio.Stop() 'stop the music
        ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
        Timer1.Stop()   'stop timer1 as music consider ended.
        Button1.Text = "Play"   'change text from pause to play
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button3.Click


        If b_loadmusic = False Then Exit Sub
        If objAudio.Playing = True Then
            Try
                objAudio.Stop()
                ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
                int_currplayindex = int_currplayindex - 1
                If int_currplayindex < 0 Then
                    int_currplayindex = ListView1.Items.Count - 1
                End If
                ListView1.Items.Item(int_currplayindex).Selected = True
                ListView1.Items.Item(int_currplayindex).EnsureVisible()
                Try
                    If int_currplayindex < 0 Then Exit Try
                    str_filepath = CStr(ListView2.Items.Item(int_currplayindex).Text) 'getting file path
                    objAudio = New Audio(str_filepath, False)  'method to load music
                    b_loadmusic = True
                    objAudio.Play()
                Catch ex As Exception
                End Try
            Catch ex As Exception
            End Try
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button4.Click


        If b_loadmusic = False Then Exit Sub
        If objAudio.Playing = True Then
            Try
                objAudio.Stop()
                ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
                int_currplayindex = int_currplayindex + 1
                If int_currplayindex > ListView1.Items.Count - 1 Then
                    int_currplayindex = 0
                End If
                ListView1.Items.Item(int_currplayindex).Selected = True
                ListView1.Items.Item(int_currplayindex).EnsureVisible()
                Try
                    If int_currplayindex < 0 Then Exit Try
                    If int_currplayindex > ListView1.Items.Count - 1 Then
                        int_currplayindex = 0
                    End If
                    str_filepath = CStr(ListView2.Items.Item(int_currplayindex).Text) 'getting file path
                    objAudio = New Audio(str_filepath, False)  'method to load music
                    b_loadmusic = True
                    objAudio.Play()
                Catch ex As Exception
                End Try
            Catch ex As Exception
            End Try
        End If


    End Sub


    Private Sub ListView1_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles ListView1.Click


        If b_musicplaying = True Then Exit Sub 'do nothing if music playing.
        If int_currplayindex <> Nothing Then
            Try
                If ListView1.SelectedItems(0).Index <> int_currplayindex Then
                    ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
                End If
            Catch ex As Exception
            End Try
        End If
        Try
            int_currplayindex = ListView1.SelectedItems(0).Index
            ListView1.Items.Item(int_currplayindex).EnsureVisible()
            If int_currplayindex < 0 Then Exit Try
            str_filepath = CStr(ListView2.Items.Item(int_currplayindex).Text) 'getting file path
            objAudio = New Audio(str_filepath, False)  'method to load music
            b_loadmusic = True
        Catch ex As Exception
            b_loadmusic = False
        End Try
    End Sub


    Private Sub ListView1_DoubleClick(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles ListView1.DoubleClick


        ListView1.Items.Item(int_currplayindex).BackColor = clr_previous 'previous color
        int_currplayindex = ListView1.SelectedItems(0).Index
        ListView1.Items.Item(int_currplayindex).EnsureVisible()
        str_filepath = CStr(ListView2.Items.Item(int_currplayindex).Text)
        If b_loadmusic = True Then
            If b_musicplaying = True Then
                Timer1.Stop()
                objAudio.Stop()
                b_musicEnding = True
                Button1.Text = "Play"
            End If
        End If
        Try
            objAudio = New Audio(str_filepath, False)
            b_loadmusic = True
            b_musicplaying = True
            Timer1.Start()
            objAudio.Play()
            Button1.Text = "Pause"
        Catch ex As Exception
            b_loadmusic = False
            b_musicplaying = False
        End Try
    End Sub
#End Region


There will be part III for the final part of coding.                                                                         Please feel free to ask any questions if you don't understand this method or any coding here.    


(PS: Sorry for my noob code block. I don't really know html much although my website is fully customized in html and CSS only.)



Saturday, January 29, 2011

My first step-by-step tutorial on Simple Music Player (VS2008) Part I

     After I posted more than 10 tutorials, I found out that it might not be useful to those beginner. So this is my first step-by-step tutorial. I hope that it will be useful to everyone. This tutorial might not be completed in only one post but at least two posts. For now, I haven’t decide about the exact number of posts I going to use.

    Tutorial start here:

    Requirements : 1) Visual Studio 2008 (VS2008) or Visual Basic 2008 Express Edition (VB2008).

                               2) Newest Direct X recommended.  Download from here.

    First, start either VS2008 or VB2008. A start page will appear. Click on create project from start page or go to File | New Project. For VS2008, choose Visual Basic | Windows | Windows Form Application while for VB2008, just simply choose Windows Form Application. Choose a name for your application and click OK. In this tutorial, I will be using Simple Music Player.

(Remarks: I am using VS2008)

1

Picture for New Project Dialog

 

2

After click on OK button, this should be the screen that appear

    After that, drag a button from toolbox to the form. If you can’t find toolbox, go to View | Toolbox. Toolbox will appear on the left. Follow the following picture and place four buttons to the same position. To change the button text to the same in the picture, go to the properties. Properties should be on the right but if it is not, go to View | Properties. To change button text, click on the button and look for text in the properties (The red square area on the right). Change the four buttons text to “Play”, “Stop”, “Previous”, “Next”. (ps: sorry for the wrong arrangement in the picture.)

3

Properties:

Button1 - text:Play    - anchor:bottom,left
Button2 - text:Stop    - anchor:bottom,left
Button3 - text:Next    - anchor:bottom,left
Button4 - text:Previous    - anchor:bottom,left

Anchor is use to lock the position and size of any object that being drag into the form.

(Tip: In order to get the same size of button, drag only one button to the form. Copy the button and paste it to the form for the rest buttons.)

     Add two listviews to the form from the toolbox. Listview1 is use to show the music name while listview2 is use to store the music file path. Listview2 will be invisible by setting the properties of visible to false. Resize listview1 to any size that you like.

7

Properties:

listview1 - allowdrop:true         - anchor:top,bottom,left,right      - view:list      - multiselect:false
listview2 - visible:false

     Add a timer into the form. Timer will be appear under the form. Change the Interval to 1. Interval is the time for timer event to elapsed. This is calculated by milliseconds. In this project, timer is use to check for ending of song and also starting the next song.

13

 

      Here, you will need to have DirectX SDK in order to proceed. There are few methods to play Music with VB.net. In this tutorial, I will guide using DirectX method. In order to use DirectX, you will need to add reference to the project. Go to Project | Add Reference and the following dialog will appear. Look for Microsoft.DirectX.AudioVideoPlayback and click ok.

5

 

This will be the first part of the tutorial. This part includes only interfaces but not coding. The next part will be coding. I not sure how long do I need to complete it but I will try my best to complete it asap. Please comment for any problems.

Monday, January 10, 2011

Music Player in vb.net

     As from the title, I had created a music player with playlist, random shuffle, songs repeat and drag drop functions. I had include a video showing how to use the music player.

My Music Player Video

For those who want to download the music player, please visit my website.

Friday, January 7, 2011

My first C sharp (C#) 2010 program - Paper Scissors Stone game

     As someone as me to make my previous program, Paper Scissors Stone game in C#, I tried and now it is working. This is actually my first program with C#. I though everything will be the same as VB.net but now I found out that there are difference keywords from VB.net.

     Well, I work it out using visual basic 2010. It looks exactly the same as from my previous VB 2008 but this time, I change the reset all button to reset directly instead of doing clear status and clear score separately. And for those who want to have the coding, as usual, I will upload it to my website. Just leave your comment for any problems.

image

Monday, January 3, 2011

A new start

       2011 had came and 2010 had passed.  It will be a new start for everyone to continue with their dreams and plans. Dreams and plans might not go through as expected but if without any effort, we won’t know. To achieve our dreams and plans, we should put all our effort on it and even though we might not able to achieve it, never mind. We can try it again and again until we meet our target or we can come out with another better plans.

       Today, I would like to wish everyone happy new year. I hope that everyone had a good start and also unforgettable countdown last few days ago. In this year, I hope that I could start with a new tutorial in programming of Android. I am still trying to learn but hopefully, I could master it in next month. I will be happy if someone could also guide me in Android.