New Fashion Windows 8 Development (36): Play

Source: Internet
Author: User

This play to, I still use it as its original name, because I don't know how to translate it. Although it is translated as "played" on msdn, it always feels awkward.

What do I do with this ...... In short, it is generally used in the same network, such as a home group, where there are n Windows 8 computers and a computing is playing multimedia. At this time, streaming media can be sent to B, c, D. Play on other computers.

So what occasions do I think I will use it? Let's play a movie. For example, if I play a horror film on machine a now, we have a group of friends but the child is absolutely big. So, everyone said to a: Don't be so selfish. It's not fun to enjoy a horror movie alone, go to the office and open the projection device, where we can sit down and watch it together.

So, B went to the office, started the Win8 computer connected to the projection device, and enabled an application that could receive playto streams. Then, a selected the office computer on its computer as the target device, in this way, the horror film will be played on the computer in the office. You can sit together and watch it while drinking.

 

Obviously, to complete a playto, you must have two terminals, one sending and one receiving, and two machines. It's a bit sad. I only have one laptop with a Win8 system, and the PC was a magic machine 10 years ago. You know that Win8 cannot be installed. No way, the children of the poor can afford a notebook, in rural areas like ours, it costs to buy a computer. Therefore, I have spent more than 10 years on a computer.

As a result, I tried to test it on a machine. After N tests, it was a result multiple times-failed! Testing with a virtual machine also failed. I don't know why, but I'm working on it.

So, you may ask, can the same machine be tested? Of course, this is also available in the SDK documentation. Below I will introduce how to test playto on the local machine. The sender is okay, but fails to write a receiver.

 

I have a disadvantage: I do not like to talk about theory. So, check the instance.

1. Start your Vs and create a new project named "my oX B application ".

2. refer to the following XAML to renovate the mainpage. XAML. You can also press Ctrl + C directly. It doesn't matter. I won't sue you for plagiarism.

<Page X: class = "app1.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app1" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <page. resources> <style X: Key = "btn_style" targettype = "button"> <setter property = "margin" value = "5, 8, 15, 27 "/> <setter property =" fontfamily "value =" "/> <setter property =" fontsize "value =" 24 "/> </style> <style X: key = "txb_base" targettype = "textblock"> <setter property = "fontfamily" value = ""/> <setter property = "fontsize" value = "20"/> <setter property = "margin" value = "3, 4, 3, 3 "/> <setter property =" textwrapping "value =" Wrap "/> </style> <style X: key = "txb_fd_name" targettype = "textblock" basedon = "{staticresource txb_base}"> <setter property = "fontweight" value = "bold"/> <setter property = "margin" value = "4, 4, 13, 3 "/> </style> </page. resources> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <button content = "open audio file" Click = "onopenfiles" style = "{staticresource btn_style}"/> <grid. row = "1" horizontalalignment = "center" verticalalignment = "center"> <grid. columndefinitions> <columndefinition width = "Auto"/> <columndefinition width = "*"/> </grid. columndefinitions> <image grid. column = "0" margin = "8" stretch = "Uniform" X: name = "IMG" width = "200" Height = "200" verticalalignment = "TOP"/> <grid. column = "1" margin = "5"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "Auto"/> </ grid. rowdefinitions> <grid. columndefinitions> <columndefinition width = "Auto"/> <columndefinition width = "Auto"/> </grid. columndefinitions> <textblock grid. row = "0" grid. column = "0" text = "song name:" style = "{staticresource txb_fd_name}"/> <textblock X: Name = "tbname" grid. column = "1" grid. row = "0" style = "{staticresource txb_base}"/> <textblock grid. row = "1" grid. column = "0" text = "singer:" style = "{staticresource txb_fd_name}"/> <textblock X: Name = "tbartist" grid. column = "1" grid. row = "1" style = "{staticresource txb_base}"/> <textblock grid. row = "2" grid. column = "0" text = "album:" style = "{staticresource txb_fd_name}"/> <textblock X: Name = "tbalbum" grid. column = "1" grid. row = "2" style = "{staticresource txb_base}"/> <textblock grid. row = "3" grid. column = "0" text = "year:" style = "{staticresource txb_fd_name}"/> <textblock X: Name = "tbyear" grid. column = "1" grid. row = "3" style = "{staticresource txb_base}"/> </GRID> <mediaelement X: name = "me" opacity = "0" width = "0" Height = "0" autoplay = "true"/> </GRID> </Page>

As you can see, there is a button event to be processed. We will reference mediaelement named me in the background code.

 

3. CS code. First, we need to reference the following namespace:

using Windows.Media.PlayTo;using Windows.Storage;using Windows.Storage.Pickers;using Windows.Storage.FileProperties;using Windows.Storage.Streams;using Windows.UI.Xaml.Media.Imaging;

Then declare a playtomanager and add it to the page class constructor.

Playtomanager Mgr = NULL; // playto management object public mainpage () {This. initializecomponent (); // get the management object instance Mgr = playtomanager. getforcurrentview (); // registers the sourcerequested event and sets playto source Mgr. sourcerequested + = mgr_sourcerequested ;}

Okay, the point is over. I will press Ctrl + V for the remaining code.

Async void mgr_sourcerequested (playtomanager sender, playtosourcerequestedeventargs ARGs) {var df = args. sourcerequest. getdeferral (); await dispatcher. runasync (windows. UI. core. coredispatcherpriority. normal, () => {args. sourcerequest. setsource (this. me. playtosource) ;}); df. complete ();} private async void onopenfiles (Object sender, routedeventargs e) {// open an audio file and try fileopenpicker picker = new fil Eopenpicker (); picker. filetypefilter. add (". MP3 "); picker. filetypefilter. add (". WMA "); picker. suggestedstartlocation = pickerlocationid. musiclibrary; storagefile audiofile = await picker. picksinglefileasync (); If (audiofile! = NULL) {// read audio file attributes and display musicproperties MP = await audiofile. properties. getmusicpropertiesasync (); this. tbname. TEXT = MP. title; this. tbartist. TEXT = MP. artist; this. tbalbum. TEXT = MP. album; this. tbyear. TEXT = MP. year. tostring (); // obtain the file icon var icon = await audiofile. getthumbnailasync (thumbnailmode. musicview, 200, thumbnailoptions. resizethumbnail); bitmapimage BMP = new bitmapimage (); BMP. setsource (icon); this. IMG. source = BMP; // start playing this. me. setsource (await audiofile. openasync (fileaccessmode. read), audiofile. contenttype );}}

Note: when processing the sourcerequested event, you must first obtain a playtosourcedeferral, then write the code, and finally call the complete method. Because it is asynchronous, if you do not do this, it will suddenly pass, and your Code may not be able to be executed.

 

Well, now let's prepare for the test. We need to do some work first. Otherwise, the test will not be good.

1. Open Windows Media Player and find out where it is. If you cannot find it, search in the application search bar.

2. Click the drop-down arrow of "media stream" and select "Allow Remote Control of my player ".

 

3. Click "Allow Remote Control in the network" in the pop-up window ".

 

4. Open the control panel, find devices and printers, and click Add device.

 

5. If no exception occurs, the local media player is displayed on the device to be searched. Select the Media Player and click "Next ".

 

6. Installing

 

After the operation is successful, you will see it in the multimedia device.

 

Note: Keep media player on. Do not turn it off. Otherwise, the test fails. Once you close it, the device becomes invalid.

Now, go back to vs, run the application, select an audio file, and start playing. The page displays the song information.

 

Move the cursor to the upper right or lower right corner and select device from the test sidebar ].

 

In the subsequent pane, click the multimedia device we just added.

 

At this time, if everything is normal, you will find that the audio has been played in Windows Media Player. You can control audio pause or stop in WMP.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.