Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///
- /// Written by Istace Emmanuel
- ///
- /// More info : http://istacee.wordpress.com/
- /// Topic : http://istacee.wordpress.com/2011/05/22/dotnet-mci-wrapper-basic-audio-player-c/
- ///
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace AudioPlayer
- {
- public class MyBasicAudioPlayer
- {
- /// <summary>
- /// Provide you a basic audio player based on MMI
- /// </summary>
- [DllImport("winmm.dll")]
- private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
- private string _command;
- /// <summary>
- /// Return true if a file is open, false if not
- /// </summary>
- public bool isOpen { get; internal set; }
- /// <summary>
- /// Return true if a file is paused, false if not
- /// </summary>
- public bool isPause { get; internal set; }
- /// <summary>
- /// Default constructor
- /// </summary>
- public MyBasicAudioPlayer()
- {
- isOpen = false;
- isPause = false;
- }
- /// <summary>
- /// Close the audio file
- /// </summary>
- public void Close()
- {
- if (isOpen)
- {
- _command = "close MediaFile";
- mciSendString(_command, null, 0, IntPtr.Zero);
- isOpen = false;
- }
- }
- /// <summary>
- /// Open the audio file passed on argument
- /// </summary>
- /// <param name="fileName">Path of the file from the root</param>
- public void Open(string fileName)
- {
- if (!isOpen)
- {
- _command = "open \"" + fileName + "\" type mpegvideo alias MediaFile";
- mciSendString(_command, null, 0, IntPtr.Zero);
- isOpen = true;
- isPause = false;
- }
- }
- /// <summary>
- /// Play the audio file who's loaded whith Open(string filename)
- /// </summary>
- /// <param name="loop">If true, the audio will be play again and again.</param>
- public void Play(bool loop)
- {
- if (isOpen && !isPause)
- {
- _command = "play MediaFile";
- if (loop)
- _command += " REPEAT";
- mciSendString(_command, null, 0, IntPtr.Zero);
- }
- }
- /// <summary>
- /// Pause the current audio file
- /// </summary>
- public void Pause()
- {
- if (isOpen && !isPause)
- {
- _command = "pause MediaFile";
- mciSendString(_command, null, 0, IntPtr.Zero);
- isPause = true;
- }
- }
- /// <summary>
- /// Resume after a pause the current audio file
- /// </summary>
- public void Resume()
- {
- if (isOpen && isPause)
- {
- _command = "resume MediaFile";
- mciSendString(_command, null, 0, IntPtr.Zero);
- isPause = false;
- }
- }
- /// <summary>
- /// Stop the current audio file
- /// </summary>
- public void Stop()
- {
- if (isOpen)
- {
- _command = "stop MediaFile";
- mciSendString(_command, null, 0, IntPtr.Zero);
- isPause = false;
- }
- }
- /// <summary>
- /// Set the volume of the current audio file
- /// </summary>
- /// <param name="volume">Volume to set between 0 & 1000</param>
- public void SetVolume(int volume)
- {
- _command = "setaudio MediaFile volume to " + volume.ToString();
- mciSendString(_command, null, 0, IntPtr.Zero);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement