A complete music player developed by WPF (I)-creation of the music playing class

Source: Internet
Author: User

 

Recently, When I was busy, I thought of using my knowledge to write a music player. I am not good at the interface in advance, so the interface is made some times, but not in this series.Article.

First, let's talk about the technologies used to develop this music player: Data Binding, XML, mediaplayer, and data templates. They will be explained later.

To elaborate on the overall idea of player development: to build a music playing class for playing music, use two controls as the playback list and playback control respectively, and use the control template to change their interface, the XML Data Reading Class xmllistsreader is used to read the XML in the storage list, read information such as the song name, file path, and duration singer to the product class, and set itemsouse of ListBox to this class, use a data template to display data.

Now, let's start the first part of the tutorial-building the music playing class.

There are several ways to play music using WPF: mediaplayer, soundplayer, and DirectX sound. To select a method with many features and ease of use, it must belong to the mediaplayer class. The only restriction is that it depends on Windows Media Player (WMP ). However, in a Windows environment, this restriction is negligible. It is all built-in to the system, isn't it?

Of course, we can directly prevent mediaplayer operations in the window.CodeBut we encapsulate it into the musicplayer class for better normalization and maintainability.

At the beginning of the class, we first define several private variables and public enumeration (indicating the player status ):

       Public   Enum Playstate: Int  {Stoped = 0  , Playing = 1  , Paused = 2  }  Private Mediaplayer player = Null  ;  Private Uri musicfile;  Private  Playstate state;  Public  Uri musicfile {  Set  {Musicfile = Value ;}  Get  {  Return  Musicfile ;}} 

Next, write the constructor, which has a parameter (music file path) and no parameter:

PublicMusicplay () {player=NewMediaplayer ();}PublicMusicplay (URI file) {load (File );}

The constructor uploads the input file path to the load method for processing. The following is the code of the load method:

 
Public VoidLoad (URI file) {player=NewMediaplayer (); musicfile=File; player. Open (musicfile );}

The load method sets the musicfile (Public variable, indicating the file path), and loads the music file using the mediaplayer open method.

The following is the code for playing, pausing, and stopping:

Public VoidPlay () {player. Play (); State=Playstate. Playing ;}Public VoidPause () {player. Pause (); State=Playstate. paused ;}Public VoidStop () {player. Stop (); State=Playstate. stoped ;}

The first sentence of the above three methods is to set playing, pause, and stop, and the second sentence is to set the current status of the player.

Then, you can obtain the natural duration of the music file:

PublicTimespan getmusicdurationtime (){While(!Player. naturalduration. hastimespan ){If(Player. naturalduration. hastimespan ){ReturnPlayer. naturalduration. timespan ;}}Return NewTimespan ();}

The naturalduration. hastimespan of mediaplayer is used to check whether the music can be read for a natural duration. The while loop is used to avoid reading null values or failing to be read.

Here is how to set and get the current progress:

Public VoidSetposition (timespan TP) {player. Position=TP ;}PublicTimespan getposition (){ReturnPlayer. position ;}

Set the volume and read volume. If the input parameter is 0, it is set to mute:

Public VoidSetvolume (DoubleVolume) {player. Volume=Volume ;}Public DoubleGetvolume (DoubleVolume ){ReturnPlayer. Volume ;}

To obtain and set the status of the current player, there are only three statuses, but there may be more:

        Public  Playstate getplaystate (){  Return  State ;} Public   Void  Setplaystate (playstate state ){  If (State = Playstate. Playing ){  This  . Play ();}  Else   If (State = Playstate. paused ){  This  . Pause ();}  Else   If (State = Playstate. stoped ){  This  . Stop ();}} 

Add a method to get the music name based on the file path:

Public StringGetmusictitle (){StringTitle =Player. Source. tostring ();ReturnTitle. substring (title. lastindexof ("/") +1, Title. Length-title. lastindexof ("/")-1);}

The second code of this method is to intercept the part after the last "/" of the string.

In addition, I have also defined a dispatchertimer to update the music location. You can use the change notification to complete the same task, but I am not using it. You can try it.

The following is the complete code without any comments. It is not difficult to take a look:

Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Media;  Using  System. text;  Using  System. Threading;  Using  System. windows;  Using  System. Windows. Media; Using  System. Windows. Threading;  Namespace  Wpfapplication10 {  Public    Class  Musicplay {  Public   Enum Playstate: Int  {Stoped = 0  , Playing = 1 , Paused = 2  }  Private Mediaplayer player = Null  ;  Private  Uri musicfile;  Private  Playstate state;  Public  Uri musicfile {  Set  {Musicfile =Value ;}  Get  {  Return  Musicfile ;}}  Public Dispatchertimer dt = Null  ;  Public  Musicplay () {player = New  Mediaplayer ();}  Public Musicplay (URI file) {load (File );}  Public   Void  Load (URI file) {player = New  Mediaplayer (); musicfile = File; player. Open (musicfile );}  Public   Void  Play () {player. Play (); DT. Start (); State = Playstate. Playing ;} Public   Void  Pause () {player. Pause (); State = Playstate. paused ;}  Public   Void  Stop () {player. Stop (); State = Playstate. stoped ;}  Public   String  Getmusictitle (){  String Title =Player. Source. tostring ();  Return Title. substring (title. lastindexof ( "  /  " ) + 1 , Title. Length-title. lastindexof ( "  /  " )- 1  );  //  Return "";  }  Public Timespan getmusicduringtime (){  While (! Player. naturalduration. hastimespan ){  If  (Player. naturalduration. hastimespan ){  Return  Player. naturalduration. timespan ;}}  Return   New  Timespan ();}  Public   Void Setposition (timespan TP) {player. Position = TP ;}  Public  Timespan getposition (){  Return  Player. position ;}  Public   Void Setvolume ( Double  Volume) {player. Volume = Volume ;}  Public   Double Getvolume ( Double  Volume ){  Return  Player. Volume ;}  Public  Playstate getplaystate (){  Return  State ;}  Public   Void  Setplaystate (playstate state ){  If (State = Playstate. Playing ){ This  . Play ();}  Else   If (State = Playstate. paused ){  This  . Pause ();}  Else   If (State = Playstate. stoped ){  This  . Stop ();}}}} 

Don't rush to build the interface... In the next article, we will build an XML read class, so stay tuned and look forward to the coming of brick makers .......

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.