karate_kid007

hi i've created a screensaver with background music in the form of an mp3, the problem i have is that i can't seem to make the song loop over and over again until the screensaver closes, is there a simple piece of code that will make this work.

here is the code i have used so far

"Defining Audio"

Dim song As Audio

"Loading The Song when the screensaver starts"

song = New Audio("C:\Users\karate_kid007\Documents\Visual Studio 2005\Projects\ScreenSaver1\ScreenSaver1\My Project\killbill.mp3", True)

"Playing The Song when the screensaver starts"

song.Play() -- Here is where i want the song to loop until the screensaver ends

"Ending The Song when the screensaver starts"

song.Stop()

many thanks in advance

luke



Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

field33

You can try a timer. Make the time the same as the length of the MP3. Add then song.Play() in the timer1_tick block and try it.

Regards,

field33.






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

that sounds good but i wouldn't know how to setup the timer to the same time as the song and then get it ot tick sorry but im a real novice at this vb if you could please elaberate and provide code if possible, many thanks

luke





Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

field33

karate_kid007 wrote:

that sounds good but i wouldn't know how to setup the timer to the same time as the song and then get it ot tick sorry but im a real novice at this vb if you could please elaberate and provide code if possible, many thanks

luke

Can I get some info about your song Then I will give you the good settings for the timer Wink




Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

js06

What you are looking for is the following:

audio.duration

and

audio.position

you can store the both of these in a variable and compare the 2

you can use labels to show the values for testing

label1.text=audio.duration.tostring

label2.text=audio.position.tostring

you whole code would look something like this

public class form1

dim dur as integer

dim pos as integer

playbtn click event

dur = audio.duration

label1.text = dur.tostring

audio.play

timer1.start

update the postition with the timer tick event

timer tick event

pos = audio.position

label2.text = pos.tostring

if pos >= to dur then

audio.stop

audio.play

end if

add a timer to your form

set your timer interval to 1000






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

yeah sure mate

format: .mp3

length: 2mins and 29secs

Misc: i am using it as a background track in my screensaver hope this helps

thanks for your time much appreciated

luke





Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

hi js06,

thank you very much that code seems to work and is just what i was looking for thank you for taking time to reply and your reply is much appreciated. thank you.

luke





Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

js06

No problem

here is a link to example projects for audio and video and a ton of other good stuff

http://www.vbcodesource.com/exampleNet.html

this is where i picked up the audio code

if you look at the code in the audio projects you can filter out the base parts to get what you need

some of the projects have more functionality built in for balance and volume etc... and it can be confusing

just look for the key parts and you can eliminate most of the clutter

look for these particular ones

Music Module v2.4 with Example (very good)

Audio/Video Direct X 9.0 Example - VB.NET 2005 Release Version (very good)

directSoundExNet

playWaveFileEx-Beta2






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

js06

Originally for one of my projects i was going to just use a timer to stop restart the song after it reached a specified number of seconds

so a quick way to do it would be to manually stop and start the timer so to speak

example:

if your song is 2:29 then your total seconds would be 149

you could set your timer to stop at say 150 and then restart the song

dim seconds as integer = 0

timer tick event

seconds = seconds +1

if seconds = 150 then

audio.stop

audio.start

end if

this is cheating a little and it's not good if you want flexibility in what files you play but it works

anyway, i thought this might help you somewhere down the line






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

thanks i will check out the link when i get a moment, your advice and knowledge is priceless, thank you. and other information you think would be useful would be good, cheers

luke

p.s do you know if you can use animated gif files as background pics so each picture in a screensaver is made up of many pics thanks





Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

js06

I don't know much about that

have you tried loading one as a background to test it

I just loaded an animated gif in a picturebox on a form and it works just fine when you run it

i would think that you should be able to use one in your screensaver

but i think it might not be as simple as it sounds

unless you have your pictures set up to show at certain intervals where it can show the animation and then move on to the next pic

example:

if you have a gif that shows it's animation over a period of 2 seconds then show all your pictures for a duration of 2 seconds

I don't know how you are created the screensaver or how you have it set up but you might just be able to use a picturebox on a form that's full screen and use a timer to update the pictures every 2 seconds

if you have a specific order that you will show the pictures then you could use a varible where you update the timer for each picture with the timer tick event and then you can change the timer interval to have enough time to show the next gif or pic

example:

dim picnumber as integer = 0

timer1 tick event

picnumber = picnumber +1

if picnumber = 1 then

timer1.interval = 2000 ' this will give you 2 seconds to show the gif

elseif picnumber=2 then

timer1.interval = 3000 ' 3 seconds for the next one

etc...

that could be a lot of code if you have a a bunch of pictures

that why i first suggested to use a more general time of 2 seconds or so to handle all the pics and gifs

keep in mind that i have not created any screensavers but i think you could make this work

you may have to get creative on this one

anyway, hope this helps you

if you get something going with it, please post some code for the rest who may be interested in making screensavers






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

thank you i'll give it a try and if i can get it working i'll let you know and post the code on here for everyone else cheers.

luke





Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

Tall Dude

Here's yet another variation:

Code Snippet

Imports Microsoft.DirectX

Imports Microsoft.DirectX.AudioVideoPlayback

Public Class Form1

Friend WithEvents m_Audio As Audio

Dim filen As String = Nothing

Private Sub tmrPosition_Tick(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Timer1.Tick

Label1.Text = (m_Audio.CurrentPosition / 60).ToString("00.00")

Label2.Text = (m_Audio.Duration / 60).ToString("00.00")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles btnPlay.Click

If filen IsNot Nothing Then

If btnPlay.Text = "Play" Then

' Start playing.

btnPlay.Text = "Stop"

m_Audio = New Audio(filen)

AddHandler m_Audio.Ending, AddressOf Me.MusicEnds

m_Audio.Play()

' m_StartTime = Now

Timer1.Enabled = True

Else

' Stop playing.

btnPlay.Text = "Play"

m_Audio.Stop()

RemoveHandler m_Audio.Ending, AddressOf Me.MusicEnds

m_Audio.Dispose()

m_Audio = Nothing

Timer1.Enabled = False

Label1.Text = ""

Label2.Text = ""

End If

End If

End Sub

Private Sub OpenFile_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles OpenFile.Click

Dim ofd As New OpenFileDialog

ofd.Filter = "Music Files (*.MP3;*.WMA;*.WAV;*.MID;*.WMV) |*.MP3;*.WMA;*.WAV;*.MID;*.WMV"

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

filen = ofd.FileName

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

Label1.Text = ""

Label2.Text = ""

btnPlay.Text = "Play"

End Sub

Private Sub MusicEnds(ByVal sender As Object, ByVal e As System.EventArgs)

' The following line causes the song to loop.

' You can surround it with an

' If check box checked bla bla bla ... Then...

' To make the loop function optional

m_Audio.CurrentPosition = 0

End Sub

End Class






Re: Visual Basic Express Edition how do you get an mp3 to loop untill told to stop

karate_kid007

thanks Tall Dude i'll try this method out and if it works then i will mark it as the answer, cheers for replying to my post,

luke