C # Development notes for small software (SaveClassic) -- (3) Basic (MP3 playing MP3Player)

Source: Internet
Author: User

 

Why does this class exist? There are many advantages of music. I will not repeat them here. One thing I can only say is that when you are reading music, sometimes you get twice the result with half the effort, improving your work efficiency and enjoying your mood. So write this class. This is only an auxiliary function. The code for this class is as follows.

1 using System. Runtime. InteropServices;

2 using System;

3 namespace Common

4 {

5 public class MP3Player

6 {

7 /// <summary>

8 // file address

9 /// </summary>

10 public string FilePath;

11 /// <summary>

12 // play music

13 /// </summary>

14 public void Play ()

15 {

16 string tmepstr = "";

17 tmepstr = tmepstr. PadLeft (128, Convert. ToChar (""));

18 mciSendString ("close all", "", 0, 0 );

19 mciSendString ("open" + FilePath + "alias media", tmepstr, tmepstr. Length, 0); mciSendString ("play media", "", 0, 0 );

20}

21

22 /// <summary>

23 // pause

24 /// </summary>

25 public void Pause ()

26 {

27 mciSendString ("pause media", "", 0, 0 );

28}

29

30 /// <summary>

31 // stop

32 /// </summary>

33 public void Stop ()

34 {

35 mciSendString ("close media", "", 0, 0 );

36}

37 // <summary>

38 // continue playing

39 /// </summary>

40 public void Resume ()

41 {

42 string TemStr = "";

43 TemStr = TemStr. PadLeft (128, Convert. ToChar (""));

44 mciSendString ("resume media", TemStr, TemStr. Length, 0 );

45}

46

47 // obtain the music Length

48 public int GetMusicLength

49 {

50 get

51 {

52 string durLength = "";

53 durLength = durLength. PadLeft (128, Convert. ToChar (""));

54 mciSendString ("status media length", durLength, durLength. Length, 0 );

55 durLength = durLength. Trim ();

56 if (durLength = "" | durLength = "\ 0") return 0;

57 return (int) (Convert. ToDouble (durLength ));

58}

59}

60 /// <summary>

61 // API function www.2cto.com

62 // </summary>

63 [DllImport ("winmm. dll", EntryPoint = "mciSendString", CharSet = CharSet. Auto)]

64 private static extern int mciSendString (

65 string lpstrCommand,

66 string lpstrReturnString,

67 int uReturnLength,

68 int hwndCallback

69 );

70}

71}

 

 

This class mainly uses WINMM. dll to play MP3. The method is also very simple. I will not describe it in detail. I will only describe one point, which is also a tough problem I encountered during development. It means that some MP3 files in the same folder can be played, while others cannot be played. At first, I thought it was a problem with the MP3 Chinese path, but I tested it many times and changed all the files to English. the problem persists. The search engine keeps searching for answers, and the problem persists. I once wanted to abandon winmm. dll and use other methods to implement MP3. However, with the help of a friend, I found the cause of the problem and solved the problem. It turns out that the cause of this problem is mainly the file path, but not the Chinese language, but winmm. there is no problem in playing MP3 short paths in a dll in a window. If it is a long path, you must convert the long path into a short path in DOS to play normally. The code for this method is as follows.

 

[DllImport ("kernel32.dll", CharSet = CharSet. Auto)]

Public static extern int getmediapathname ([externalas (UnmanagedType. LPTStr)] string path, [externalas (UnmanagedType. LPTStr)] StringBuilder implements path, int implements pathlength );

StringBuilder shortMusicPath = new StringBuilder (80 );

Int result = getmediapathname (mymp3list [0], shortMusicPath, shortMusicPath. Capacity );

String s = shortMusicPath. ToString ();

 

 

 

Description of the getmediapathname parameter: 1. MP3 path; 2. Returned short path; 3. Maximum length of text in memory.

Author zhaoyang

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.