need help planning out this program

TheDude05

Limp Gawd
Joined
Jan 27, 2005
Messages
393
I'm wanting to write a simple media player application for a game my friends and I play. Heres what I'm wanting to do.

Add a bunch of mp3 files to a list view control. That listview will be our playlist. When I press the start button I want the program to enumerate the list and play the song for 60 seconds then change to the next one. (If you're in college you may have guessed the game :p )

So here the first few roadblocks.

1) Managed Directsound cannot playback mp3s. I need a library of some sort that can playback mp3s, preferrably a managed one as I've never done any c++

2) I need to figure out a way to go down this list correctly. Ive made a class for my song files with a FileInfo object and a few other instance variables. I was thinking that when a file gets added to my program, it goes into a List<Song> object but I'm not entirely sure how to keep the listview and this object in sync.

So far those are my first few problems. I'm sure ill have more later. This is more of a learning exercise than anything.
 
Yea thats no problem. I'm talking more about changing the order of the listview, changing a property in the listview that should be reflected in the corresponding object, removing that object from the playlist, etc
 
Why do you have a list collection separate from the list control itself? The list control itself will let you hold a reference to an arbitrary object in each of its list item members.
 
mikeblas said:
Why do you have a list collection separate from the list control itself? The list control itself will let you hold a reference to an arbitrary object in each of its list item members.

Was not aware you could do that. Is it with the ListViewItem class?
 
Yes. ListViewItem.Tag holds a reference to whatever object you'd like.
 
Awesome solves that problem. Now a new one... threading

I understand the concept of threading, but I've never done any programming with threads. Heres my current logic.

I create an Iterator to go over the objects. Get object from ListItem and play it in new thread. My play method takes two parameters, start and how long to play. Now pause for 60 seconds then continue loop (or maybe until thread dies?)

I can get songs to play all at once but I don't know how to make my loop pause for 60 seconds without calling a sleep(). I have timer callbacks to my form so I cant sleep it.

I'm not sure this is the best way or can even be achieved so please comment. Keep in my mind I'll also want functionality to pause/stop my music. Thats what is making me think my while loop wont work out right.
 
Ok here is my progress so far...

I have a class interfaces MCI which I can use to pause, stop, play the music. I also have a way to wait for the file to finish playing by checking its status. I just need a new thread so I can use my form. However, when I try to make a new thread I get exceptions because I was making un-safe calls. If I make a new thread inside the loop I get an error which say it can't find device, more than likely because it was in a thread. Therefore I need some help making it thread safe. I'll post my code later. For now I need to sleep
 
TheDude05 said:
Awesome solves that problem. Now a new one... threading
Is that your way of saying "thank you"?

TheDude05 said:
I can get songs to play all at once but I don't know how to make my loop pause for 60 seconds without calling a sleep(). I have timer callbacks to my form so I cant sleep it.

You also shouldn't sleep in your main thread because that thread is responsible for responding to UI. That is, it should be available to interacxt with the user in case they want to cancel, click on the form to change something, or so on.

I think I'd end up with two threads. One would work the UI. It wouldn't sleep. It would kick off another thread to actually play music. The music thread would run, play as much music as it needed, then send an event back to the main thread asynchronously to let it know it was done. The music thread would also listen for a cancel event which gets signalled when the application is closed or when the user wants to cancel the current song (to skip to the next one, for instance).

When the UI thread got the done event, it would find the next song to play and keep going. Meanwhile, it's interacting with the user by processing messages -- and not sleeping or blocking on anything.
 
mikeblas said:
Is that your way of saying "thank you"?

Yea, sorry. I didn't mean to sound ungrateful because I am really thankful for your help.

You also shouldn't sleep in your main thread because that thread is responsible for responding to UI. That is, it should be available to interacxt with the user in case they want to cancel, click on the form to change something, or so on.

I think I'd end up with two threads. One would work the UI. It wouldn't sleep. It would kick off another thread to actually play music. The music thread would run, play as much music as it needed, then send an event back to the main thread asynchronously to let it know it was done. The music thread would also listen for a cancel event which gets signalled when the application is closed or when the user wants to cancel the current song (to skip to the next one, for instance).

When the UI thread got the done event, it would find the next song to play and keep going. Meanwhile, it's interacting with the user by processing messages -- and not sleeping or blocking on anything.


When you mean two threads, are you counting my main thread? Also, do you think the BackgroundWorker class could accomplish what I want?
 
Yes, two threads includes your main thread. In a WinForms app, you want the main thread to be the UI thread.

I can't comment on the fitness of BackgroundWorker for your application because I don't know what you're doing to play your media files.
 
mikeblas said:
Yes, two threads includes your main thread. In a WinForms app, you want the main thread to be the UI thread.

I can't comment on the fitness of BackgroundWorker for your application because I don't know what you're doing to play your media files.

I actually didn't end up having to do anything multi threading. I have a class that calls functions from Windows MCI which I'm guessing is natively threaded because it wouldn't block the code. Eventually I'm going to want to get away from MCI and use some other library so I'll probably be writing those threads later on. (MCI sometimes doesn't play certain mp3's back well)
 
Back
Top