add Programmyday form github
This commit is contained in:
80
AudioPlayerMusterlösung/AudioPlayer.cs
Normal file
80
AudioPlayerMusterlösung/AudioPlayer.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AudioPlayerMusterlösung
|
||||
{
|
||||
internal class AudioPlayer
|
||||
{
|
||||
private AudioFile[] audiofiles;
|
||||
|
||||
public AudioPlayer(int max)
|
||||
{
|
||||
audiofiles = new AudioFile[max];
|
||||
}
|
||||
public void AddFile(AudioFile audiofile)
|
||||
{
|
||||
for (int i = 0; i < audiofiles.Length; i++)
|
||||
{
|
||||
if (audiofiles[i] is null)
|
||||
{
|
||||
audiofiles[i] = audiofile;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void RemoveFile(int id)
|
||||
{
|
||||
if (id >= 0 && id < audiofiles.Length)
|
||||
{
|
||||
audiofiles[id] = null;
|
||||
}
|
||||
}
|
||||
public void Play(int id)
|
||||
{
|
||||
if (id >= 0 && id < audiofiles.Length && audiofiles[id] is null)
|
||||
{
|
||||
audiofiles[id].Play();
|
||||
}
|
||||
}
|
||||
public void PlayAll()
|
||||
{
|
||||
foreach (AudioFile audio in audiofiles)
|
||||
{
|
||||
if (audio is not null)
|
||||
{
|
||||
audio.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void PlayShuffle()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
AudioFile[] playFiles = new AudioFile[audiofiles.Length];
|
||||
for (int i = 0; i < playFiles.Length; i++)
|
||||
{
|
||||
playFiles[i] = audiofiles[i];
|
||||
}
|
||||
while (playFiles.Length > 0)
|
||||
{
|
||||
int playFile = rnd.Next(playFiles.Length);
|
||||
if (playFiles[playFile] is not null)
|
||||
{
|
||||
playFiles[playFile].Play();
|
||||
}
|
||||
AudioFile[] newArray = new AudioFile[playFiles.Length - 1];
|
||||
for (int i = 0; i < playFile; i++)
|
||||
{
|
||||
newArray[i] = playFiles[i];
|
||||
}
|
||||
for (int i = playFile; i < newArray.Length; i++)
|
||||
{
|
||||
newArray[i] = playFiles[i + 1];
|
||||
}
|
||||
playFiles = newArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user