0

I currently have an mp3 player in which a Label should show the name of the selected song. As I have it set right now it shows the full path to the song instead:

label1.Content = media.Source.ToString();

As the song can be selected there is no set location or filepath mentioned in the code, letting the user select a song from wherever they want.

I've tried media.Name.ToString(); but it only gave me the word "media", so Im probably missing something simple. Would like some help!

EDIT: Here's an example of what I want to see: "“TestMp3.mp3 is playing”"

Also would need to show "paused" or "stopped" when it is.

Sorez
  • 19
  • 4
  • Possibly a duplicate? http://stackoverflow.com/questions/6505870/how-to-get-the-properties-of-a-mp3-file-in-c-sharp – Frank Alvaro Dec 15 '15 at 16:27
  • are you using the `System.IO` class at all.. ? if so you can use some of the `File and Path` functions to pull back the FileName property – MethodMan Dec 15 '15 at 16:28
  • Do you want to show the file name (as stated in the question title)? That could be done by [`Path.GetFileName(media.Source.ToString())`](https://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx). Or the song title? Then see ffa's comment. – René Vogt Dec 15 '15 at 16:29
  • The file's name (which in this case is TestMp3) I dont know exctly where to put Path.GetFileName, as its meant to be shown inside a label. – Sorez Dec 15 '15 at 16:32

1 Answers1

1

If you want to show only the file name instead of the full path, you may use

label1.Content = Path.GetFileName(media.Source.ToString());

using the static method GetFileName of the class System.IO.Path.

This will return test.mp3 from e.g. C:\MyFiles\Audio\test.mp3.

How to show if it is playing or paused depends highly on how you manage those states. Unfortunatly your question doesn't show what type media is of, so I don't know if the information is in there.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Woo! That solved the name part that's been driving me crazy, thanks! Now all I have to figure out is how to show whether it is playing, paused or stopped. media is a MediaElement type if thats of any importance. – Sorez Dec 15 '15 at 16:52
  • @Sorez Sorry can't help you there. `MediaElement` doesn't seem to populate that, and I don't know what you are doing in the rest of your application. And for the sake of SO: this would be a totally different question than this one, so you may want to open a new question for that. It makes it easier to find for those who may answer as well as for others who have the same issues. – René Vogt Dec 15 '15 at 16:56