William Shatner is recovering from an agonizing medical condition that almost drove him to suicide.

The Star Trek legend has beaten tinnitus — a constant ringing in the ears that won’t go away due to the permanent destruction of the nerve cells of the inner ear. In an exclusive interview with The National Enquirer, Shatner, 74, spoke about the maddening condition, which he believes was triggered by standing too close to a loud explosive device on the movie set.

“It’s like listening to the hiss of a TV that’s not tuned to a channel. I thought I’d go deaf or nuts,” the Boston Legal star said of the problem that affects over 35 million Americans, including Star Trek costar Leonard Nimoy.

“I thought it would ruin my career. I thought of killing myself.” Shatner — who suffered from the condition for 15 years — finally underwent a process called tinnitus retraining therapy (TRT). “(The doctor) fitted me with a device like a hearing aid, continually piping white noise into both ears,” said Shatner. “Once your brain gets used to the noise, you’re not listening to it any more. Then you can wean yourself off it.

“It normally takes two years to be able to stop using the device, but I was able to stop after two months. Now, the condition doesn’t affect me.”

Shatner, who’s now a spokesman for the American Tinnitus Association, offered some advice to others suffering from the condition: “Find the right expert to break the cycle of fear that it won’t go away.”