Domanda

Sto scrivendo un'applicazione Windows Forms che dovrebbe riprodurre tre file audio e alla fine di ogni file audio, è per cambiare la fonte di un'immagine.

Posso farlo riprodurre i suoni usando System.Media.SoundPlayer . Tuttavia, sembra riprodurre il suono in un thread diverso, continuando su.

L'effetto netto di questo è che viene riprodotto solo l'ultimo suono e tutte le immagini vengono cambiate.

Ho provato Thread.Sleep , ma dorme tutta la GUI e dopo il periodo di sonno tutto accade contemporaneamente e l'ultimo suono riprodotto.

Aggiorna

Pensavo che PlaySynch funzionasse, ma sembra congelare la mia GUI che non è l'ideale. Cos'altro posso fare?

È stato utile?

Soluzione

Hai provato Metodo SoundPlayer.PlaySync ? Dall'aiuto:

  

Il metodo PlaySync utilizza l'attuale   thread per riprodurre un file .wav, impedendo   il thread dalla gestione di altri   messaggi fino al completamento del caricamento.

Altri suggerimenti

Invece di utilizzare il metodo Play, utilizzare Metodo PlaySync .

Usa questo codice:

[DllImport("WinMM.dll")]
public static extern bool  PlaySound(byte[]wfname, int fuSound);

//  flag values for SoundFlags argument on PlaySound
public static int SND_SYNC        = 0x0000;      // Play synchronously (default).
public static int SND_ASYNC       = 0x0001;      // Play asynchronously.
public static int SND_NODEFAULT   = 0x0002;      // Silence (!default) if sound not found.
public static int SND_MEMORY      = 0x0004;      // PszSound points to a memory file.
public static int SND_LOOP        = 0x0008;      // Loop the sound until next sndPlaySound.
public static int SND_NOSTOP      = 0x0010;      // Don't stop any currently playing sound.
public static int SND_NOWAIT      = 0x00002000;  // Don't wait if the driver is busy.
public static int SND_ALIAS       = 0x00010000;  // Name is a registry alias.
public static int SND_ALIAS_ID    = 0x00110000;  // Alias is a predefined ID.
public static int SND_FILENAME    = 0x00020000;  // Name is file name.
public static int SND_RESOURCE    = 0x00040004;  // Name is resource name or atom.
public static int SND_PURGE       = 0x0040;      // Purge non-static events for task.
public static int SND_APPLICATION = 0x0080;      // Look for application-specific association.
private Thread t; // used for pausing
private string bname;
private int soundFlags;

//-----------------------------------------------------------------
public void Play(string wfname, int SoundFlags)
{
    byte[] bname = new Byte[256];    //Max path length
    bname = System.Text.Encoding.ASCII.GetBytes(wfname);
            this.bname = bname;
            this.soundFlags = SoundFlags;
            t = new Thread(play);
            t.Start();
}
//-----------------------------------------------------------------

private void play()
{
    PlaySound(bname, soundFlags)
}

public void StopPlay()
{
    t.Stop();
}

public void Pause()
{
    t.Suspend(); // Yeah, I know it's obsolete, but it works.
}

public void Resume()
{
    t.Resume(); // Yeah, I know it's obsolete, but it works.
}

Quello che probabilmente vuoi fare è emettere un suono asincrono, ma poi disabilitare la tua UI in modo tale che non risponda alla risposta dell'utente. Quindi, una volta riprodotto il suono, riattivi l'interfaccia utente. Ciò ti consentirebbe di dipingere ancora l'interfaccia utente normalmente.

Ho capito come farlo. Ho usato PlaySynch, ma ho eseguito la riproduzione in un thread separato rispetto al codice che disegna l'interfaccia utente. Lo stesso thread aggiorna anche l'interfaccia utente dopo la riproduzione di ogni file.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top