Hallo an alle und vielen Dank im Voraus.C# mp3-Datei klingt nicht
Ich versuche, eine MP3-Datei in einem Webformular zu spielen. Ich bin mit dieser Klasse, die ich im Netz gefunden habe ...
using System.Runtime.InteropServices;
using System.Text;
namespace MP3_Player
{
class MusicPlayer :System.IDisposable
{
public bool Repeat { get; set; }
public MusicPlayer(string filename)
{
const string FORMAT = @"open ""{0}"" type mpegvideo alias MediaFile";
string command = System.String.Format(FORMAT, filename);
mciSendString(command, null, 0, 0);
}
[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 MediaFile";
mciSendString(command, null, 0, 0);
}
public void play()
{
string command = "play MediaFile";
if(Repeat) command += " REPEAT";
mciSendString(command, null, 0, 0);
}
public void stop()
{
string command = "stop MediaFile";
mciSendString(command, null, 0, 0);
Dispose();
}
public void Dispose()
{
string command = "close MediaFile";
mciSendString(command, null, 0, 0);
}
}
}
... und dann versuche ich, von meinem Web-Formular mit diesem Stück Code zu spielen ...
private MusicPlayer player;
...
private void Detalles_Click(object sender, EventArgs e)
{
...
Thread thread = new Thread(Musica);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void Musica()
{
if(player != null)
{
player.stop();
}
player = new MusicPlayer("~/Mantenimiento/MP3/ejemplo.mp3");
player.play();
}
... aber es funktioniert nicht. Kann mir bitte jemand sagen, was ich falsch mache, was fehlt oder was?
Übrigens, gibt es einen einfacheren Weg, einen Sound zu spielen? Ich bin es gewohnt, es in Android zu tun, und es ist nur etwa fünf oder sechs Zeilen Code.
Vielen Dank für Ihre Zeit und Hilfe.