3

I am developing a MP3 player in C# using WPF.

I have successfully added the play,pause,stop,next,previous and add file to the list functionality.

But every time i open the player, i have to manually add files using the add file button.

What i want is that, just like iTunes, on opening the player, all mp3 files should be loaded from a folder and shown in the 'listbox'. (Like a preloaded music library)

I do not intend to use any database.
This can be done using CSV file. I tried with the code specified here How to create CSV Excel file C#? but could not get it work correctly. (There were lots of problems and hence i am not specifying it here)

Any help is appreciated in this direction. I am open to new approaches if it solves my problem.

P.S. I also want to add the functionality of making custom playlist from this music library. More help in this direction is also welcome.

Community
  • 1
  • 1
Jagrut
  • 922
  • 7
  • 21
  • What have you tried so far? You're right a CSV file is a good approach...but I don't see, how we could help, if you don't show us your code... – DHN Jul 30 '13 at 07:16
  • I specified a link above. I used that code with the modification that File.WriteAllText was replaced by File.AppendAllText because WriteAllText replaces the older file with new one. Also i want to store only unique values in the CSV file. – Jagrut Jul 30 '13 at 07:21

2 Answers2

0

I would assume there are two ways you could approach this, depending on the scale of what you're trying to do.

If you really want to just list all the files in a folder, then use the Directory.GetFiles(path) method - you can filter out anything that's not a .mp3 file, and list them.

However, I've got the feeling you want to build individual playlists (correct me if I'm wrong), in that case, yes, you will need to store the list of files somewhere.

I'd avoid CSV, because you're limited as to the data that can be stored there. I'd go for XML. Create XML that looks something like -

<xml>
 <playlist name="playlist1">
  <file path="C:\file1.mp3"/>
  <file path="C:\file2.mp3"/>
  <file path="C:\file3.mp3"/>
 </playlist>
</xml>

Or store whatever you want in there. Creating XML files using XDocument is fairly straight-forward. You can then read the list of files from there.

Jamie Burns
  • 1,258
  • 9
  • 21
  • Yes you are right that i want to build individual playlist. But, as it is there in iTunes, you just select the audio files from main songs library and then drag and drop it in the playlist you created. Drag and drop is not necessary but yes creating something from main library is important. – Jagrut Jul 30 '13 at 07:42
  • 1
    Great stuff. If you can build up a list in C# of the files you want to include in the playlist, you can convert it to XML fairly easily (just do some research on XML creation in C#). How you build that list though is entirely up to you. Drag and drop should be possible though - MSDN has a nice article on it - [link](http://msdn.microsoft.com/en-us/library/ms742859.aspx) – Jamie Burns Jul 30 '13 at 07:47
0

As evilbhonda says, you can use XML but you can store it in IsolatedStorage that is available for WPF. how to use IsolatedStorage (storage available only for your applcia Isolated Storage simple demo List of tracks could be serialized into XML using DataContracts for example:

[DataContract]
public class MusicTrack
{
    [DataMember]
    public string TrackName { get; set; }
    [DataMember]
    public string TrackPath { get; set; }
}

Serialization and deserialization of DataContracts is described here -> Serialization and Deserialization of DataContracts

toszo
  • 76
  • 4