Hi, I have the following code:
var url: []const u8 = undefined;
var track: ?yt.TrackInfo = null;
if (res.args.search) |query| {
const tracks = try yt.search(allocator, query, 20);
defer allocator.free(tracks);
const stdout = std.io.getStdOut().writer();
for (tracks, 1..) |t, i| {
try stdout.print("[{d}] {s}\n", .{ i, t.title });
}
try stdout.print("Select a number (1-{d}): ", .{tracks.len});
// read input ...
track = tracks[id - 1];
}
if (track == null) {
if (res.positionals[0]) |pos| {
url = pos;
} else {
const stderr = std.io.getStdErr().writer();
try stderr.print("You need to specify an url\nUSAGE:\n", .{});
return clap.help(stderr, clap.Help, ¶ms, .{});
}
}
var yt_stream = Youtube.init(allocator, CHANNELS, SAMPLE_RATE);
defer yt_stream.deinit();
if (track) |t| {
try yt_stream.playFromTrack(t);
} else {
try yt_stream.playFromUrl(url);
}
var buffer: [BUFFER_SIZE * CHANNELS * @sizeOf(f32)]f32 = undefined;
while (should_run) {
const bytes_read = try yt_stream.stdout.read(std.mem.sliceAsBytes(&buffer));
if (bytes_read == 0) break;
audio.write(f32, &buffer, bytes_read / (CHANNELS * @sizeOf(f32))) catch {};
}
This code works fine with yt_stream.playFromUrl(...), but when It uses yt_stream.playFromTrack(...) it doesn't crash but there is no audio, just noice.