If the oxidation number of the oxygen in BaO2 were -2, the oxidation number of the barium would have to be+4. But elements in Group IIA can't form +4 ions. This compound must be barium peroxide, [Ba2+][O22-]. Barium therefore is +2 and oxygen is -1.
The oxidation number of oxygen is -1 because the compound is a peroxide.
But this is not in the case of SiO2
1 O oxidation number is = -2
2 O oxidation number is = -4
so oxidation number of O is = -4
Oxidation number of si is = +4
because oxidation number on any compound should be zero if oxidation number of oxygen is -4 then oxidation number of si must be +4
Hope you understand the difference between the two compounds