J'ai une classe "Player", qui est censée gérer mon lecteur de musique global. Cela fonctionne également jusqu'à présent. La classe est en bas, si vous avez des suggestions d'amélioration, n'hésitez pas à nous le donner.Commencer la chanson suivante pendant que l'on joue
Je veux commencer une deuxième chanson pendant la fin de la chanson en cours. Donc, un FadeIn dans la chanson suivante et un FadeOut de la chanson en cours, ce qui rend la chanson plus calme.
Mon approche pour l'instant est qu'une chanson est en cours d'exécution dans l'objet "waveOutDevice1" et le second attend dans le second objet. Et dès que la chanson actuelle est sur le point de se terminer, le second WavePlayer démarre. Mais je ne sais pas comment je peux y réagir, dès que la chanson actuelle est sur le point de se terminer.
Avez-vous une idée ou des suggestions? Sincères salutations
ma classe de joueur:
public class Player
{
#region Properties, Fields
private IWavePlayer waveOutDevice1;
private IWavePlayer waveOutDevice2;
private AudioFileReader fileReader1;
private AudioFileReader fileReader2;
public AudioFile CurrentSong { get; private set; }
public Playlist CurrentPlaylist { get; private set; }
public List<AudioFile> lstPastSongs { get; private set; }
public List<AudioFile> lstNextSongs { get; set; }
public PlaybackState PlaybackState { get; private set; }
public bool Muted { get; private set; }
private float OldVolume = 0.0f;
#endregion
public Player()
{
this.lstNextSongs = new List<AudioFile>();
this.lstPastSongs = new List<AudioFile>();
}
#region Methods
public void Play(int index)
{
if (this.lstNextSongs.Count > 0 && index >= 0 && index < this.lstNextSongs.Count)
{
this.ResetFileReader();
this.CurrentSong = this.lstNextSongs[index];
this.CurrentPlaylist = this.CurrentSong.Playlist;
this.lstNextSongs.RemoveAt(index);
this.fileReader1 = new AudioFileReader(this.CurrentSong.Path);
this.waveOutDevice1 = new WaveOut();
this.waveOutDevice1.PlaybackStopped += WaveOutDevice1_PlaybackStopped;
this.waveOutDevice1.Init(this.fileReader1);
this.PlaybackState = PlaybackState.Playing;
this.waveOutDevice1.Play();
}
}
private void WaveOutDevice1_PlaybackStopped(object sender, StoppedEventArgs e)
{
this.Next();
}
private void ResetFileReader()
{
if (this.fileReader1 != null)
{
this.fileReader1.Dispose();
this.fileReader1 = null;
}
if (this.fileReader2 != null)
{
this.fileReader2.Dispose();
this.fileReader2 = null;
}
if(this.waveOutDevice1 != null)
{
this.waveOutDevice1.Dispose();
this.waveOutDevice1 = null;
}
if(this.waveOutDevice2 != null)
{
this.waveOutDevice2.Dispose();
this.waveOutDevice2 = null;
}
}
public void Pause()
{
if(this.waveOutDevice1 != null)
if (this.waveOutDevice1.PlaybackState == PlaybackState.Playing)
this.waveOutDevice1.Pause();
if(this.waveOutDevice2 != null)
if (this.waveOutDevice2.PlaybackState == PlaybackState.Playing)
this.waveOutDevice2.Pause();
this.PlaybackState = PlaybackState.Paused;
}
public void Continue()
{
if (this.waveOutDevice1 != null)
if (this.waveOutDevice1.PlaybackState == PlaybackState.Paused)
this.waveOutDevice1.Play();
if (this.waveOutDevice2 != null)
if (this.waveOutDevice2.PlaybackState == PlaybackState.Paused)
this.waveOutDevice2.Play();
this.PlaybackState = PlaybackState.Playing;
}
public void Next()
{
if(this.lstNextSongs.Count > 0)
{
if (this.CurrentSong != null)
this.lstPastSongs.Add(this.CurrentSong);
if (GlobalSettings.Shuffle)
{
System.Random random = new System.Random();
int randomNumber = random.Next(0, this.lstNextSongs.Count - 1);
this.Play(randomNumber);
}
else
this.Play(0);
}
else
{
if(GlobalSettings.Replay)
if(GlobalSettings.CurrentPlaylist != null)
for (int i = 0; i < GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.NumberOfSongs; i++)
this.lstNextSongs.AddRange(GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.AllSongs);
}
}
public void Previous()
{
if(this.CurrentSong == null)
{
if(this.lstPastSongs.Count > 0)
{
this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]);
this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1);
this.Play(0);
}
}
else
{
if(this.fileReader1 != null)
this._Previous(this.waveOutDevice1, this.fileReader1);
else if(this.fileReader2 != null)
this._Previous(this.waveOutDevice2, this.fileReader2);
}
}
private void _Previous(IWavePlayer waveOutDevice, AudioFileReader fileReader)
{
if (fileReader.CurrentTime.Seconds >= 10 || this.lstPastSongs.Count == 0)
{
waveOutDevice.Pause();
fileReader.CurrentTime = new System.TimeSpan(0, 0, 0);
waveOutDevice.Play();
}
else
{
this.lstNextSongs.Insert(0, this.CurrentSong);
this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]);
this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1);
this.Play(0);
}
}
public void SetVolume(int Volume)
{
if (Volume > -1 && Volume < 101)
{
float vol = (float)Volume/100;
if (this.fileReader1 != null)
this.fileReader1.Volume = vol;
if (this.fileReader2 != null)
this.fileReader2.Volume = vol;
this.Muted = false;
}
}
public void Mute()
{
if(this.Muted)
{
if(this.fileReader1 != null)
{
this.fileReader1.Volume = this.OldVolume;
this.Muted = false;
}
else if(this.fileReader2 != null)
{
this.fileReader2.Volume = this.OldVolume;
this.Muted = false;
}
}
else
{
this.Muted = true;
if(this.fileReader1 != null)
{
this.OldVolume = this.fileReader1.Volume;
this.fileReader1.Volume = 0;
}
else if(this.fileReader2 != null)
{
this.OldVolume = this.fileReader2.Volume;
this.fileReader2.Volume = 0;
}
}
}
#endregion
}
yest ce serait une alternative. mais à mon avis, une minuterie serait une mauvaise idée pour une bonne performance. – ExclusivAtom
Il ne devrait pas être. Surtout si vous l'exécutez sur un autre thread. 'Thread.Sleep()' est TRÈS faible impact. – theGleep
Ensuite, je vais utiliser un autre minuteur jusqu'à ce que je trouve une meilleure solution. – ExclusivAtom