Pratush Pandita
Pratush Pandita

Reputation: 43

Playing a music file in the music library

how do I play a song from the music library? I tried this:

private void click_AlarmSet(object sender, RoutedEventArgs e)
{
    play();
}  

async private void play()
{
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

    openPicker.SuggestedStartLocation =Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
    openPicker.FileTypeFilter.Add("toxic.mp3");

    var file = await openPicker.PickSingleFileAsync();
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

    sound.SetSource(stream, file.ContentType);
    sound.Play();
}

here sound is my media element and "toxic.mp3" is the mp3 file I want to play, but the mp3 is not playing.

Upvotes: 1

Views: 519

Answers (1)

beard
beard

Reputation: 91

openPicker.FileTypeFilter.Add("toxic.mp3")

I think that the FileTypeFilter is looking for ".mp3" or ".wav" not a specific file name. http://msdn.microsoft.com/en-us/library/windows.storage.pickers.fileopenpicker.filetypefilter.Aspx

Suggestion:

var name = "toxic.mp3";
var file = await openPicker.GetFileAsync(name);
var stream = await file.OpenAsync(FileAccessMode.Read);
sound.SetSource(stream, file.ContentType);

-Or- If you wanted all the '.mp3'

openPicker.FileTypeFilter.Add(".mp3")

Upvotes: 1

Related Questions