A former police officer convicted in the accidental shooting death of an unarmed man in a darkened stairwell was spared prison time Tuesday, and a judge reduced his manslaughter conviction to a lesser charge.

Peter Liang was sentenced to five years’ probation and 800 hours of community service in the 2014 shooting of Akai Gurley, who was walking down a stairway in a public housing complex when the rookie officer fired a bullet into the dark — by accident after being startled, he said. The bullet ricocheted and killed Gurley, 28.

“Given the defendant’s background and how remorseful he is, it would not be necessary to incarcerate the defendant to have a just sentence in this case,” Brooklyn state Supreme Court Justice Danny Chun said in sentencing Liang, also 28.

A jury had convicted him in February of a manslaughter charge carrying up to 15 years in prison. But Chun on Tuesday reduced the offense to criminally negligent homicide, which carries up to four years in prison.

Liang, speaking softly, said he never meant to fire the gun and apologized to Gurley’s family.

“Growing up my parents thought it was a foolish dream that I wanted to become a police officer. When I graduated from the academy it was a dream come true,” Liang told the judge, before his apology. “Judge my life is forever changed, I hope I have a chance to improve.” Liang.

Liang was the first New York City police officer convicted in an on-duty shooting since 2005, and the verdict prompted an outpouring of demonstrations on both sides of a closely watched case. The shooting happened in a year of debate nationwide about police killings of black men. Gurley was black; Liang is Chinese-American.

As the sentencing approached, dozens of demonstrators gathered outside the courthouse — some supporting Liang, others the Gurley family.

Brooklyn prosecutors had recommended Liang serve no time, based on his record and the circumstances of the trial.

“As a result of this conviction, Peter Liang will never be a police officer again,” Brooklyn Assistant District Attorney Joe Alexis told the judge. “He’s not someone who needs to be locked up to protect society.”

Prosecutors suggested five years of probation, six months of home confinement and 500 hours of community service; the judge decided against home confinement.

Some members of Gurley’s family had said they felt betrayed by prosecutors’ recommendation and had hoped Chun would sentence Liang to prison anyway.

“On that night, because of the recklessness of that night, I’m without my partner, our daughter is without her father, a mom is without her son,” Gurley’s domestic partner, Kim Ballinger, said in court.

Gurley’s girlfriend, Melissa Butler, who was with him when he died, told Liang: “When you stole Akai’s life, you stole mine as well.”