How To Play Sounds and Music in WPF - Ged Mead's Blog - vb. City. Introduction This is a re- post of an item that I had on my old blog. As it seems to continue to get a lot of hits (when the site is up) I thought I would transfer it over here to the new site. It is more of a "here's how" approach than a "why it works that way" one. Also I will be creating everything in code, not markup. There are several more approaches and advanced techniques available via XAML that I won't be covering here. PART 1 : The Sound. Player. 1. Hard Coded File Path Starting with possibly the least realistic scenario, you can use the Sound. Player to play a . Imports System. Media Private Sub Button. Click(By. Val sender As Object, By. Val e As System. Windows. Routed. Event. Args) Handles Button. Click Dim player As New Sound. Player("C: \Temp\4. WAY. WAV")player. Play() End Sub Note the Imports statement at the top. This wave file will play asynchronously (I do wish someone would come up with an easier to type word than that! - it's nearly as bad as 'concatenation' ) But I digress.. If it's a particularly large wav file, you can help things out by inserting a Load statement after the initialization and before the Play fires. This will tee the player up to play without any kind of pause while it cranks up the file. This is something well worth bearing in mind as we users have become more and more impatient of delays. Use a Content File One particularly useful approach which gets around all the potential problems of hard coding file paths is to add the file to your Solution Explorer items. You can do this with the standard "Add Existing Item" selection from the IDE menu, adding the wav file directly to the list of files for the Project. This now brings me to Frustration #1. First, here's valid code to play the sound using a Sound. Player object: Private Sub Button. Click(By. Val sender As System. Object, By. Val e As System. Windows. Routed. Event. Args) Handles Button. Click Dim player As New Sound. Playerplayer = New Sound. Player("SND0. 10. WAV")player. Play() End Sub Now, before we move on, let's check the Solution Explorer: As you can see the file name is correctly entered and that file is sitting in the Solution Explorer's list of files that (you would think) it knows about. The key to fixing this problem is the 'Copy to Output Directory' Property. By default, this is set to 'Do Not Copy'. You need to change this to 'Copy Always' and you'll be back on track. Audio File as Resource (Frustration #2) I wasn't going to include this option because it has generally caused me more trouble than it's worth (especially bearing in mind that there are several relatively trouble- free alternatives). However, you can add a wav file as a Project Resource as normal and then play that Resource via a Stream. The syntax is: Private Sub btn. Resource_Click(By. Val sender As System. Object, By. Val e As System. Windows. Routed. Event. Args) Handles btn. Resource. Click Dim player As New Sound. Player(My. Resources. Play() End Sub Even with small, well tested files I often found that using this approach I would get a lot of unacceptable sound distortion (actually additional unrelated sounds to that expected). I have tried it on several PCs, each with different sound card configurations and the problems occur on them all. Other Sound. Player Features In addition to the standard Play method, the Sound. Player has a Play. Sync method - in case you should rather unusually want the system to be locked until the sound has finished playing. And finally if you want a sound to be repeatedly played, you can use the Play. Looping method to drive people mad; sanity can be restored by inserting the Stop method! PART 2: Media. Player and Media Element As you've already seen, the Sound. Player has some limitations. It only understands wav format files and you can generally only play one wav file at a time (unless you start to get quite fancy with multithreading). In many cases, the Media. Player or Media. Element will be a better option, but there is a good reason why I included the discussion of the Sound. Player above. In some situations on particular systems you will have problems getting the two Media components to work properly. So it's always useful to know how to use the lighter weight Sound. Player if you run into these problems in the future. But I Want to Play MP3 Files! - or WMA or MID, etc. And this is a job for either the Media. Player or Media. Element. Keeping to my approach of doing all the work in code, I'm therefore going to home in on the Media. Player. (If you have a need to create a media object in XAML and have it play, pause, stop, trigger, etc, only in XAML then the Media. Element will be your tool of choice.) Before abandoning the Media. Element approach altogether, I should point out that you can create the Media. Element in XAML and then access it's properties and methods in code. However, there is no advantage that I know of and in fact, the syntax is slightly more verbose and it also caused Frustration #4 - the fact that the audio file will only play once and then needs to be 'reset' by calling its Stop method; the Stop method apparently has the effect of returning the playhead to the beginning of the file. So let's look at the Media. Player in WPF. It has an Open method which loads the file in the form of a URI. This was Frustration #5 for me, as it took me a while to get past the problem of the "Value of Type 'String' cannot be converted to 'System. URI.'" error. It took a lot more than a quick search to finally discover that I needed simply to add the URIKind argument to the Open method; so the considerable amount of time I spent trying to do casts was wasted. But maybe that's something I should have known. It also has Play, Pause and Stop methods, plus the more sophisticated Balance and Volume properties. There is another small Gotcha involved in using this control. Take a look at the following code snippet: Code Copy Dim mplayer As New Media. The vMix Software Video Mixer and Switcher is the complete live video. then vMix is for you. Check out our Solutions page to see how vMix can fit into your. JW Player is live on over 2 million sites. Developers & Partners Integrate video technology into your core. JW Plans Let us help you find the package that. Playermplayer. Open(New Uri("Godfather. Uri. Kind. Relative))mplayer. Play() It's hard to see anything wrong with that code snippet, but if you do try it you'll find that the audio will play for a short period of time and then - for no apparent reason - stop. This is Frustration #6. As I understand it, what happens here is that the new Media. Player object is created, passed a URI for its source and then instructed to play. Play begins but at that point seemingly the Media. Player closes itself, saying "I've set the audio away, so my work here is done". The end result is that at some unpredictable point in time the closed Media. Player is released from memory and will be disposed. And of course as soon as it no longer exists there is no means for the sound to continue to be played. So it stops in mid note! Anyway, having dragged you laboriously through that explanation, you'll probably already have realised that the fix is simple. All you need do is instantiate the Media. Player outside the scope of the click event so that the Media. Player will continue to exist throughout the lifetime of that Window or Page. In other words, like this: Class Window. Dim mplayer As Media. Player Private Sub Button. Click(By. Val sender As Object, By. Val e As System. Windows. Routed. Event. Args) Handles Button. Clickmplayer = New Media. Playermplayer. Open(New Uri("Godfather. Uri. Kind. Relative))mplayer. Play() End Sub. End Class As with the Sound. Player sample, the mp.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |