Background audio not working in windows 8 store / metro app

Posted by roryok on Stack Overflow See other posts from Stack Overflow or by roryok
Published on 2012-11-21T12:56:10Z Indexed on 2012/11/22 11:01 UTC
Read the original article Hit count: 139

Filed under:
|
|

I've tried setting background audio through both a mediaElement in XAML

<MediaElement x:Name="MyAudio" Source="Assets/Sound.mp3" AudioCategory="BackgroundCapableMedia" AutoPlay="False" />

And programmatically

async void setUpAudio()
{
    var package = Windows.ApplicationModel.Package.Current;
    var installedLocation = package.InstalledLocation;
    var storageFile = await installedLocation.GetFileAsync("Assets\\Sound.mp3");
    if (storageFile != null)
    {
        var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
        _soundEffect = new MediaElement();
        _soundEffect.AudioCategory = AudioCategory.BackgroundCapableMedia;
        _soundEffect.AutoPlay = false;
        _soundEffect.SetSource(stream, storageFile.ContentType);
    }
}

// and later...
_soundEffect.Play();

But neither works for me. As soon as I minimise the app the music fades out

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-8