-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicPlay.cs
92 lines (83 loc) · 2.82 KB
/
MusicPlay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace MediaPlayer
{
public class MusicPlay
{
public static uint SND_ASYNC = 0x0001;
public static uint SND_FILENAME = 0x00020000;
[DllImport("winmm.dll")]
public static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hWndCallback);
public void PlayNmusinc(string path)
{
mciSendString(@"close temp_alias", null, 0, 0);
mciSendString(@"open """ + path + @""" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
/// <summary>
/// 播放音乐文件(重复)
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public void PlayMusic_Repeat(string p_FileName)
{
try
{
mciSendString(@"close temp_music", " ", 0, 0);
mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
mciSendString(@"play temp_music repeat", " ", 0, 0);
}
catch
{ }
}
/// <summary>
/// 播放音乐文件
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public void PlayMusic(string p_FileName)
{
try
{
mciSendString(@"close temp_music", " ", 0, 0);
//mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
mciSendString(@"open """ + p_FileName + @""" alias temp_music", null, 0, 0);
mciSendString(@"play temp_music", " ", 0, 0);
}
catch
{ }
}
/// <summary>
/// 停止当前音乐播放
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public void StopMusic(string p_FileName)
{
try
{
mciSendString(@"close " + p_FileName, " ", 0, 0);
}
catch { }
}
public void pauseMusic()
{
mciSendString(@"pause temp_music", " ", 0, 0);
}
public void resumeMusic()
{
mciSendString(@"resume temp_music", " ", 0, 0);
}
public void seekToPosition(int pos)
{
// pos的单位是ms 在主线程中计算传参进
mciSendString(@"seek temp_music to " + pos.ToString(), null, 0, 0);
mciSendString(@"play temp_music", " ", 0, 0);
}
public void setVolume(int volume)
{
mciSendString(@"setaudio temp_music volume to " + volume.ToString(), null, 0, 0);
}
}
}