Relative Uri works for BitmapImage, but not for MediaPlayer?

若如初见. 提交于 2019-12-13 03:39:36

问题


This will be simple for you guys:

var uri = new Uri("pack://application:,,,/LiftExperiment;component/pics/outside/elevator.jpg");
imageBitmap = new BitmapImage();
imageBitmap.BeginInit();
imageBitmap.UriSource = uri;
imageBitmap.EndInit();
image.Source = imageBitmap;

=> Works perfectly on a .jpg with Build Action: Content Copy to Output Directory: Copy always

MediaPlayer mp = new MediaPlayer();
var uri = new Uri("pack://application:,,,/LiftExperiment;component/sounds/DialingTone.wav");
mp.Open(uri);
mp.Play();

=> Does not work on a .wav with the same build action and copy to output. I see the file in my /debug/ folder..

MediaPlayer mp = new MediaPlayer();
var uri = new Uri(@"E:\projects\LiftExp\_solution\LiftExperiment\bin\Debug\sounds\DialingTone.wav");
mp.Open(uri);
mp.Play();

=> Works perfectly..

So, how do I get the sound to work with a relative path? Why is it not working this way? Let me know if you want more code or screenshots.

Thanks.


回答1:


The pack://application URI syntax is for "embed" files, make sure the the media file is set to that, or use the pack://siteoforigin for "loose" files (copied to bin directory).

MSDN link



来源:https://stackoverflow.com/questions/2625608/relative-uri-works-for-bitmapimage-but-not-for-mediaplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!