Uzair Ali
Uzair Ali

Reputation: 540

How to get mp3 file from resources in WPF

I want to get URI of default.mp3 from resources and play that file in System.Windows.Media.MediaPlayer
I included the mp3 file in resources folder as Content and copy always but I dont know how to play get the URI. I am currently doing this but its not working

 uri = new Uri("pack://application:,,,/Resources/default1.mp3");   // This is not working neither showing and error
 var player = new MediaPlayer();
 //MessageBox.Show(uri.ToString());
 player.Open(uri);
 player.Play();

Upvotes: 1

Views: 4994

Answers (2)

wally
wally

Reputation: 31

You can not apply file from resource to MediaPlayer in .Net. The music file should be placed in separatly file and Uri created with phisical file path.

Please, see the message details in MediaPlayer.MediaError event.

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157098

You have to set the 'Build Action' to 'Resource' in order to make the resource accessible through WPF.

You can change this in the properties pane of the file:

enter image description here

I got it working with these steps:

  1. Create resource file in Resources\default.mp3;
  2. Add resource file to project. Set Build Action to Resource;
  3. Use this code:

    Uri uri = new Uri("pack://application:,,,/WpfApplication1;component/Resources/default.mp3");
    

Upvotes: 0

Related Questions