I'm currently developing a MP3 player using C#. I'm a beginner. I have been able to develop a normal MP3 player with minimal functionalities like open file, pause, play and stop. But the problem is it plays some songs and doesn't play some. I have imported the winmm.dll file too. But some files are played while some are not.Moreover can anyone suggest how can I add a stack of songs to it which will play randomly?The code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace MP3Player
{
class MusicPlayer
{
Boolean isPlay=false;
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
public void open(String file)
{
string command = "open \"" + file + "\" type MPEGVideo alias MyMp3";
mciSendString(command, null, 0, 0);
isPlay = false;
}
public void play()
{
if (isPlay == false)
{
string command = "play MyMP3";
mciSendString(command, null, 0, 0);
isPlay = true;
}
}
public void pause()
{
if (isPlay == true)
{
string command = "pause MyMP3";
mciSendString(command, null, 0, 0);
isPlay = false;
}
}
public void stop()
{
string command = "stop MyMp3";
mciSendString(command, null, 0, 0);
isPlay = false;
command = "close MyMp3";
mciSendString(command, null, 0, 0);
}
}
}
I dont know why your app doesnt play some mp3s, i think i can assume its because of the audio encoding, but dont quote me.
As for your "Shuffle feature"
what you can try to implement is an array that looks in a directory and and gets all the mp3 files in that directory,
then it randomly plays a song,
heres a sample code:
fileinfo MySongs() = MySongDirectoryString.getFiles();
foreach (song in MySong)
{
string SongName = song.tostring();
//code to play that song
}
I suggest using Windows Media Player SDK. Here's an example.
For your random playing.
Use a stack (makes it easier to track progress)
//Retrieve a list of files from a directory.
var di = new DirectoryInfo("Path to folder");
//Get the files and order them randomly.
var listOfFiles = di.GetFiles().OrderBy(s=> Guid.NewGuid);
//Convert the list to a stack.
var stack = new Stack<FileInfo>(listOfFiles);
Now you can use your stack with the Pop method to get the next random song in the list.
//Usage
var current = stack.Pop();
As for the not playing issue, you might want to use LAME encoder/decoder for your mp3 files it's more robust than the windows dll.
Related
I have a Script Task in an SSIS (2008) package that downloads files from a remote FTP server to a local directory. The Script Task is written in C# 2008, and uses WinSCPnet.dll. Using examples from WinSCP's documentation, I came up with the script below. The script functions correctly to download the files, but all the file success/failure messages are held until the entire script completes, and then all the messages are dumped at once. File progress is not displayed at all using Console.Write(), and trying to use Dts.Events.FireInformation() in SessionFileTransferProgress gives me
Error: "An object reference is required for the non-static field, method, or property Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase.Dts.get"
Is there a way I can use the DTS.events.Fire* events to display file progress information as it happens, and file completion status after each file?
Script:
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
using System.AddIn;
using WinSCP;
namespace ST_3a1cf75114b64e778bd035dd91edb5a1.csproj
{
[AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : VSTARTScriptObjectModelBase
{
public void Main()
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = (string)Dts.Variables["User::FTPServerName"].Value,
UserName = (string)Dts.Variables["User::UserName"].Value,
Password = (string)Dts.Variables["User::Password"].Value
};
try
{
using (Session session = new Session())
{
// Will continuously report progress of transfer
session.FileTransferProgress += SessionFileTransferProgress;
session.ExecutablePath = (string)Dts.Variables["User::PathToWinSCP"].Value;
// Connect
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult = session.GetFiles(
(string)Dts.Variables["User::ExportPath"].Value
, (string)Dts.Variables["User::ImportPath"].Value
, false
, transferOptions
);
// Throw on any error
transferResult.Check();
// Print results
bool fireAgain = false;
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Dts.Events.FireInformation(0, null,
string.Format("Download of {0} succeeded", transfer.FileName),
null, 0, ref fireAgain);
}
}
Dts.TaskResult = (int)DTSExecResult.Success;
}
catch (Exception e)
{
Dts.Events.FireError(0, null,
string.Format("Error downloading file: {0}", e),
null, 0);
Dts.TaskResult = (int)DTSExecResult.Failure;
}
}
private static void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
{
//bool fireAgain = false;
// Print transfer progress
Console.Write("\r{0} ({1:P0})", e.FileName, e.FileProgress);
/*Dts.Events.FireInformation(0, null,
string.Format("\r{0} ({1:P0})", e.FileName, e.FileProgress),
null, 0, ref fireAgain);*/
// Remember a name of the last file reported
_lastFileName = e.FileName;
}
private static string _lastFileName;
}
}
The answer I figured out was pretty simple. I changed SessionFileTransferProgress from private static void to private void. Once this method was no longer static, I was able to use this to invoke the Dts.Events.Fire* methods. SessionFileTransferProgress was changed to:
private void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
{
bool fireAgain = false;
this.Dts.Events.FireInformation(0, null,
string.Format("\r{0} ({1:P0})", e.FileName, e.FileProgress),
null, 0, ref fireAgain);
// Remember a name of the last file reported
_lastFileName = e.FileName;
}
My Windows desktop application made in Unity can read and play audio.wav but can't open it.
I want to double click a .WAV file and play it.
I have done myPlayWav(string path) function.
And assigned to .wav file extension to call and run my application when is double click with the mouse. But:
How I get the audio file path when my application is open and run?
Do I need to include something? because there is no Unity function.
using UnityEngine;
public class AtStart : MonoBehaviour
{
void Start ()
{
string path;
System.Run( path ?) ...
StartCoroutine( myPlayWav ( path) );
}
//...
Is better to use Awake()?
Pease edit my English.
Use System.Environment.CommandLine.
This returns the raw (unformatted) string of the commandline. Then use string.Split(' ') to divide it into its individual args. At index 0 there should be your application. At index 1 the file you want to open and then at further indexes other arguments.
using System;
void Start ()
{
string[] args = Environment.CommandLine.Split(' ');
string path = args[1];
StartCoroutine(myPlayWav(path));
}
Note that if the application is NOT started by opening a file, args[1] will be empty.
To prevent an IndexOutOfRange exception you could do this:
using System;
void Start ()
{
string[] args = Environment.CommandLine.Split(' ');
if (args.Length < 1)
{
Debug.Log("There is no file that is trying to be opened!");
return;
}
string path = args[1];
StartCoroutine(myPlayWav(path));
}
I need an help about the performing of the mp3 player files. I search on internet and i find how to use the winmm.dll libreary only if I need to take an mp3 file from an external directory. I need to modify it for an internal directory of my app.
Let's show the code:
I create the Mp3Player class like that:
class Mp3Player : IDisposable
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
public void open(string file)
{
const string FORMAT = #"open ""{0}"" type MPEGVideo alias MyMp3";
string command = String.Format(FORMAT, file);
mciSendString(command, null, 0, 0);
}
public void play()
{
string command = "play MyMp3";
mciSendString(command, null, 0, 0);
}
public void stop()
{
string command = "stop MyMp3";
mciSendString(command, null, 0, 0);
}
public void Dispose()
{
string command = "close MyMp3";
mciSendString(command, null, 0, 0);
}
public double Duration()
{
string command = "status MyMp3 length";
double bho = mciSendString(command, null, 0, 0);
return bho;
}
And call for open the internal file here:
var soundfile = "Assets/audio/myaudio.mp3";
private Mp3Player _mp3player = new Mp3Player();
_mp3player.open(soundfile);
_mp3player.play();
This give me an error on the opening of the of the file, so i'm sure that the problem is in the directory path that i send to the class. I tryed some version but no one works and on internet can't find nothing helping me. Some one of you can say me the correct path for making work that function?
Thank you very much to all.
Excuse me for my really bad english.
According to this forum on Windows Mobile, all waveform audio function are implemented in 'coredll.dll'. Use this DLL instead of 'winmm.dll'.
I'm using a package called Visualizer Studio (http://www.alteredr.com/visualizer-studio/) to make a game where objects react to music. My goal is to have the user be able to load a song at any time during the game. It's going to be standalone/PC, not WebPlayer.
My problem is that Visualizer Studio takes the audio data from an Audio Source in the scene. So when I use NAudio to load and stream MP3s, visualizer studio doesn't hear them, and my objects don't react to the music.
I'm using IWavePlayer right now in my code. I've tried adding audio.clip = www.GetAudioClip and similar functions to have the audio clip load the music that is being played, but to no avail.
I got the code I'm using to select and stream .mp3s from this blog post (the source code is at the bottom): (http://denis-potapenko.blogspot.com/2013/04/task-6-loading-mp3-audio-via-www-class.html). It's unchanged at the moment, because nothing I was trying was working.
To be clear, the .mp3s DO play when I select them from a file browser. I just need them to play via an audio source in my scene. Please, there has to be a way to do this. I've contacted visualizer studio support and asked on the unity forums, but nobody can help so far.
Please keep in mind that I'm not exactly a great programmer, so you might have to dumb answers down for me. Thanks for you patience in advance. Anyway,
Here's my code I'm using to open a file browser and stream audio:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using NAudio;
using NAudio.Wave;
public class AppRoot : MonoBehaviour
{
///////////////////////////////////////////////////////////////////////////
#region Variables
private static AppRoot mInstance = null;
private const string cLocalPath = "file://localhost/";
private IWavePlayer mWaveOutDevice;
private WaveStream mMainOutputStream;
private WaveChannel32 mVolumeStream;
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Interface
public AppRoot()
{
mInstance = this;
}
public void Start()
{
}
public void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
UnloadAudio();
LoadAudio();
}
}
public void OnGUI()
{
if (GUI.Button(new Rect(100, Screen.height - 200 + 100, Screen.width - 200, 35), "Load audio"))
{
UnloadAudio();
LoadAudio();
}
}
public void OnApplicationQuit()
{
UnloadAudio();
}
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Implementation
private void LoadAudio()
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Title = "Open audio file";
ofd.Filter = "MP3 audio (*.mp3) | *.mp3";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
WWW www = new WWW(cLocalPath + ofd.FileName);
Debug.Log("path = " + cLocalPath + ofd.FileName);
while (!www.isDone) { };
if (!string.IsNullOrEmpty(www.error))
{
System.Windows.Forms.MessageBox.Show("Error! Cannot open file: " + ofd.FileName + "; " + www.error);
return;
}
byte[] imageData = www.bytes;
if (!LoadAudioFromData(imageData))
{
System.Windows.Forms.MessageBox.Show("Cannot open mp3 file!");
return;
}
mWaveOutDevice.Play();
Resources.UnloadUnusedAssets();
}
}
private bool LoadAudioFromData(byte[] data)
{
try
{
MemoryStream tmpStr = new MemoryStream(data);
mMainOutputStream = new Mp3FileReader(tmpStr);
mVolumeStream = new WaveChannel32(mMainOutputStream);
mWaveOutDevice = new WaveOut();
mWaveOutDevice.Init(mVolumeStream);
return true;
}
catch (System.Exception ex)
{
Debug.LogWarning("Error! " + ex.Message);
}
return false;
}
private void UnloadAudio()
{
if (mWaveOutDevice != null)
{
mWaveOutDevice.Stop();
}
if (mMainOutputStream != null)
{
// this one really closes the file and ACM conversion
mVolumeStream.Close();
mVolumeStream = null;
// this one does the metering stream
mMainOutputStream.Close();
mMainOutputStream = null;
}
if (mWaveOutDevice != null)
{
mWaveOutDevice.Dispose();
mWaveOutDevice = null;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#region Properties
private static AppRoot Instance
{
get
{
return mInstance;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
}
Sincerely,
Jonathan
http://answers.unity3d.com/answers/1128204/view.html
With the "latest" version of NAudio.dll and NAudio.WindowsMediaFormat.dll inserted into your Resources folder utilize this code to do what you described:
var musicInput : GameObject;
private var aud : AudioFileReader;
private var craftClip : AudioClip;
private var AudioData : float[];
private var readBuffer : float[];
private var soundSystem : AudioSource;
private var musicPath : String[];
//Check if there's a pref set for the music path. Use it AND add all the files from it
function CheckMusic()
{
var pathname = musicInput.GetComponent.<InputField>();
if(PlayerPrefs.HasKey("musicpath") == false)
{
PlayerPrefs.SetString("musicpath", "Enter Music Directory");
}
else
{
pathname.text = PlayerPrefs.GetString("musicpath");
musicPath = Directory.GetFiles(PlayerPrefs.GetString("musicpath"),"*.mp3");
}
}
function LoadSong(songToPlay : int)
{
//Download the song via WWW
var currentSong : WWW = new WWW(musicPath[songToPlay]);
//Wait for the song to download
if(currentSong.error == null)
{
//Set the title of the song
playingSong.text = Path.GetFileNameWithoutExtension(musicPath[songToPlay]);
//Parse the file with NAudio
aud = new AudioFileReader(musicPath[songToPlay]);
//Create an empty float to fill with song data
AudioData = new float[aud.Length];
//Read the file and fill the float
aud.Read(AudioData, 0, aud.Length);
//Create a clip file the size needed to collect the sound data
craftClip = AudioClip.Create(Path.GetFileNameWithoutExtension(musicPath[songToPlay]),aud.Length,aud.WaveFormat.Channels,aud.WaveFormat.SampleRate, false);
//Fill the file with the sound data
craftClip.SetData(AudioData,0);
//Set the file as the current active sound clip
soundSystem.clip = craftClip;
}
}
And I quote
The "songToPlay" variable that is passed to the function is a simple int that is acquired from the array created under the CheckMusic function. I search a chosen directory entered from a GUI Inputfield for a specific file type (MP3) which can be changed to WAV or OGG and then input those files to an array. Other code chooses the number of the song on the array to play and you can change that to anything you like. The important part is that the NAudio,dll does all the heavy lifting. All you need to do is use the aud.Read(float[] to send data to, song starting point(usually 0),length of song data (aud.length). The Float[] here is the same length as aud.length so create the float of the same length, read the file, fill the float, create the clip, then dump the float data in with AudioClip.SetData()
Right now this code works and it does the job. Downside is it takes
2-3 seconds to fill the float in this way and is a noticeable drag. It
also tends to chew up memory pretty fast, but it gets the job done.
Hope it helps as a starting point for those who are looking to do
this. I know I needed it.
When you use a WaveOut instance you are playing (relatively) directly on the soundcard, outside of the Unity environment. You need a way to introduce that data into Unity.
I haven't worked with Unity so this might not be the best answer,but...
You can introduce wave data into the scene using OnAudioFilterRead. You can use this to create procedural sound, and with a little coding it can presumably be hooked up to a NAudio wave source. This will introduce the audio data directly into the game for your in-game code to deal with.
Here's an article I found that might help: Procedural Audio with Unity
mWaveOutDevice = new WaveOut();
Maybe causing the issue, it is better practice to use WaveOutEvent() which supports running in all non GUI threads.
mWaveOutDevice = new WaveOutEvent();
Related StackOverFlow Question
I know how to get the list of all of the installed printers on a machine with .Net:
foreach (String printer in PrinterSettings.InstalledPrinters)
{
Console.WriteLine(printer.ToString());
}
Console.ReadLine();
InstalledPrinters is just a list of strings though. Is there any way to get the installed printer objects that contain both the name and the icon image that I would ordinarily see under "Devices and Printers" in the Windows Explorer?
The icon is normally embedded into either one of the dll files or the main EXE, look at the System.Drawing.Icon static methods, the link below is for WinForms, its slightly different with WPF as you have to create an ImageSource from the extracted icon stream.
How to: Extract the Icon Associated with a File in Windows Forms
C# code for this task:
public static class PrinterIcons
{
public static Dictionary<string, Icon> GetPrintersWithIcons(IntPtr hwndOwner)
{
Dictionary<string, Icon> result = new Dictionary<string, Icon>();
Shell32.IShellFolder iDesktopFolder = Shell32.GetDesktopFolder();
try
{
IntPtr pidlPrintersFolder;
if (Shell32.SHGetFolderLocation(hwndOwner, (int)Shell32.CSIDL.CSIDL_PRINTERS, IntPtr.Zero, 0, out pidlPrintersFolder) == 0)
try
{
StringBuilder strDisplay = new StringBuilder(260);
Guid guidIShellFolder = Shell32.IID_IShellFolder;
IntPtr ptrPrintersShellFolder;
iDesktopFolder.BindToObject(pidlPrintersFolder, IntPtr.Zero, ref guidIShellFolder, out ptrPrintersShellFolder);
Object objPrintersShellFolder = Marshal.GetTypedObjectForIUnknown(ptrPrintersShellFolder, Shell32.ShellFolderType);
try
{
Shell32.IShellFolder printersShellFolder = (Shell32.IShellFolder)objPrintersShellFolder;
IntPtr ptrObjectsList;
printersShellFolder.EnumObjects(hwndOwner, Shell32.ESHCONTF.SHCONTF_NONFOLDERS, out ptrObjectsList);
Object objEnumIDList = Marshal.GetTypedObjectForIUnknown(ptrObjectsList, Shell32.EnumIDListType);
try
{
Shell32.IEnumIDList iEnumIDList = (Shell32.IEnumIDList)objEnumIDList;
IntPtr[] rgelt = new IntPtr[1];
IntPtr pidlPrinter;
int pceltFetched;
Shell32.STRRET ptrString;
while (iEnumIDList.Next(1, rgelt, out pceltFetched) == 0 && pceltFetched == 1)
{
printersShellFolder.GetDisplayNameOf(rgelt[0],
Shell32.ESHGDN.SHGDN_NORMAL, out ptrString);
if (Shell32.StrRetToBuf(ref ptrString, rgelt[0], strDisplay,
(uint)strDisplay.Capacity) == 0)
{
pidlPrinter = Shell32.ILCombine(pidlPrintersFolder, rgelt[0]);
string printerDisplayNameInPrintersFolder = strDisplay.ToString();
Shell32.SHFILEINFO shinfo = new Shell32.SHFILEINFO();
Shell32.SHGetFileInfo(pidlPrinter, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), Shell32.SHGFI.PIDL | Shell32.SHGFI.AddOverlays | Shell32.SHGFI.Icon);
Icon printerIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
Shell32.DestroyIcon(shinfo.hIcon);
result.Add(printerDisplayNameInPrintersFolder, printerIcon);
}
}
}
finally
{
Marshal.ReleaseComObject(objEnumIDList);
}
}
finally
{
Marshal.ReleaseComObject(objPrintersShellFolder);
}
}
finally
{
Shell32.ILFree(pidlPrintersFolder);
}
}
finally
{
Marshal.ReleaseComObject(iDesktopFolder);
}
return result;
}
}
Beware, that printer names in result dictionary will be printer names shown in Printers shell folder, and they can be different from printer names, used in PrinterSettings class (for example, network printers in Printers shell folder can be shown as " on ", and word "on" depends from windows localization and can be not machine network name). I don`t know yet how to get "real" printer name from IShellFolder to use it with standart PrinterSettings class.
Anyway, this code loads printers system icons, so you can use it for you task.
Upd: Shell32 class code, used in this code can be found here (too big for answer): http://pastebin.com/thJuWx45