Like, can I make a program that works like that of winamp or can vb only play certain files?
Printable View
Like, can I make a program that works like that of winamp or can vb only play certain files?
Its not VB that plays the files, but the Windows API. Search for mciSendString.
But for something a little closer to your level of programming you should just drag and drop the MediaPlayer component (Ctrl+T) into VB and start from there....
Afetr you get more experienced then you can move on to declarations....:wave:
The best solution. :thumb: Paste this code into a class file and declare it as
Private MyPlayer as New ClassNAme
then you can access it with,
MyPlayer.Stop
The post that I got it from had smilies enabled, so it did something similar to about 3 lines in the code, so if you get an error, it should be either ":)" or ";)" (without quotes).
Enjoy. :)
VB Code:
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength 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 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function ReleaseCapture Lib "user32" () 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 Const SWP_NOMOVE = &H2 Private Const SWP_NOSIZE = &H1 Private Const HWND_TOPMOST = -1 Private Const HWND_NOTOPMOST = -2 Private TheFileName As String Private Function IsItPlaying() As Boolean Static yn As String * 30 mciSendString "status MP3Play mode", yn, Len(yn), 0 IsItPlaying = (Mid$(yn, 1, 7) = "playing") End Function Private Function mp3Play(FileName As String) Dim cmdToDo As String * 255 Dim dwReturn As Long Dim ret As String * 128 Dim tmp As String * 255 Dim lenShort As Long Dim ShortPathAndFie As String If Dir(FileName) = "" Then mmOpen = "Error with input file" Exit Function End If lenShort = GetShortPathName(FileName, tmp, 255) ShortPathAndFie = Left$(tmp, lenShort) glo_hWnd = hwnd cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias MP3Play" dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&;) If dwReturn <> 0 Then 'not success mciGetErrorString dwReturn, ret, 128 mmOpen = ret MsgBox ret, vbCritical Exit Function End If mmOpen = "Success" mciSendString "play MP3Play", 0, 0, 0 End Function Private Function mp3Pause() mciSendString "pause MP3Play", 0, 0, 0 End Function Private Function mp3Unpause() mciSendString "resume MP3Play", 0, 0, 0 End Function Private Function mp3Stop() As String mciSendString "stop MP3Play", 0, 0, 0 mciSendString "close MP3Play", 0, 0, 0 End Function Private Function PositionInSec() Static PIS As String * 30 mciSendString "set MP3Play time format milliseconds", 0, 0, 0 mciSendString "status MP3Play position", PIS, Len(PIS), 0 PositionInSec = Round(Mid$(PIS, 1, Len(PIS)) / 1000) End Function Private Function Position() Static P As String * 30 mciSendString "set MP3Play time format milliseconds", 0, 0, 0 mciSendString "status MP3Play position", P, Len(P), 0 sec = Round(Mid$(P, 1, Len(P)) / 1000) If sec < 60 Then Position = "0:" & Format(sec, "00") If sec > 59 Then mins = Int(sec / 60) sec = sec - (mins * 60) Position = Format(mins, "00") & ":" & Format(sec, "00") End If End Function Private Function LengthInSec() Static L As String * 30 mciSendString "set MP3Play time format milliseconds", 0, 0, 0 mciSendString "status MP3Play length", L, Len(s), 0 LengthInSec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(s, 1, Len(s))) / 1000) End Function Private Function Length() Static L As String * 30 mciSendString "set MP3Play time format milliseconds", 0, 0, 0 mciSendString "status MP3Play length", L, Len(L), 0 sec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(l, 1, Len(l))) / 1000) If sec < 60 Then Length = "0:" & Format(sec, "00") If sec > 59 Then mins = Int(sec / 60) sec = sec - (mins * 60) Length = Format(mins, "00") & ":" & Format(sec, "00") End If End Function Private Function TimeRemaining() As String RSecs = (LengthInSeconds) - (PositionInSec) RMins = Int(RSecs / 60) RSecs = Format((((RSecs / 60) - RMins) / 1.67), "0.00") * 100 If RSecs > 59 Then RMins = RMins + 1 RSecs = 0# Else: RMins = RMins RSecs = RSecs End If If RSecs < 10 Then TimeRemaining = RMins & ":0" & RSecs Else: TimeRemaining = RMins & ":" & RSecs End If End Function Private Function SeekTo(Second) mciSendString "set MP3Play time format milliseconds", 0, 0, 0 If IsItPlaying = True Then mciSendString "play MP3Play from " & Second, 0, 0, 0 If IsItPlaying = False Then mciSendString "seek MP3Play to " & Second, 0, 0, 0 End Function Private Function FastFoward(Second) Second = (PositionInSec + Second) * 1000 mciSendString "set mpeg time format milliseconds", 0&, 0&, 0& If IsItPlaying = True Then mciSendString "play mpeg from " & Second, 0&, 0&, 0& Else: mciSendString "seek mpeg from " & Second, 0&, 0&, 0& End If End Function Private Function Rewind(Second) Second = (PositionInSec - Second) * 1000 mciSendString "set mpeg time format milliseconds", 0&, 0&, 0& If IsItPlaying = True Then mciSendString "play mpeg from " & Second, 0&, 0&, 0& Else: mciSendString "seek mpeg from " & Second, 0&, 0&, 0& End If End Function Private Function SetVolume(Channel As String, VolumeValue As Long) As String Dim cmdToDo As String * 128 Dim dwReturn As Long Dim ret As String * 128 Dim VolumeV As Long VolumeV = VolumeValue If VolumeV < 0 Or VolumeV > 100 Then SetVolume = "out of volume" Exit Function End If VolumeV = VolumeV * 10 If LCase(Channel) = "left" Or LCase(Channel) = "right" Then cmdToDo = "setaudio mpeg " & Channel & " Volume to " & VolumeV Else cmdToDo = "setaudio mpeg Volume to " & VolumeV End If dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&) If Not dwReturn = 0 Then 'not success mciGetErrorString dwReturn, ret, 128 'Get the error SetVolume = ret Exit Function End If 'Success SetVolume = "Success" End Function Private Function Pitch(Value As Integer) As String Dim dwReturn As Long Dim ret As String * 128 Dim Pval As Long Value = Value - 100 Pval = 1000 + Value dwReturn = mciSendString("set mpeg speed " & Pval, 0&, 0&, 0&) If Not dwReturn = 0 Then 'not success mciGetErrorString dwReturn, ret, 128 'Get the error Pitch = ret Exit Function End If Pitch = "Success" End Function
That code needs serious fixing. First of all, where is Option Explicit? There are tons of undeclared variables... and when I started to go through fixing it, I ended up getting confused what the *censored* it is doing. TimeRemaining for example, it does something weird in there. And why all the return values are string? And why there are functions even if those don't return anything? Should be a sub then.
Just some annoyances in that code ;)
VB Code:
RSecs = Format((((RSecs / 60) - RMins) / 1.67), "0.00") * 100
What this line is doing again? Anyways, I'm converting it like this:
RMins = RSecs \ 60
RSecs = RSecs Mod 60
If the original RSecs were the time in seconds, this should work.
lol@Merri....
Thank you....you just proved my prior statement...
And @ PC_Madness.... what are you trying to do the kid with all those codes?
The kid just found out what boolean is and you're gonna lay this sort of coding on him?
(Not to mention the codes are incorrect...)
:cool:
Well, the code does work (I guess, I didn't check it before I started to fix it). The problem is how it works and what it returns. It is far from optimal. And boy do people like optimal... :D
Edit Here we go again!
VB Code:
If sec < 60 Then Position = "0:" & Format(sec, "00") If sec > 59 Then mins = Int(sec / 60) sec = sec - (mins * 60) Position = Format(mins, "00") & ":" & Format(sec, "00") End If
Dear lord...
More edit I don't mean to be insultive, it just bugs me so much to see such a code. Or maybe I'm just evil?
see, this is the type of overwhelming things I'm talking about. Why the hell is there so many things just to play a simple mp3?!
Oh and thanks protocol. I know someone gots mah back :cool:
I'm now working with a "better" class, which is fully commented and should be easy to use as well. I guess I make a sample program too. Atleast I can say I made it, since it is all different from the one posted above :D
And yeah, it is made hard, but this is because there are so many things one might want to know (such as playback position, playback length and so on).
Wonder if I go as far as getting tags... or, even, saving tags. That'd be cool to implement :rolleyes:
To play an MP3 is a bit complicated. You only see the Play and Stop buttons in Winamp, the code for Winamp is actually quite LARGE.Quote:
Originally posted by SandmanSamR
see, this is the type of overwhelming things I'm talking about. Why the hell is there so many things just to play a simple mp3?!
Oh and thanks protocol. I know someone gots mah back :cool:
I was working on an MP3 class in C++. I haven't worked on it for a while, but the declarations of everything alone is a good 100-200 lines of code. Not to mention the ID3 v1, v1.1, and v2 Tag reading code. I haven't finished the whole playing part, lol.
Anyway, there isn't going to be a very easy solution like
You first need to decide how you want to play it. Win API? Direct Sound? ect.... there are many ways. When you determine that, you will then need to learn how an MP3 file is made and works so you can write the code to play it.Code:PlayMP3("Slayer.mp3")
Seeing that you are new, I would suggest a different project. Perhaps a text editor or something? That way you'll learn the language much better before tackling such a project.
Just to get it stright, your gonna add stuff like
right?VB Code:
'this does this because of this
Yes that is commenting.Quote:
Originally posted by SandmanSamR
Just to get it stright, your gonna add stuff like
right?VB Code:
'this does this because of this
Quote:
Originally posted by kasracer
I was working on an MP3 class
I just started seeing class used in explinations, I am unfamiliar with the term. how, what, and why is it used? Normally I would first go look it up but its kind of hard looking up the word class without thousands of irrelevent results
Seeing that you are new, I would suggest a different project. Perhaps a text editor or something? That way you'll learn the language much better before tackling such a project.
Common now, Text editing? well yeah I guess so, seeing as how thats what we recently learned. Recently being couple weeks ago but still. I dont like the pace the class because 1/2 the class is learning slower then others, by a lot!
I was just making sure, no harm no foul right?
VB6 does not have classes. Classes are in OOP languages (Object Oriented Languages).
VB.NET, C++, Objective C, C#, Java, ect.... are OOP languages.
Ok, "Class Module". There you go smarty :D
"Class Module"? How can it be a class if there is no OOP in VB6? :confused:Quote:
Originally posted by Merri
Ok, "Class Module". There you go smarty :D
so class module is not the same as class? I would fuiger them to be the same :confused:
VB does have classes. The language is just not class oriented.
And here we have the MP3 class I've been working on. The class module and a simple project demonstrating how to use it.
I included a proper error handler. It records all errors for programmer's convenience.
And as a btw, I found atleast one mp3 which it didn't play. It seemed to work ok, but I didn't hear anything. I don't know if this is a bug in MCI or in my sound drivers. All other mp3s I tested played just fine.
You're welcome to report any errors you find :)
Edit Fixed GetStatus problem, it now returns the correct status.
More edit This versiom has no ID3v1 support, scroll below for newer code.
I downloaded it, and I'm going to check it right after I reinstall VB6.. I've been meaning to make some autoplay's for MP3 CDs that I've made in the past, but I didn't like the Media Player Control and it's options that much.. so, I'll get back to you.
This new version has a reading support for ID3v1 tags. Enjoy :)
lol
@Merri....Just goes to show you that postcount does not make the Guru...;)
I have yet to find an MP3 tag class that supports MusicMatch feature for album imaging....when that is done, to the codebank it goes.....
@ kasracer: 100-200 lines of declarations?!!
What are you creating an OS?
And a new version out. Now you can edit and save ID3v1 tags. Ain't that fun? Also, fixed an error in the genres list (values were one too big) and did some other minor enhancements somewhere. I can't find a bug, so its ready enough for another upload :D
And, well, guru... I'm good only with some things. Maybe a guru of generic coding or something like that. And well, I haven't been all that much in here, so that's why my post count is so small. I'm no MadBoy who posts about anything and everything he gets his mind into before even thinking about trying doing it himself ;) Ok, that was a bit over, but it is a half-truth, since he HAVE posted somewhat twice as much in two months as I have in the entire time I've been here (registered the first time in early 2001, add about 200 to my post count).
Uhhuh, this is going too much off-topic, so please don't answer to this. Otherwise we gotta go to Chit Chat.
Maybe I'm able to work on ID3v2 tags too. That requires a lot more work though, since I have no proper code ready for it atm and v2 tags are far more complicated to read (and especially write) than v1 tags.
Also, I happened to find www.fmod.org - maybe I can make a similar class module for that DLL to ease the life of a VB programmer, since the DLL is free. I tested it and it worked like a charm. Ideal for playing audio. It supports OGGs and modules too :)
declarations do not only mean the API's. they are also public variables, types, UDT's.Quote:
Originally posted by Protocol
@ kasracer: 100-200 lines of declarations?!!
What are you creating an OS?
so combined, it probably WOULD be 100-200 lines.
I've been reading the ID3v2 specification and I'm pretty sure I understand it completely by now. Complex format compared to ID3v1, but I managed to get it after some ten hours of reading and pondering. Of course I did some other things in the while. I'm pretty sure I'm now able to write the code (I wrote some types while going through the document and fixed them as I understood more... atm I need only to write the code that does the dirty work and reads the tag into memory).
I expect to have a new version of the class module to be out by tomorrow, with full ID3v2 support. Full means you can get any information from the tag, be it pictures or whatever it can have. I think the next bigger problem I have is CRC-32 since there can be CRC-32 data... as for encryptions and compressions, I'm leaving those for the programs to handle. There are just so many different ways to do it.
Quote:
Originally posted by Protocol
lol
@ kasracer: 100-200 lines of declarations?!!
What are you creating an OS?
First of all, That is a phat ass program! Thank you!!!!
a couple questions though, when I first opened it, it said something was wrong with this line:I just unloaded everything, opened project as new and just added the form and the class into the already opened project and it seems to have fixed that error message.VB Code:
Dim MyMP3 As New MP3_CLASS
Also, everytime I open a new song it says error "263|The specified device..." why does that keep opening?
Do you think with my "know-how" I can add a playlist to that? and if so, what about a randomize order and/or play?
I would rather try it before you go off and do it unless you think its to much of a task for me to handle
No offence intended, but it's called PROGRAMMING and not playing with toysQuote:
Originally posted by Protocol
@ kasracer: 100-200 lines of declarations?!!
What are you creating an OS? [/B]
SandmanSamR:
Yeah, I just noticed I forgot .FRX files from the zip file. And nobody told me I had done so. I don't update the zip before I get ID3v2 support done. Which is going on nicely, I just managed to read some of my files properly with it :D Still, it needs some expanding to be fully functional...
First of all, does the song play? If it does, you can ignore the error. The error detection is maybe reacts too easily to things... but atleast it is quite good :rolleyes:
Then some small coding tutorials :)
To go on with a simple playlist:
- add a listbox
- instead of placing a filename into txtFile textbox, add it to the listbox
- modify code in butOpen to use List1.List(List1.ListIndex) instead of txtFile.Text to open files
To have a random song playing when a song ends:
- find the following line in StatusUpdate_Timer:
Now, remove everything after Then and add butClose_Click, List1.ListIndex = Int(Rnd * List1.ListCount) and butOpen_ClickVB Code:
If Val(Status) = 4 And Length = Position Then MyMP3.SeekTo 0: MyMP3.PlayMP3
You might get same song twice, as you don't keep any track on what have been played. You might encounter some errors too, since there is no error checking (such as if there are listitems at all in the listbox).
Happy learning :)
Voilá! I have now coded a simple ID3v2 tag support. It isn't perfect... yet. Wonder if this will be ready this year? Two days to go...
What I added:
- flexible read and storage routines for ID3v2 tags
- UTF-16 decoder (and the other character codings should work too)
- tons of comments
Maybe not that much done, but it was a hard job to get this far. ID3v2 isn't of the easiest formats... I guess its only popular because WinAmp uses it. Oh well, the hardest part is done. Now it needs only some fixing so that it is 100% standard compliant... it reads most tags just fine.
If you find a problematic song, please let me know.
All he has to do is stick it into a class file and then he doesn't have to see it again. ;)Quote:
Originally posted by Protocol
And @ PC_Madness.... what are you trying to do the kid with all those codes?
The kid just found out what boolean is and you're gonna lay this sort of coding on him?
(Not to mention the codes are incorrect...)
:cool:
and to do that, he just needs to know what a class module is, how to use one, how to access one, how to initialize one :DQuote:
Originally posted by Pc_Madness
All he has to do is stick it into a class file and then he doesn't have to see it again. ;)
He also needs to edit the code so that it would work (the code has set all functions to Private! not accessible outside the class!), remove the parts that don't work (Pitch and SetVolume for the least), remove the unused API commands (GetShortPathName, SendMessage, ReleaseCapture, SetWindowPos), remove the unused constants, edit the file opening so that instead of using shortpathname API (because it doesn't work) it uses Chr(34) & Filename & Chr(34)...
Umm... Am I going too far with listing what is wrong with the code...? I just hate code that doesn't work. There is too much of it in this world. Back to fixing mine.
Merri, you are my hero. I've been trying to figure out V2 tags for 2 months now.
Few comments:
The button with the ellipsis on it should probably say something like "Browse for MP3".
Once a file is selected there, why not open it immediately on return from the common dialog? By "Open" I mean, read & display the tag info.
Why an "Open" button? This seems to start playing (I don't have speakers on this PC). Shouldn't the "Play" button do that?
Why a "Close" button?
Wouldn't hurt to use the ToolTipText to add some help.
Will you be adding V2 tag editing?
Thanks and Happy New Year to all. Looking forward to the next release. DaveBo
Sorry, I'm not concentrating to the actual program. I wanted to make it more clear how many different commands there are to play files. Like, play-button could be also the pause/resume button... but since these are three different commands, I wanted to point it out visually. It is a sample program and I'm concentrating on the Class Module itself.
V2 editing is coming once I've fixed some things. I've already fixed an error in ID3v1 loading plus I've fixed some other things... atm I'm working CRC-32 support, since it is possible there is a file with CRC-32 checking. Now if I happened to have a CRC-32 enabled file, I'd be super-happy. I just have to make the support in theory... in the other hand, I gotta do a lot of things to be working in theory.
Expect an update in a few hours :)
Here I come again with an another update. I made the ID3v2 reading support more complete, now it reads almost any ID3v2 enabled file (the only thing missing is reading of synchronized frame data, which I probably leave unimplemented - it would be a pain to reformat the code now).
- added CRC-32 support
- made error detection more complete: you now know where the error comes from
- possible to get header information
- fixed two-three stupid bugs (not sure if all of them were in the last version)
- even more comments!
- did I have UTF-16 support in the last version? Anyways, it is there now
Once again, enjoy! I think I'm now ready to go on with ID3v2 writing :)
Should I start a new topic for this?
I am learning a thing or two if it means anything to you. I got a question though, whats the purpose of adding the common dialog box?
This topic is fine. If it wasn' t for more people posting I would say yeah, make a new one. As it is, people are aware of the updates so you dont really have to. Ultimatly it is up to you though, if you wish to show everyone on this board what you have done I have no problem with that
I use Common Dialog Box to show the file opening dialog. It was the easiest way to do it. It can open only one file at a time though. I have a module that lets open multiple files at a time, showing the exactly same Dialog, but I didn't want to use it in this example. Wanted to keep it as simple as possible.
Yet, I'm planning on making a more complex sample program, since ID3v2 is complex... and there is no way going around it. Maybe I make multiple sample programs on different possibilities of the Class Module (one as a simple MP3 player with ID3v1 view and a ID3v2 file browser or something like that).
what about scrolling text inside a textbox? would I just doVB Code:
Private Sub Timer1_Timer() Filename.Left = Filename.Left - 50 If Filename.Left <= -2160 Then Filename.Left = Filename.Left + 50 If Filename.Left >= 1920 Then Filename.Left = Filename.Left - 50 End Sub
I just made it so that instead of the text moving inside the box, the whole textbox moves. I just made a frame to put it in so it doesn't overlap the other things and it looks cool :bigyello: