Windows Phone Dev: Play internal mp3 files with winmm.dll - c#

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'.

Related

Webbrowser Navigate To PDF Files Does not Show Anything

I am trying to Impersonate and show the Pdf files from another computer in my network. The problem is, webbrowser goes and finds the pdf files but returns as a gray window. I guess it means that it can see the pdf but cannot load or something like that. Because when I change the path it says "The page cannot be reached". I searched about it for a couple days but couldn't find solution. I think there is no problem on impersonation because I can copy or delete or recreate the files with my program but cannot see :D Another interesting thing is when I navigate to a image file, no problem occurs. It works perfectly. Here is my code:
public class Impersonate : IDisposable
{
private static string m_UserName = "myUserName";
private static string m_Password = "mypassword";
private static string m_Domain = "myDomain";
private IntPtr token = IntPtr.Zero;
WindowsImpersonateContext person;
public void Dispose()
{
Undo();
}
public WindowsImpersonateContext Person()
{
bool success = LogonUser(m_userName, m_Domain, m_Password, 9, 0,
ref token)
if(success)
{
person = new WindowsIdentity(token).Impersonate();
return person;
}
}
public void Undo()
{
person.Undo();
Closehandle(token);
}
}
using(Impersonate imp = new Impersonate())
{
imp.Person();
string fpath = "Path_to_the_pdf_file";
newWebBrs.Navigate(new Uri(fpath));
newWebBrs.Show();
newWebbrs.Refresh();
}
The 'using' part is under a button. Any help or idea will be appreciated :D

Developing a MP3 Player using C#

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.

Playing mp3 file with winmm.dll in C# works well in GUI app, but does not work in cosnsole app

So, I am trying to play mp3 file in C# console application.
class Program
{
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command,
StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
static void Main(string[] args)
{
string filePath = "1.mp3";
string command = "open \"" + filePath + "\" type mpegvideo alias MyTag";
Int32 err = mciSendString(command, null, 0, IntPtr.Zero);
command = "play MyTag";
err = mciSendString(command, null, 0, IntPtr.Zero);
Console.ReadLine();
}
}
But I'm getting error 266 (MCIERR_CANNOT_LOAD_DRIVER) when try to open file. Intersting thing is that the same code works well if I put it into button callback of GUI app (Windows Forms Application). Also it works if I change code ommitting file open.
string filePath = "1.mp3";
string command = "play " + filePath;
Int32 err = mciSendString(command, null, 0, IntPtr.Zero);
It also works well but I'd like to separate open action.
I'm using Microsoft Visual Studio 2008, .NET 3.5 (tested also on .NET 2.0 with the same results), Windows 7 32-bit.
Any ideas why is it so?

C# MciSendString Recording, Works in debug, not deployed

I really hope someone can point me in the right direction with this. This code works perfectly when I'm debugging within Visual Studio Express 2010, but give me the mci error 263 - "The specified device is not open nor recognised by mci" when built and deployed.
I've tried tinkering with build settings and what not, but no joy. Surely this must be a config issue rather than the code?
Any help anyone can provide would be greatly appreciated.
----- Code Follows ----
[DllImport("winmm.dll")]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
[System.Runtime.InteropServices.DllImport("winmm.dll")]
private static extern bool mciGetErrorString(int fdwError, StringBuilder lpszErrorText, int cchErrorText);
///Inside a button function
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
//Inside another button function
int i = mciSendString(#"save recsound C:/test22/sound", outs, 0, 0);
MessageBox.Show(""+i);
StringBuilder buffer = new StringBuilder(128);
bool returnValue = mciGetErrorString(i, buffer, buffer.Capacity);
string err = buffer.ToString();
MessageBox.Show(buffer.ToString());
Well I got your code to work by actually specifying a file to save it as...
string outs = "";
//Inside another button function
int i = mciSendString(#"save recsound C:/test.wav", ref outs, 0, 0);
I also changed the out parameter to a ref though I doubt that had anything to do with it.
What type of file are you trying to play? And where is it deployed? I recently came upon the same issue; I cannot play .mp3s or .wmas on one of my test machines (I get the same error: 263). I installed Windows Media Player and they worked, so this leads me to believe that a codec is missing. Now I just have to figure out what codec(s) it was.

Can I get the icon associated with an installed printer through .Net?

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

Categories