I'd like to play .mp3 or wav sounds in my uwp app, I need to play it only when app is open and without any media element on the ui. Is there a possibility to make some threads to play separate songs at the same time. Any relevant info is appreciated.
Adapting the above answer slightly using MediaElement. Presumes you have a media file at the root of your application in MyFolder/MySound.wav
var element = new MediaElement();
var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("MyFolder");
var file = await folder.GetFileAsync("MySound.wav");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
element.SetSource(stream, "");
element.Play();
May be this code snippet will help you out.
MediaElement PlayMusic = new MediaElement();
StorageFolder Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
Folder = await Folder.GetFolderAsync("MyFolder");
StorageFile sf = await Folder.GetFileAsync("MyFile.mp3");
PlayMusic.SetSource(await sf.OpenAsync(FileAccessMode.Read), sf.ContentType);
PlayMusic.Play();
Related
I am trying to save an captured photo into a folder as following:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
StorageFile photo = await
captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null)
{
// User cancelled photo capture
return;
}
StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
However, I cannot find the ProfilePhotoFolder in my file system. Could any one please tell me where is this folder. This is an Universal Windows project.
When you write to UWP LocalFolders the data is placed into Isolated Storage which is only accessible by your application, and thus you won't simply be able to fire up Windows Explorer and start digging into the files.
The recommended way for Windows 10 devices is to set your device to Developer Mode then use the web browser interface they've created (see https://blogs.windows.com/buildingapps/2016/06/08/using-the-app-file-explorer-to-see-your-app-data/#6O50PWljxSKfKCAm.97).
Try:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
Docs
Currently i'm working on UWP apps, In that i need to download the pdf and word url to the system downloads folder, for that need i'm trying backgrounddownloader class as shown in below code.
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
An also i'm trying another way to download the urls here is the code
var request = WebRequest.CreateHttp(selectedfile.DocumentPath);
var sampleFile = await KnownFolders.PicturesLibrary.CreateFileAsync(selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
var cli = new HttpClient();
var str = await cli.GetStreamAsync(request.RequestUri);
var dst = await sampleFile.OpenStreamForWriteAsync();
await str.AsInputStream().AsStreamForRead().CopyToAsync(dst);
For using above two methods url can be downloaded as pdf and word but i'm trying to open the pdf file and word file but it couldn't open, it showing error like as shown in below figure trying to open the open downloaded file it shown like this. Can any one help to solve this issue. I want to download the file and also downloaded file should be open.
The BackgroundDownloader.CreateDownload method initializes a DownloadOperation object that contains the specified Uri and the file that the response is written to.
When the DownloadOperation is created, it does not starts the download operation.
We should be able to use DownloadOperation.StartAsync to start the download operation.
For example:
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync("ab.pdf", CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await download.StartAsync();
If you want to know more detail about down load file, please refer the Background transfer sample.
I am trying to play a sound file when I click on a button. The file is located in the Assets folder and the build action is set to content. I tried the following:
Option 1:
element.Source = new Uri("ms-appx:///Assets/alarm.wav");
Option 2:
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("alarm.wav");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
element.SetSource(stream, file.ContentType);
The second option works, but it takes a long time before the sound plays when I click on the button. According to other questions the first option should work, but the sound is not played and there is no exception.
Any suggestions?
EDIT:
I am using a Xamarin template for cross-platform development. So I have the following structure:
App (PCL)
App.Droid
App.iOS
App.UWP
App.WinPhone
With DependencyService I try to play sound on each platform, but with App.UWP option 1 doesn't work.
you can try this way
Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Assets/alarm.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
I'm trying to play songs from a local machine by using the directory path to the song.
MediaElement.Source = new Uri(#"D:\Music\Artist\Album\Song.mp3", UriKind.Absolute);
Is this even possible to get to work or can Windows 8 apps only use URI schemes like mss-appx: to access package data?
When I try and run the code I get a message on the MediaElement control "Invalid Source"
Windows Store apps do not have full access to the file system. They can directly access (by path) only limited locations (i.e. their install and applicationdata folders).
The MediaElement can load items from paths it can directly access, but this is not generally useful since these locations have URIs (ms-appx: and ms-appdata:) which will target the right location regardless of what the actual Path is.
Typically songs are in the Music library, which the MediaElement cannot directly access. It can get brokered access through the MusicLibrary capability, but that doesn't allow access by path. The app will need to get to the file through the KnownFolders object:
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder musicLib = Windows.Storage.KnownFolders.MusicLibrary;
StorageFile song = await musicLib.GetFileAsync(#"Artist\Album\Song.mp3");
var stream = await song.OpenReadAsync();
me.SetSource(stream, stream.ContentType);
}
If the song isn't in a library that can be permitted by capability then the user will need to grant permission through a FolderPicker or such. The user can pick the root of the music location and the app can cache that with the Windows.Storage.AccessCache classes so the user doesn't need to pick the folder multiple times or individually pick files.
I discuss this in more detail in my blog entry Skip the path: stick to the StorageFile
You need to use the file open picker to select a file in D drive.
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStreamWithContentType content = await file.OpenReadAsync();
Debug.WriteLine("Content Type: " + content.ContentType);
player.SetSource(content, content.ContentType);
This link has the answer (in case you can use the application folder)
MediaElement.Source = new Uri("ms-appx-web:///Assets/Song.mp3", UriKind.Absolute);
I'm developing metro app using C# and XAML,As we all know we can search music files in music library using below code.
IReadOnlyList<IStorageItem> itemsList = await KnownFolders.MusicLibrary.GetItemsAsync();
What about music files in other drives(partitions) like D:,E: etc..Is there any way to search other drives for music files?
Brief: I want to search .mp3 files from my hard-drive and display them in Gridview, is there any efficient way to achieve this? Please help me.
It´s possible to search within other folders or drives, but it´s required to use the FolderPicker to select the StorageFolder...
async Task<IEnumerable<StorageFile>> FindMusicFiles()
{
var folderPicker = new FolderPicker()
{
CommitButtonText = "Yippie!",
SuggestedStartLocation = PickerLocationId.ComputerFolder,
ViewMode = PickerViewMode.List
};
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFileQueryResult queryResult = folder.CreateFileQuery();
queryResult.ApplyNewQueryOptions(new QueryOptions(CommonFileQuery.OrderByName, new[] { ".mp3" }));
IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync();
return files;
}
return new StorageFile[] { };
}
Windows Store apps are restricted to search within KnownFolders or LocalFolder of the application, so you cannot search within other drives
It's also possible for the app to remember the folder(s) you've selected with the FolderPicker, see my post on Windows 8 App and access to file system where I've explained how to use FutureAccessList...