Getting your Trinity Audio player ready...

A Far South Side man charged with shooting at a Chicago police officer Monday protested that the cop shot at him first and yelled “I should’ve smoked his a–” during a tumultuous bail hearing Tuesday.

“Write it down,” Judge Adam Bourgeois Jr. told a court reporter after the shouted confession from Kentrell Pledger, 29, a reputed Black Discipline gang member.

Despite Pledger’s defiance in court Tuesday, the father of two allegedly said “thank you for not shooting me” to the officers who found him hiding under porch stairs on the Far South Side, according to the arrest report.

Prosecutors said photos of Pledger, of the 400 block of West 103rd Street, posing with the .40-caliber handgun used in the shooting were posted earlier the day of the shooting on social media.

Pledger grew agitated after Bourgeois ordered him held without bail for shooting at the officer. “He shot at me first,” Pledger said. “Chicago police officer shot at me first.”

“Get your story straight dog…I should’ve smoked his a–,” Pledger said.

Prosecutors said that at around 2:15 p.m. Monday officers on patrol in an unmarked car saw Pledger walking in the 300 block of West 105th Place while fidgeting with the front of his belt.

An officer in the car asked Pledger a few questions, prosecutors said, and Pledger ran off, pulling a handgun from his pants as he ran.

Pledger ran up a driveway into an alley pursued on foot by the officer who had asked him questions, prosecutors said. He then turned and fired twice at the pursing cop.

That officer then returned fire, prosecutors said. No one was hit.

Responding officers found Pledger hiding under some porch stairs in the 200 block of West 106th Place, according to an arrest report.

Pledger, who was then shirtless, crawled out with his hands up and told the officers the gun was under the stairs wrapped in his red T-shirt, according to the arrest report. Seven small baggies of heroin were found in his wallet, the report says.

Police found two fired shell casings near the spot where Pledger fired on the officer. He later confessed in a videotaped interview with detectives to opening fire at the cop, knowing he was a Chicago police officer.

“You ain’t black, you’re white b—-,” Pledger shouted at Bourgeois, who is African-American, as he was led from the courtroom.

“What they fail to understand is the law is above us all,” Bourgeois replied.

The prior defendant, Rico Walker, had called him a “motherf——racist” and yelled “f— you b—- a–” after the judge ordered him held on $5 million bail on an attempted sexual assault charge.

The judge then ordered Walker held without bail.