0

I've looked up a lot of threads in here, none have helped. I can't seem to play sound files from resources using MediaElement. SoundPlayer works but it's very limited. It doesn't support pause or resume, or anything important.

 class Songs
    {
        static MediaPlayer Player = new MediaPlayer();
        public static void PlaySong()
        {
            Player.Open(new Uri(@"pack://application:,,,/Songs/RandomSong.mp3"));
            Player.Play();
        }
    }

Something like this doesn't give any errors, but doesn't play the song.

Is it possible to play from resources using MediaElement? If not, what do I do?

Sound
  • 113
  • 5

1 Answers1

0

How about SoundPlayer?

Define your audio file(s) as Resource

var player = new SoundPlayer(Application.GetResourceStream("/YourAssemblyName;component/Songs/RandomSong.mp3").Stream);

player.Play();
DeMama
  • 1,134
  • 3
  • 13
  • 32
  • SoundPlayer is very limited. It doesn't support Pause or Resume – Sound Sep 09 '20 at 18:34
  • Try changing "pack://application:,,,/Songs/RandomSong.mp3" with "/YourAssemblyName;component/Songs/RandomSong.mp3" – DeMama Sep 09 '20 at 18:36
  • Sadly, doesn't work. – Sound Sep 09 '20 at 18:44
  • Are you sure you changed "YourAssemblyName" with the name of your project? – DeMama Sep 09 '20 at 18:45
  • Yes, I have changed that. It just gives an invalid url exception – Sound Sep 09 '20 at 18:46
  • Try setting the build action to `Embedded Resource` for your audio files. More than probably either your build action is wrong or your `Uri` is wrong. – DeMama Sep 09 '20 at 18:57
  • I've created a folder inside my Project and I've already put the song as Resource at the build action. Here is how my Uri looks `Player.Open(new Uri("/MusicProject;component/Songs/RandomSong.mp3"));` – Sound Sep 09 '20 at 19:04
  • Sadly it seems like there's no easy way to achieve this. See this: https://stackoverflow.com/questions/13348897/read-audio-file-to-stream-with-mediaplayer-wpf – DeMama Sep 09 '20 at 19:25