C
C#14mo ago
Tobeash

❔ How to play music files from resources/relative paths in wpf c# ?

I have spend good 2 to 3 hours trying to get a sound file played from relative path, it just doesnt work no matter what I try, I have tried mediaplayer, later found on stackoverflow it doesnt work from relative path so i switched to sounplayer, I have tried like 20 different codes lines I dont understand how is this so hard, is there like no support for playing sound? what i tried
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream stream = assembly.GetManifestResourceStream("Pomodoro.Music.taskdone2.wav");
System.Media.SoundPlayer player = new System.Media.SoundPlayer(stream);
player.Play();

try
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"Music\taskdone2.wav");
//SoundPlayer player = new SoundPlayer(audioStream);
player.Play();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}


string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Music\\taskdone2.mp3");
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(path, UriKind.Absolute));
player.Play();


string relativePath = @"./Music/taskdone2.mp3";
string absolutePath = Path.GetFullPath(relativePath);

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Open(new Uri(absolutePath, UriKind.Absolute));
mediaPlayer.Play();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream stream = assembly.GetManifestResourceStream("Pomodoro.Music.taskdone2.wav");
System.Media.SoundPlayer player = new System.Media.SoundPlayer(stream);
player.Play();

try
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"Music\taskdone2.wav");
//SoundPlayer player = new SoundPlayer(audioStream);
player.Play();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}


string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Music\\taskdone2.mp3");
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(path, UriKind.Absolute));
player.Play();


string relativePath = @"./Music/taskdone2.mp3";
string absolutePath = Path.GetFullPath(relativePath);

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Open(new Uri(absolutePath, UriKind.Absolute));
mediaPlayer.Play();
not even once was there a sound, only thing that worked was absolute path straight from c, which i dont know how could i use that for making an app that isnt always going to be in the same place
4 Replies
JakenVeina
JakenVeina14mo ago
uhm... convert your paths to absolute paths? Like you already seem to know how to do? I really don't understand the issue
Tobeash
TobeashOP14mo ago
there is something incredibly cursed with the pathing C:\Users\name\OneDrive\Desktop\code\C#\Pomodoro\Pomodoro\bin\Debug\net6.0-windows\ is my base directory there is no music no resources files despite everything online saying there should be stuff so everytime i try to access my recourses and actually connect the two it just doesnt work the music folder is ins C:\Users\name\OneDrive\Desktop\code\C#\Pomodoro\Pomodoro\Music and they are not getting copied despite being set to resources and copy always mkay you have to use embledebebded resources to actually get the folder copied to trhe bin folder so that the program can work it
JakenVeina
JakenVeina14mo ago
don't do embedded resource, no that one should cause the resource file to be encoded directly into the compiled DLL/EXE which you can then write code to extract at runtume all you need to do is set the "Copy to Output" flag on all those files, which you should be able to do, regardless of what compile action they're set to the most sensible compile action would be simply "Content"
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server