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();
Related
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();
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 am trying to add a .wav/.mp3 file to a project. I've added some pictures and they are working well, but I can't add any music. When I type the Uri like new Uri("file.mp3") and I put the file in the project directory it works, but this code below isn't (but it reads pictures O.o).
Uri jezus_uri = new Uri(#"pack://application:,,,/Resources/jesusrender.png");
Uri devil_uri = new Uri(#"pack://application:,,,/Resources/devilrender.jpg");
BitmapImage jesus_obraz = new BitmapImage(jezus_uri);
BitmapImage devil_obraz = new BitmapImage(devil_uri);
Uri muzyka = new Uri(#"pack://application:,,,/Resources/plig.wav");
MediaPlayer punch = new MediaPlayer();
punch.Open(muzyka);
punch.Play();
Build action is set to Resource of course. There is no error. Music is just not playing.
You cannot play sounds from a resource in a Media Player. You can only play it from a file. You may want to go with the approach of having your sound file deployed in the project directory and playing the file from the Media Player.
Alternatively, you can use the SoundPlayer to play sounds from a resource file.
SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav");
sndPing.Play();
Ref: https://msdn.microsoft.com/en-us/library/3w5b27z4%28v=vs.80%29.aspx
I'm trying to play a small video file in my windows phone application. Is pretty basic
void StartMediaPlayer()
{
MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
mediaPlayerLauncher.Media = new Uri("/Assets/video/video1.wmv", UriKind.Relative);
mediaPlayerLauncher.Location = MediaLocationType.Install;
mediaPlayerLauncher.Controls = MediaPlaybackControls.All;
mediaPlayerLauncher.Orientation = MediaPlayerOrientation.Landscape;
mediaPlayerLauncher.Show();
}
i call this void on image tap event, and this is what happend
the debugger show that the error is here: mediaPlayerLauncher.Show();
That error will be thrown the application can't find your file /Assets/video/video1.wmv in the install directory on the phone. Make sure that your video is in your project, and is set to build type "Content". You could open the XAP file to double-check it's in the correct relative location too (it's just a ZIP file archive renamed).
if some has the same problem, just a heads up the files should be in main directory
mediaPlayerLauncher.Media = new Uri("video1.wmv", UriKind.Relative);
I'm trying to launch a file (document, picture,...) from my Windows 8 app using the Launcher API but the file won't open with the default program associated with it.
Following code runs when clicked on a file:
AttachedFile file = e.ClickedItem as AttachedFile;
bool isLaunched = await Launcher.LaunchUriAsync(new Uri(file.Path, UriKind.Absolute));
//isLaunched is false
The specified path is an absolute path that works when pasting it into the File Explorer. (C:\Users...\file.txt)
Using the Launcher with a StorageFile returns an error because the app doesn't have the permissions to edit the file.
Do you need a programmatic access to the files outside of the local folder or the libraries? Sorry, there is no API for this.
var fold = Windows.Storage.KnownFolders.DocumentsLibrary;
var f1 = await fold.GetFileAsync("hi.txt");
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
bool success = await Windows.System.Launcher.LaunchFileAsync(f1, options);
Should add "capability in manifest", to use KnownFolders like DocumentLibrary,PictureLibrary,MusicLibrary...
Source:http://lunarfrog.com/blog/2011/10/03/winrt-storage-overview