我想在桌面上打开一个Wav文件来播放声音。 这在UWP应用程序中是否可行?
var mediaElement = new MediaElement();
//StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
var uri = new System.Uri(filename);
var converted = uri.AbsoluteUri;
StorageFile storageFolder = await StorageFile.GetFileFromApplicationUriAsync(uri);
var file = await storageFolder.TryGetItemAsync(filename) as IStorageFile;
if (file != null)
{
StorageFile soundfile = await storageFolder.GetFileAsync(filename);
mediaElement.SetSource(await soundfile.OpenAsync(FileAccessMode.Read), soundfile.ContentType);
mediaElement.Play();
}
这是我的文件名。这是完整的路径。
D:\develop\visualstudio2\local\sound\sound1.wav
这是我的要求。
答案 0 :(得分:0)
假设您的应用程序“media / test.wav”的根目录中有一个wav文件
var element = new MediaElement();
var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("media");
var file = await folder.GetFileAsync("test.wav");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
element.SetSource(stream, "");
element.Play();
答案 1 :(得分:0)
我成功了!
这是步骤
好的,我们走吧。
首先,选择一个文件。
public async void Click_SettingCommonPathSoundFile1()
{
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".wav");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// save to the file name to Sqlite at here.
await file.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
然后再玩。
public async void Click_SettingCommonPathSoundFile1Play()
{
// get file name from database.
await playit("sound1.wav");
}
和..
private async Task playit(string filename)
{
var element = new MediaElement();
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
var file = await storageFolder.GetFileAsync(filename);
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
element.SetSource(stream, "");
element.Play();
}