public void StartPlayback(string filePath) { if (IsRecording) return; if (IsInPlayback) return; if (!File.Exists(filePath)) { JNet.Error($"File {filePath} does not exist."); return; } var bytes = File.ReadAllBytes(filePath); int index = 0; while (true) { // Read chunk float time = BitConverter.ToSingle(bytes, index); index += 4; int bits = BitConverter.ToInt32(bytes, index); index += 4; // Calculate number of bytes from bits. int byteCount = (int)Math.Ceiling((double)bits / 8); // Copy bytes. byte[] data = new byte[byteCount]; Array.Copy(bytes, index, data, 0, byteCount); index += byteCount; // Make data chunk. var chunk = new DataChunk(); chunk.BitLength = bits; chunk.Time = time; chunk.Data = data; PendingData.Enqueue(chunk); if (index >= bytes.Length) break; } Debug.Log($"Loaded {PendingData.Count} data chunks."); IsInPlayback = true; Time = 0f; JNet.Log("Started playback."); }
public void StartRecording(string filePath, bool deleteExisting = false) { if (IsRecording) return; if (IsInPlayback) return; if (File.Exists(filePath)) { if (deleteExisting) { File.Delete(filePath); } else { JNet.Error($"File {filePath} already exists."); return; } } try { Writer = new BinaryWriter(new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None)); } catch(Exception e) { JNet.Error(e.Message); if(Writer != null) { Writer.Dispose(); } return; } Time = 0f; IsRecording = true; }