c# - Playing sounds in WPF as a resource -
this question has answer here:
- how play wpf sound file resource 2 answers
first of all, i've searched site , looked @ this unfortunately did not me much.
whenever click frog image made, want play .wav file resource. here's code:
void newfrog_mouseup(object sender, mousebuttoneventargs e) { system.media.soundplayer player = new system.media.soundplayer(@"g:\cosr3s\frogsandlilypads\frogsandlilypads\sounds\frogcroak.wav"); player.play(); }
of course worked problem directory changes computer computer, thought i'd have play resource. don't know how that. new programmer, keep in mind.
i tried few variations based on aforementioned link , played around it. examples:
void newfrog_mouseup(object sender, mousebuttoneventargs e) { uri uri = new uri(@"pack://application:,,,/sounds/frogcroak.wav"); var player = new mediaplayer(); player.open(uri); player.play(); }
i tried doing this, although thought 1 wouldn't work, tried anyway:
void newfrog_mouseup(object sender, mousebuttoneventargs e) { uri uri = new uri(@"pack://application:,,,/sounds/frogcroak.wav"); system.media.soundplayer player = new system.media.soundplayer(uri); player.play(); }
i not sure if "pack://application:,,,
" refer file being played.
i tried make if image, so:
void newfrog_mouseup(object sender, mousebuttoneventargs e) { system.media.soundplayer player = new system.media.soundplayer(new uri(@"pack://application:,,,/sounds/frogcroak.wav", urikind.relativeorabsolute)); player.play(); }
so instead of new bitmapimage
did new system.media.soundplayer
, find out wouldn't work way.
edit: forgot mention worked, did not play sound.
void newfrog_mouseup(object sender, mousebuttoneventargs e) { uri uri = new uri(@"pack://application:,,,/sounds/frogcroak.wav"); var player = new mediaplayer(); player.open(uri); player.play(); }
edit2: possible duplicate technically didn't solve problem, according accepted answer not possible. there other way play audio using different method?
this works me using visual studio 2013 command line application:
- embed .wav file resource (resources.resx) in project
- i used add resource existing file
- set persistence file 'linked @ compile time'
reference file code using this. timer namespace code , notificationsound called embedded sound:
var notificationsound = new soundplayer(timer.properties.resources.notificationsound); notificationsound.playsync();
this allowed me play sound application after moving executable , renaming original sound file (just check embedded).