Playing Sound & Video
Visual Basic tutorials for UWP apps.
1 post
Page 1 of 1
Sound and Video
You can play sound or video in two different ways the first by using a MediaElement control in XAML just like this:
The other way of playing sound or video is by creating a media element in code like this:
The volume property on both goes from 0 to 1 where 0 is the no sound and 1 is maximum volume level 0.5 would be in the middle.
Basic simple and useful for adding background music, sound fx and video into your apps.
You can play sound or video in two different ways the first by using a MediaElement control in XAML just like this:
Code: Select all
After adding the MediaElement you can adjust it size, location using properties and set other options such as media source file, playback speed, auto play, looping and volume. You can access the MediaElement via code but you need to give it a name first I gave this one the name 'BoomBox' and you can control it in your code like this:<MediaElement x:Name="BoomBox" Margin="0" Source="NameOfMediaFile.ext" />
Code: Select all
All properties are accessible via code. BoomBox.Play()
BoomBox.Stop()
BoomBox.Pause()
BoomBox.IsLooping = True
BoomBox.Volume = 1
The other way of playing sound or video is by creating a media element in code like this:
Code: Select all
I had some trouble with the volume option in that code as it didn't seem to be working correctly the first few times I tried it and I then realised that you had to set the volume before setting the source to make it work - very odd lol Dim BoomBox As MediaElement = New MediaElement()
Dim BBFolder As StorageFolder = Await Package.Current.InstalledLocation.GetFolderAsync("FolderName")
Dim BBFile As StorageFile = Await BBFolder.GetFileAsync("NameOfMediaFile.ext")
Dim BBStream = Await BBFile.OpenAsync(FileAccessMode.Read)
BoomBox.Volume = 1
BoomBox.SetSource(BBStream, BBFile.ContentType)
BoomBox.Play()
The volume property on both goes from 0 to 1 where 0 is the no sound and 1 is maximum volume level 0.5 would be in the middle.
Basic simple and useful for adding background music, sound fx and video into your apps.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023