2013-07-14 13 views
9

mio c forma # Windows è consentire di svolgere un mp3 file.I ha fatto questo utilizzando questo codicecome rilevare quando un file mp3 ha finito di giocare

WMPLib.WindowsMediaPlayer wplayer; 
    wplayer = new WMPLib.WindowsMediaPlayer(); 
    wplayer.URL = "c:/Standup.mp3"; 
    wplayer.controls.play(); 

questo funziona perfettamente, ma io voglio sapere quando il file ha finito di giocare in modo da poterlo riavviare.

Pls come faccio?

risposta

5

Se non è necessario sapere quando il file è stato completato per altri scopi rispetto al ciclo, è possibile considerare il metodo setMode per attivare il looping della traccia.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd564867(v=vs.85).aspx

+0

Ho davvero non capiscono che link..Pls possono dare u un esempio di codice. –

+3

Non ho usato questa libreria, ma dalla mia comprensione, dovresti essere in grado di usare: wplayer.settings.setMode ("loop", true); o wplayer.settings.setMode ("autoRewind", true); –

0

È possibile utilizzare il lettore multimediale bulitinto di PlayStateChange (int NewState) per rilevare lo stato di arresto.

16

È possibile farlo utilizzando la PlayStateChanged event. puoi aggiungerlo al tuo MediaPlayer in questo modo.

WMPLib.WindowsMediaPlayer wplayer; 
wplayer = new WMPLib.WindowsMediaPlayer(); 
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange); 
wplayer.URL = "c:/Standup.mp3"; 
wplayer.controls.play(); 

si può quindi verificare la MediaEndedPlayState nel EventHandler e ripristinare la CurrentPosition per l'inizio della canzone:

void wplayer_PlayStateChange(int NewState) 
{ 
    if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded) 
    { 
     wplayer.controls.currentPosition = 0; 
    } 
} 

Edit: mi aspettavo di essere in grado di fare una canzone ripetibile al punto ero stufo di esso, e il codice di cui sopra ha funzionato quando ho avuto i punti di interruzione impostati. Una volta rimossi, ho scoperto che c'erano altri PlayStation che impedivano la riproduzione del file. Sono stato in grado di bypassarlo utilizzando un timer one shot .. Ora ho am stanco della canzone che stavo usando. Potrebbe/probabilmente essere un modo migliore per farlo, ma funzionerà.

Codice Modificato

public partial class Form1 : Form 
{ 
    WMPLib.WindowsMediaPlayer wplayer; 
    Timer tmr = new Timer(); 
    public Form1() 
    { 
     InitializeComponent(); 
     tmr.Interval = 10; 
     tmr.Stop(); 
     tmr.Tick += new EventHandler(tmr_Tick); 
     wplayer = new WMPLib.WindowsMediaPlayer(); 
     wplayer.URL = "c:/Standup.mp3"; 
     wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange); 
     wplayer.controls.play(); 
    } 

    void tmr_Tick(object sender, EventArgs e) 
    { 
     tmr.Stop(); 
     wplayer.controls.play(); 
    } 

    void wplayer_PlayStateChange(int NewState) 
    { 
     if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded) 
     { 
      tmr.Start(); 

     } 
    } 


} 
+0

Grazie ha funzionato anche –

2

È possibile controllare costantemente con un Thread tuttavia, v'è poca documentazione ...

//player .playState 
    //Possible Values 
    // 
    //This property is a read-only Number (long). The C-style enumeration constant can be derived by prefixing 
    //the state value with "wmpps". For example, the constant for the Playing state is wmppsPlaying. 
    //Value State Description 
    //0  Undefined  Windows Media Player is in an undefined state. 
    //1  Stopped   Playback of the current media item is stopped. 
    //2  Paused   Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location. 
    //3  Playing   The current media item is playing. 
    //4  ScanForward  The current media item is fast forwarding. 
    //5  ScanReverse  The current media item is fast rewinding. 
    //6  Buffering  The current media item is getting additional data from the server. 
    //7  Waiting   Connection is established, but the server is not sending data. Waiting for session to begin. 
    //8  MediaEnded  Media item has completed playback. 
    //9  Transitioning Preparing new media item. 
    //10 Ready   Ready to begin playing. 
    //11 Reconnecting Reconnecting to stream. 
Problemi correlati