media element doesn't play audio in windows ph

2019-09-21 16:55发布

问题:

Possible Duplicate:
adding media element in windows phone 7?

media element doesn't play audio even correctly giving the properties.

回答1:

It does work.

In code:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

var stream = TitleContainer.OpenStream("beep.wav");
                var effect = SoundEffect.FromStream(stream);
                effect.Play();

N.B. "beep.wav" must be configured as "Content".

In app constructor add:

this.ApplicationLifetimeObjects.Add(new XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));

Also add the following class:

public class XNAAsyncDispatcher : IApplicationService
{
    private DispatcherTimer frameworkDispatcherTimer;

    public XNAAsyncDispatcher(TimeSpan dispatchInterval)
    {
        this.frameworkDispatcherTimer = new DispatcherTimer();
        this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);
        this.frameworkDispatcherTimer.Interval = dispatchInterval;
    }

    void IApplicationService.StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
    void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }
    void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }
}