开发者

How to play a sound in Silverlight without XAML

开发者 https://www.devze.com 2023-01-26 23:43 出处:网络
Looks like MediaElement requires XAML to work. So this simple code doesn\'t work开发者_运维百科 :

Looks like MediaElement requires XAML to work.

So this simple code doesn't work开发者_运维百科 :

MediaElement me = new MediaElement();
me.Volume = 1;
me.AutoPlay = false;
me.Source = new Uri("http://www.robtowns.com/music/blind_willie.mp3");
me.Play();

Is there another way to play sound in Silverlight 4 with MVVM - or do I have to find a placeholder for the media element in my XAML ?

This thread describes how to use XNA for Win Phone 7 - but I'm talking regular Silverlight http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/60e7e4b4-31dc-4a81-bdfb-e2c80e761a1c


The control still needs to be in the visual tree to operate. If you don't want it to actually appear, you could have it collapsed. But you'll need to do something like:

me.Visibility = Visibility.Collapsed;
LayoutRoot.Children.Add(me);
0

精彩评论

暂无评论...
验证码 换一张
取 消