A minor under the age of 17 who is not a party to, witness in, or directly interested in a criminal prosecution or trial before a district court, may not be present at the trial. A police officer, sheriff, or other officer in charge of a court and attending upon the trial of a criminal case in the court, shall exclude a minor under age of 17 from the room in which the trial is being held. This section does not apply when the minor is permitted to attend by order of the court before which the trial is being held. A police officer, sheriff, or deputy sheriff who knowingly neglects or refuses to carry out the provisions of this section is guilty of a misdemeanor.