Answered by:
how to play a sound in metro app?

Question
-
I'm trying to build an app that plays a sound after an item in the gridview is clicked, I've added the sound to the Assets folder in my solution and I've tried two ways to do this:
Platform::String^ voicePath("Assets/town.mid"); auto sound = ref new MediaElement(); auto uri = this->BaseUri->CombineUri(voicePath); sound->Source = uri; sound->Play();
and:
Platform::String^ voicePath("town.mid"); create_task(Windows::ApplicationModel::Package::Current->InstalledLocation->GetFolderAsync("Assets")).then([this, voicePath](StorageFolder^ folder) { auto s = folder->Path; create_task(folder->GetFileAsync(voicePath)).then([this](StorageFile^ file) { create_task(file->OpenAsync(FileAccessMode::Read)).then([this, file](Streams::IRandomAccessStream^ stream) { auto sound = ref new MediaElement(); sound->SetSource(stream,file->ContentType); sound->Play(); }); }); });
in this case the app is crashing, I've tried to debug it and I've found out that the town.mid file is not found in the AppX\Assets folder, I don't know why.
but even when I'm copying the file to the folder the sound in not playing, and the same goes for the first method.
Sunday, August 19, 2012 7:48 AM
Answers
-
Hello,
Your path schema is incorrect. That path schema will work in XAML but will not work from code. The proper path schema from code is:
ms-resource:/Files/Assets/filename.ext
Also please keep in mind that the Media element is not designed to play very short audio clips without being setup properly. Because of this we highly recommend that C++ developers use XAudio 2 for very short audio clips and sound effects. XAudio 2 is specifically designed for sound effects.
I hope this helps,
James
Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/
- Proposed as answer by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 22, 2012 10:50 PM
- Edited by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 22, 2012 10:52 PM added additional info
- Marked as answer by shmulikel Friday, August 24, 2012 7:51 AM
Wednesday, August 22, 2012 10:50 PMModerator
All replies
-
If you have N version of windows (european version), you need to install Media Feature Pack :
http://www.microsoft.com/en-us/download/details.aspx?id=30685
Sunday, August 19, 2012 1:56 PM -
I don't know what version I have but it didn't allow me to install this pack, and I do have media player so I can play this sound file outside my app.Sunday, August 19, 2012 2:10 PM
-
You may try to put town.mid in the top folder instead of Assets folder. Before I seems to have the similar toruble in Assets folder.
Charlie Chang L
Monday, August 20, 2012 1:46 AM -
didn't work tooMonday, August 20, 2012 7:05 AM
-
Hello,
The .mid format is not supported in new Windows 8 style app.
You can check the list.
http://msdn.microsoft.com/en-us/library/windows/apps/hh986969.aspxTherefore, you cannot play this file directly.
But you can use Media Foundation to support this format, but you need to write some codes by yourself. Please follow this sample code
http://code.msdn.microsoft.com/windowsapps/Media-extensions-sample-7b466096Best regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
Monday, August 20, 2012 11:10 AM -
thanks, I've converted the file to mp3 and now I can see the file in the AppX\Assets folder, but still no sound is playingMonday, August 20, 2012 1:22 PM
-
Hello,
Your path schema is incorrect. That path schema will work in XAML but will not work from code. The proper path schema from code is:
ms-resource:/Files/Assets/filename.ext
Also please keep in mind that the Media element is not designed to play very short audio clips without being setup properly. Because of this we highly recommend that C++ developers use XAudio 2 for very short audio clips and sound effects. XAudio 2 is specifically designed for sound effects.
I hope this helps,
James
Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/
- Proposed as answer by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 22, 2012 10:50 PM
- Edited by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 22, 2012 10:52 PM added additional info
- Marked as answer by shmulikel Friday, August 24, 2012 7:51 AM
Wednesday, August 22, 2012 10:50 PMModerator