分享

C#调用winmm.dll来放音乐的示例

 ylw527 2010-12-02

C#调用winmm.dll来放音乐的示例

 

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Music
{
    
/// <summary>
    
/// 
    
/// </summary>

    public class Audio
    
{
        [DllImport(
"winmm.dll")]
        
private static extern int mciSendString 
            (
                
string lpstrCommand,
                
string lpstrReturnString,
                
int uReturnLength,
                
int hwndCallback
            );

        [DllImport(
"kernel32.dll", CharSet = CharSet.Auto)]
        
public static extern int GetShortPathName
            (
                [MarshalAs(UnmanagedType.LPTStr)]    
string path,
                 [MarshalAs(UnmanagedType.LPTStr)]    StringBuilder shortPath,
                 
int shortPathLength
            ); 

        
public Audio()
        
{    

        }


        
public void Play(string FileName)
        
{
            StringBuilder shortPathTemp 
= new StringBuilder(255);
            
int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
            
string ShortPath = shortPathTemp.ToString();

            mciSendString(
"open "+ShortPath+" alias song","",0,0);
            mciSendString(
"play song","",0,0);
        }


        
public void Stop()
        
{
            mciSendString(
"stop song","",0,0);
        }


        
public void Pause()
        
{
            mciSendString(
"pause song","",0,0);
        }


        
public void Close()
        
{
            mciSendString(
"close song","",0,0);
        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多