pub fn read_blocks<R: Read>(r: R) -> BlockIterator<R> ⓘExpand description
Returns iterator of blocks from the given reader
The reader should be positioned at the start of the FLAC file.
Because this may perform many small reads,
using a buffered reader may greatly improve performance
when reading from a raw File.
§Example
use flac_codec::{
metadata::{read_blocks, Application, Block},
encode::{FlacSampleWriter, Options},
};
use std::io::{Cursor, Seek};
let mut flac = Cursor::new(vec![]); // a FLAC file in memory
// add some APPLICATION blocks at encode-time
let application_1 = Application {id: 0x1234, data: vec![1, 2, 3, 4]};
let application_2 = Application {id: 0x5678, data: vec![5, 6, 7, 8]};
let options = Options::default()
.application(application_1.clone())
.application(application_2.clone())
.no_padding()
.no_seektable();
let mut writer = FlacSampleWriter::new(
&mut flac, // our wrapped writer
options, // our encoding options
44100, // sample rate
16, // bits-per-sample
1, // channel count
Some(1), // total samples
).unwrap();
// write a simple FLAC file
writer.write(std::slice::from_ref(&0)).unwrap();
writer.finalize().unwrap();
flac.rewind().unwrap();
// read blocks from encoded file
let blocks = read_blocks(flac)
.skip(1) // skip STREAMINFO block
.collect::<Result<Vec<Block>, _>>()
.unwrap();
// ensure they match our APPLICATION blocks
assert_eq!(blocks, vec![application_1.into(), application_2.into()]);