[RESOLVED] Is there something that acts and loops like sndPlaySOund, but can play .mp3 files?
I need something that can loop perfectly like sndPlaySound does, but i also need something that can play .mp3 files... I dont want to have to use a control though. Any ideas?
If i have to use a control(like the ones in the tools...), then let me know.
The problem is that the .wav files are just WAY to big. I put them into a .res, and it makes the project HUGE. So, i want to try .mp3. Tried it with sndPlaySound(ik u cant use 'em. Just foolin around) and of course didnt work.... so.. yeah. plz help. i need this help so bad!
Last edited by Gamemaster1494; Jul 6th, 2010 at 10:11 PM.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
mmc uses mci, there is a "repeat" command for mcisendstring,
but if i'm not mistaken,
there is some delay between each loop.
you can try it, the command is:
Code:
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal HwndCallback As Long) As Long
Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
Public Const MAX_PATH = 260
Public LastMCIError As Long
Public Function LenZ(ByVal Str1 As String) As Integer
LenZ = InStr(1, Str1$, vbNullChar)
End Function
Public Function RTrimNull(ByVal Str1 As String) As String
Dim iNum As Integer
iNum = LenZ(Str1$)
If iNum <= 0 Then
iNum = InStr(1, Str1$, Chr(13))
If iNum <= 0 Then
RTrimNull$ = RTrim(Str1$)
Exit Function
End If
End If
RTrimNull$ = Left(Str1$, iNum - 1)
End Function
Public Function GetShortPath(ByVal Str1 As String) As String
Dim strBuf As String * MAX_PATH
Call GetShortPathName(Str1$, strBuf$, MAX_PATH - 1)
GetShortPath$ = RTrimNull$(strBuf$)
End Function
Public Function MCIGetError(Optional ByVal lErr As Long = -1) As String
Dim Str1 As String * 1024
If lErr = -1 Then lErr = LastMCIError
'retrieve the error string
Call mciGetErrorString(lErr, Str1$, 1023)
'strip off the trailing spaces
MCIGetError$ = RTrimNull$(Str1$)
End Function
Private Function Mci_(ByVal sCommand As String, Optional Ret As Long = vbNull) As String
Dim Str1 As String * 512
LastMCIError = mciSendString(sCommand, Str1$, 511, 0)
If Ret <> vbNull Then Ret = LastMCIError
Mci_$ = RTrimNull$(Str1$)
End Function
Function MCIOpen(ByVal fName As String, Optional ByVal sDevice As String = "mpegvideo") As Boolean
Dim Num1 As Long
Call Mci_$("open " & GetShortPath$(fName) & " type " & sDevice$ & " alias MediaStream", Num1)
Call Mci_$("set MediaStream time format milliseconds")
If Num1 Then MCIOpen = True
End Function
Sub Main
MCIOpen "my_mp3.mp3"
mci_ "play MediaStream repeat"
End Sub
Last edited by whatsup; Jul 7th, 2010 at 09:35 AM.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
for mp3 i think the repeat method can be good enough.
you can also try these methods:
using a timer and check the playing state.
using notify command, mci will call your form, when it finish playing.
i checked these 2 methods with tiny wav files and it doesn't work as i want,
sndplay does it for waves the best, except that it can't play more than one file per app.
but as i said for mp3, i think the mci is good enough.
you can also try other dlls,
on my signature there is a very good audio library, that has many features, like effects, etc...
also there is a very good app in the forum code bank, called "monoton".
this a full audio library, written in vb, and it's very good.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Put this code in module
Code:
Option Explicit
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Sub Pause()
Call mciSendString("pause mp3play", 0, 0, 0)
End Sub
Public Sub Play(MpegAudio As String)
Dim lngLen As Long, strShort As String * 255, strPlay As String
Call mciSendString("stop mp3play", 0, 0, 0)
Call mciSendString("close mp3play", 0, 0, 0)
lngLen = GetShortPathName(MpegAudio, strShort, 255)
strPlay = Left(strShort, lngLen)
Call mciSendString("open " & strPlay & " type mpegvideo alias mp3play", 0, 0, 0)
Call mciSendString("play mp3play", 0, 0, 0)
End Sub
Private Sub Resume_Play()
Call mciSendString("resume mp3play", 0, 0, 0)
End Sub
Private Sub Speed_Slow()
Call mciSendString("set mp3play speed 500", 0, 0, 0)
End Sub
Private Sub Speed_Normal()
Call mciSendString("set mp3play speed 1000", 0, 0, 0)
End Sub
Private Sub Speed_Fast()
Call mciSendString("set mp3play speed 1500", 0, 0, 0)
End Sub
Private Sub StopIt()
Call mciSendString("stop mp3play", 0, 0, 0)
Call mciSendString("close all", 0, 0, 0)
End Sub
Private Function length() As String
Dim LengthA As String * 30
Call mciSendString("status mp3play length", LengthA, Len(LengthA), 0)
length = LengthA
End Function
Private Function Position() As String
Dim PositionA As String * 30
Call mciSendString("status mp3play position", PositionA, Len(PositionA), 0)
Position = PositionA
End Function
Private Sub Mute(isMute As Boolean)
If isMute = True Then
Call mciSendString("set mp3play audio all off", 0, 0, 0)
ElseIf isMute = False Then
Call mciSendString("set mp3play audio all on", 0, 0, 0)
End If
End Sub
Private Sub PauseRsm(rSm As Boolean)
If rSm = True Then
Call Pause
ElseIf rSm = False Then
Call Resume_Play
End If
End Sub
Usage is here ... in the form write this code ...
Code:
Private Sub Form_Load ()
Play("c:\windows\help\mysound.mp3") ' your mp3 file path
End Sub
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Hmm........... well, i was kinda wanting to avoid the overall size of the game. im using the .res to hold the .wav's, then exporting them. With the .wav's, its a HUGE game. like 196MB.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
No. You don't understand. I don't want them to have to download something. I want to use the .mp3, for everything to be already there. I wanted to use the .mp3 to cut the size of the download for it. =S
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Manual repeat: Since you know the length of the tracks. Why not just store the length of each track? Then you can call "play" again after the duration(see post #4 or #7).
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Originally Posted by Gamemaster1494
Yeah. I have done that, but, it still had a delay. What if i put that code in a .dll? Would it go faster?
Did you actually check the difference between say, Timer, or GetTickCount(good, accurate enough for this)? Or did you iterate a 'static' variable in a timer(bad, not accurate enough)?
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Create a 'static' variable(or private, or a property), but instead of iterating it on a timer, you add the length of the track when the track plays(like sVariable = Timer + length_of_track). Then you simply check: If Timer >= sVariable Then PlayNextSong.
Timer is built in to VB, and is accurate to the nearest hundredth second.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Originally Posted by Gamemaster1494
I need something that can loop perfectly like sndPlaySound does, but i also need something that can play .mp3 files... I dont want to have to use a control though. Any ideas?
If i have to use a control(like the ones in the tools...), then let me know.
The problem is that the .wav files are just WAY to big. I put them into a .res, and it makes the project HUGE. So, i want to try .mp3. Tried it with sndPlaySound(ik u cant use 'em. Just foolin around) and of course didnt work.... so.. yeah. plz help. i need this help so bad!
the size problem also can be solved.
you can zip your files, then insert them to the res file,
and on run time, unzip them.
you can easily build the zip and unzip functions your self,
using for example zlib.dll
or use third party zip/unzip like 7-zip for example.
another simpler solution is to insert the files to the res, as they are,
and at the end, zip the big exe file, with upx, it will get much smaller,
as if you packed the files with zip utility.
Last edited by whatsup; Jul 11th, 2010 at 10:45 AM.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Are you sure your MP3 doesn't either have some blank lead-in or lead-out as part of the file? Using the mciSendString with repeat as part of the play command there should be no delay between the when the file ends and when the file starts again.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
I am positive MartT because i edited them with Audacity. No blank lead-in's or outs.
@FireXTol: I'm not certain what you are saying, but are you saying, to have a timer add to a variable, then if the variable is longer than the length of the track, start it over?
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
There must be more to your file than you think. Attached is a quick sample project that shows using repeat in the play command for mciSendString will play without a pause. Excuse the choice of mp3 but it was the first small file I found that didn't have a lead in and out to demonstrate with.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
MarkT. I have a feeling that due to the amount of code that i have in a different timer, that, it delays it to know when to loop. Because on my only timer, it has 1445 lines of code in it. Some of them are Calling a function.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
Also MarkT, there is a problem with your .zip. Says can't find project or library. and highlights strFile = IIf(Right(App.Path, 1) = "\", "Duck.mp3", "\Duck.mp3")
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
No, IIf is a function. About the only place I have found I like to use it is in this example where you are testing app.path. In this case I know that app.path will not end with a \ so we can just hardcode it here.
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
It recognizes the Right function because i lowercased it, and went away from that code and it automaticly uppercased it..... Hum... werid. Now it is highlighting Chr and saying the same thing.... wth is wrong with my vb!?
Re: Is there something that acts and loops like sndPlaySOund, but can play .mp3 files
You have something wierd going on. The Chr function is a common one. To get past this one remove the Length and Position functions in the module since they aren't being used.