DFBarracks - Forums




Delta Force Barracks Forums - Powered by XMB 1.9.11
Delta Force Barracks Forums
Not logged in [Login - Register]
Go To Bottom

Printable Version  
Author: Subject: Tutorial level: Begginer : Some Fun, make a sound player! (Done)
bullet magnet
Captain
****


Avatar


Posts: 263
Registered: 6-26-2005
Member Is Offline

Mood: Modding

[*] posted on 6-29-2007 at 01:52 AM
Tutorial level: Begginer : Some Fun, make a sound player! (Done)


Today we will be going over the basic steps and make a sound player.

We will first play just simple short WAV files from a button from your project files, then will progress into making our own windows media player control.

Play Sound From Resources

1.) Add two buttons and two labels if you wish. One will play from your resources, one from your computer. As you can see I already edited Form properties, since I can't stand to see Form1 at the top.


2.) Now we need to add a sound to our resources. Go to View, Solution Explorer.


Next, in the new window that popped up, select My Project


You should see a resources tab, select it, then select Add Resource (the small arrow beside it), Add Existing File, then choose a WAV file, I use Click.WAV from the df2 CD.


3.) OK time to write some code, double click the button that will play from project files (resources) and get into code view.

Now we usually type under the Private Sub Button1_Click, but this time we need to declare something first. So we will be typing right after the "Public Class Form1"

Type this:
Friend WithEvents mediasoundplayer As New System.Media.SoundPlayer

Now we go under the button1 click event and type the following code:

mediasoundplayer.Stream = My.Resources.CLICK
mediasoundplayer.Play

The above code selects what file we want to play, and we chose the file CLICK.WAV from our resources. We then type

mediasoundplayer.play

This will of course play the file.

You can choose to loop it by typing instead:

mediasoundplayer.PlayLooping()

End Result:


Now click the green run button at the top, and run your program, click the button :)

Play Sound From Computer

Alright this next part will cover playing a sound from your computer, which can be much more convenient. Make sure you have kept all the code from Part I.

1.) Double click Button2. Declare a variable As String, this will be the path to our sound file, see below:

Dim soundpath As String = "C:\Program Files\Novalogic\Delta Force 2\sound.wav

2.) We have now declared A Variable, that holds a file path. Let use it!

mediasoundplayer.soundlocation = soundpath

mediasoundplayer.play

3.) Again you can choose:

mediasoundplayer.PlayLooping()

to loop it.

End Result:


Again, test your program.

OK, the sound on loop can get annoying, so lets add a stop button, just put a button, and the code is:

mediasoundplayer.stop

Easy right?

Using Windows Media Player Control

OK, we are now going to use a Windows Media Player type soundplayer.

1.) If you notice, that control is not on the ToolBox, we have to add it manually.

To do this, right click in the tool box on an empty space, and choose "Choose Items"



Choose COM Components, and scroll down to windows media player.


Click OK, now the Windows Media Player will be on your form. Lets write a bit of code now, double click somewhere on the form, not on any controls.

2.) Write the following code under the Form1_Load Event, what it will do is store a file path as string:

Dim soundpath As String = "C:/Program Fiels/Novalogic/Delta Force 2/Sound.WAV"

Replace that file path with your own.

Now lets set a few properties of the Windows Media Player control.

Type this:
AxWindowsMediaPlayer1.Settings.AutoStart = False

This will keep the sound from playing automatically when the program starts.

Then we need to set what the Windows Media Player will play. Remember the path we set in a variable? Use that variable. Type this:

AxWindowsMediaPlayer1.URL = soundpath



Thats it, it only took three lines of code, play with and add more settings

AxWindowsMediaPlayer1.Settings.(select more settings)

Go experiment and check back for more,

Bullet Magnet






View user's profile View All Posts By User

  Go To Top

Powered by XMB 1.9.11
XMB Forum Software © 2001-2012 The XMB Group
[Queries: 15] [PHP: 48.9% - SQL: 51.1%]












Copyright © 2002 - 2008 All Rights Reserved - DFBarracks.com
Site Design by A.Bullet - ScreamingDemon.com