Mansinh
Mansinh

Reputation: 1435

Wp7: Doesn't play song with media element

I want to play audio song in my Windows Phone 7 application.

For playing song I am using a MediaElement.

and my C# code is

MediaElement song = new MediaElement();
song.Source = new Uri("Live Url of audio song", UriKind.RelativeOrAbsolute);
LayoutRoot.Children.Add(song);
song.AutoPlay = False;
song.Play();

It does't show any error and does not play.

Upvotes: 1

Views: 608

Answers (3)

MattyMerrix
MattyMerrix

Reputation: 11193

There seems to be a bug, whenever you set AutoPlay="False" so just dont do that and dont set the Source in the xaml if you dont want AutoPlay

    <MediaElement Name="mediaElementTones" Volume="1" ></MediaElement>

Then use in code

            Uri keypadUri = new Uri("./Resources/Sounds/dtmf" + keyPressed + ".wav", UriKind.Relative);
            mediaElementTones.Source = keypadUri;
            mediaElementTones.Play();

Upvotes: 0

Pking
Pking

Reputation: 963

The media element need to load its media before you can call Play(), otherwise nothing will happen. The event MediaOpened is fired when the media is loaded.

myMediaElement.MediaOpened += (o, args) => myMediaElement.Play();

Upvotes: 1

Mansinh
Mansinh

Reputation: 1435

I got Solution

i take medeiElement in XAML part..

 <MediaElement Name="myMediaElement" 
                      AutoPlay="True" 
                      />

C# code is

 Uri path;
 path = new Uri("/VodafoneAugmentedReality;component/Images/pause.png", UriKind.RelativeOrAbsolute);

myMediaElement.Source = new Uri(Uri, UriKind.RelativeOrAbsolute);
myMediaElement.Play();

Upvotes: 0

Related Questions