"WP8.1 development" uses mobile phones to control multimedia playback of computers

Source: Internet
Author: User

In order to use the computer to watch the movie convenient control, I suddenly think, do a mobile app to remotely adjust the volume on the computer through the wireless network. After the success of the attempt, I thought, just the volume seems monotonous, the play/pause, the last one, the next, and other multimedia control function Plus, so fun.

Here is a brief introduction to the principle of the whole solution of the source code I will share to everyone, for reference.

First say the server, because the control command is relatively simple, I directly with a WPF application to complete, so convenient to run, with the socket to communicate more trouble, I use WCF to do the service, using Webservicehoset, let WP mobile phone client in Http-post way to invoke.

This believes that everyone will, there is a core, is how to control the multimedia function of the system? In fact, you should find that on your laptop keyboard, there is a row of function buttons, you can press these keys to adjust the volume, control playback, the last song, etc., as well as a variety of function switches, such as turning on/off the wireless function.

That is, as long as the code can simulate the issue of these keys to achieve control, it is necessary to use the Win32 API in the SendInput function. In the initial attempt, I put the SendInput function into the managed code, but the call did not respond, I do not know whether it is not I import wrong.

Later, I simply use C + + to write a DLL, each control operation with a separate export function to wrap, and then write their own DLL in the exported function in the managed project DllImport.

The header file of the DLL you wrote is as follows:

#pragmaOnce#include"stdafx.h"/*Increase Volume*/extern "C"__declspec (dllexport)voidvolume_up ();/*Decrease Volume*/extern "C"__declspec (dllexport)voidVolume_down ();/*Mute/Resume*/extern "C"__declspec (dllexport)voidVolume_mute ();/*Next Song*/extern "C"__declspec (dllexport)voidMedia_next_track ();/*Last Song*/extern "C"__declspec (dllexport)voidMedia_prev_track ();/*Play/Pause*/extern "C"__declspec (dllexport)voidmedia_play_pause ();/*Stop*/extern "C"__declspec (dllexport)voidmedia_stop ();voidSend_input_core (WORD vkey);

The function __declspec (dllexport) is an export function, which can be used in other code, and the last Send_input_core function is only for internal use within the DLL and is no longer exported.

Note To add extern "C", let the function is exported in the C language specification, because the C language does not support function overloading, at compile time the compiler does not change the name of the function, so add the extern "C" to let managed code in the DLL import can directly use the original names of the function, This way you do not need to write a complex module definition file to rename the symbol, this method of writing DLLs is the most convenient.

The following Send_input_core functions are implemented in the CPP file:

void Send_input_core (WORD vkey) {    input input;     = Input_keyboard;     = NULL;     = vkey;    SendInput (1sizeof(INPUT));}

Use the Vkey parameter to receive the key to simulate, so that you do not have to repeat the SendInput call code, the rest of the function can call the function directly, and then pass the key value can be.

void volume_up () {    send_input_core (vk_volume_up);} void Volume_down () {    send_input_core (vk_volume_down);} void Volume_mute () {    send_input_core (vk_volume_mute);} void Media_next_track () {    send_input_core (vk_media_next_track);} void Media_prev_track () {    send_input_core (vk_media_prev_track);} void Media_play_pause () {    send_input_core (vk_media_play_pause);} void Media_stop () {    send_input_core (vk_media_stop);}

When the DLL is finished, it is imported into C # managed code:

namespacedllapis{usingSystem; usingSystem.Runtime.InteropServices;  Public Sealed classMediacontrolapis {//the name of the DLL file        Const stringDll_name ="Mediactrllib.dll"; #regionAPIs imported from a DLL[DllImport (dll_name)]extern Static voidvolume_up (); [DllImport (Dll_name)]extern Static voidVolume_down (); [DllImport (Dll_name)]extern Static voidVolume_mute (); [DllImport (Dll_name)]extern Static voidMedia_next_track (); [DllImport (Dll_name)]extern Static voidMedia_prev_track (); [DllImport (Dll_name)]extern Static voidMedia_play_pause (); [DllImport (Dll_name)]extern Static voidMedia_stop (); #endregion        #regionPublic methods/// <summary>        ///Increase Volume/// </summary>         Public Static voidVolumeup () {volume_up (); }        /// <summary>        ///Decrease Volume/// </summary>         Public Static voidVolumedown () {Volume_down (); }        /// <summary>        ///Mute/Resume/// </summary>         Public Static voidVolumemute () {volume_mute (); }        /// <summary>        ///Next Track/// </summary>         Public Static voidMedianexttrack () {media_next_track (); }        /// <summary>        ///Previous Track/// </summary>         Public Static voidMediaprevtrack () {media_prev_track (); }        /// <summary>        ///Play/Pause/// </summary>         Public Static voidMediaplaypause () {media_play_pause (); }        /// <summary>        ///Stop playing/// </summary>         Public Static voidMediastop () {media_stop (); }        #endregion    }}

I think it's much easier to import this than to import the Win32 API directly.

Next, complete the WCF service.

[ServiceContract] Public InterfaceIService {[OperationContract] [WebInvoke (UriTemplate="Invoke?action={act}")]        voidInvoke (stringAct); }     Public classWcfservice:iservice { Public voidInvoke (stringAct) {            Switch(ACT) { Case "Vu"://Increase VolumeDllAPIs.MediaControlAPIs.VolumeUp ();  Break;  Case "VD"://Decrease VolumeDllAPIs.MediaControlAPIs.VolumeDown ();  Break;  Case "VMS"://Mute/ResumeDllAPIs.MediaControlAPIs.VolumeMute ();  Break;  Case "mn"://Next SongDllAPIs.MediaControlAPIs.MediaNextTrack ();  Break;  Case "MP"://Last SongDllAPIs.MediaControlAPIs.MediaPrevTrack ();  Break;  Case "MPP"://Play/PauseDllAPIs.MediaControlAPIs.MediaPlayPause ();  Break;  Case "Ms"://StopDllAPIs.MediaControlAPIs.MediaStop ();  Break; }        }    }


Other code is not introduced, we look at the source bar.

Finally, the realization of the mobile phone client, is actually using Http-post to the WCF service just to send data.

        Private AsyncTask Postactionasync (stringaction) {            stringPosturi =string. Format ("http://{0}:{1}/invoke?action={2}", HostName, Port, action); HttpWebRequest Request=(HttpWebRequest) webrequest.create (Posturi); Request. Method="POST"; varRep =awaitrequest.        Getresponseasync (); }

Finally look at the results:

Source code Download: Http://files.cnblogs.com/tcjiaan/RemoteMediaControlSlsn.zip

"WP8.1 development" uses mobile phones to control multimedia playback of computers

Related Article

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.