Judas Priest singer Rob Halford once took a law-breaking walk on the wild side that could have ended his career.

The gay, leather-loving singer reveals in his “totally candid” memoir — “Confess: The Autobiography,” out Tuesday — that California cops covered up for his long-ago arrest for “public indecency” at a Venice Beach restroom.

In a book excerpt from Rolling Stone, the heavy-metal legend also relates a tasty affair from the early 1990s, when a then-closeted Halford would head to Camp Pendleton in California for threeways with a bisexual Marine sergeant and his wife — the only woman Halford says he has ever had sex with.

“Once the door was shut, we would go at it for hours,” Halford, now 69, writes of their hookups in his new torrid tome. “I’d leave there more exhausted than if I’d been for a gym workout.” He adds that he was “far more interested” in the man and that his “heart wasn’t in it” as far as having sex with the wife, although she was “a lovely, beautiful woman, with a perfect body.”

But the self-proclaimed “metal god” also got his kicks in other more lurid places. Halford recalls taking a mountain-bike ride in early 1992 — not long before he first left the band — and swinging by “a notorious men’s washroom” in Venice Beach, where he “decided to stop off and try my luck.”

His “luck” quickly ran out: After fondling himself in an open restroom stall, Halford was arrested by an undercover cop.

“Oh, f–k! A million thoughts raced through my mind,” he writes of getting caught. “This is it! I’ve f–ked up! It’s going to be in the papers! I’ve lost everything! And yet, at the same time, I felt oddly calm.”

A handcuffed Halford was taken to a police station and, while there, an officer recognized him.

“I thought it was you,” the cop told him, and then asked, “What the hell are you doing here, Rob Halford?”

“I’m a f–king idiot,” Halford replied. The officer said he would see what he could do about the situation. While waiting, Halford writes, other officers “flashed devil horns” at him through his cell window — a recognition of his Judas Priest stature — and he “did the same back, and stuck my tongue out. It passed the time.”

Rob Halford (center) and Judas Priest band members during a 1979 photo shoot.
Rob Halford (center) and Judas Priest band members during a 1979 photo shoot.Redferns/Getty Images

Halford was eventually released and the first officer who recognized him said they would keep the incident from the press. “I had been lucky. Again,” Halford writes.

“How did I feel? Stupid, and ashamed, but also angry — that, this late in the century, gay men still had to live in fear like this,” continues Halford, who publicly came out on MTV in 1998 and reunited with the band in 2003.

“I always call this [1992] arrest my ‘George Michael moment,’ after he did the same thing in Beverly Hills six years later,” he says, referring to the late Wham singer’s 1998 arrest for a lewd act in a park. “The only difference was that George wasn’t so lucky with the newspapers.”

He does, however, say he thinks Judas Priest fans might have gotten past his faux pas had it come to light at the time.

“I’d like to think for the most part if that incident had broken into the press then a great proportion of my fans would have said: ‘We’ll support you and stand with you,’” he recently told NME. “A few years later, when I came out on MTV, that was a proven fact because the feedback from around the world was positive — they just wanted me to continue being the singer for Judas Priest and get on with the job.”

In the memoir, Halford also talks about the sexual abuse he faced when he was younger by a friend of his father.

“At the time, it was terribly confusing and couldn’t have happened at a worse time, for me as a young guy that was already dealing with trying to figure things out,” he told Variety in an interview last week. “Talking about it now, I can feel the horror and being totally frightened and wanting to run away, but at the same time feeling, ‘Now this is affection, in a very crude brutal manner.’ It was incredibly, incredibly complex. You can only imagine for a young man, dealing with that type of assault. “