Hartranft Elementary’s music teacher quit in December, and the North Philadelphia school was never able to hire a permanent replacement. So instead of learning about instruments and songs, students had a series of subs and worksheets.

Like so many schools across the city and around the country hampered by an anemic teacher pipeline, some classes lacked permanent educators for all or part of the year.

But then something unusual — principal Keith Arrington would say miraculous — happened.

The Philadelphia police stepped in, with drums and guitar, bass and vocalist. And once a week, they taught Hartranft students music.

» READ MORE: Teachers wanted: A special report

It all started when Hartranft’s School Advisory Council decided to reach out to the new leadership of the 26th Police District to expand the relationship the school had with its beat officer. Arrington and Capt. Maria Ortiz-Rodriguez talked about the school’s needs, including a lack of a music teacher, and that planted a seed in Ortiz-Rodriguez’s mind.

“Obviously, music fills us up so much,” said Ortiz-Rodriguez. “I was trying to figure out a way to provide that for the children.”

For weeks, a handful of officers with musical backgrounds would drag their equipment to the second-floor music room at the school at Seventh and Cumberland, working with about 150 students. Hartranft, a K-8, enrolls about 350.

On Thursday, the students and officers held a joyous culmination of their partnership, with students singing, dancing, and rapping in the school’s all-purpose room. There were freestyle dances and spontaneous songs, students approaching officers with thanks and high-fives.

First graders bopped to “We Don’t Talk About Bruno” from the movie Encanto, while Lt. Jeff Campbell played drums and Officer Dominick Gulli played guitar. Officer Stephanie Velazquez wowed the crowd with her vocals.

Third grader Kyiana Sanders said it was “kind of boring” when there was no music teacher. But once the officers showed up, things changed.

“We learned fourth notes, eighth notes, 16th notes,” said Kyiana. “We got to play the drums.”

The police officers, Kyiana said, “are nice.”

Students were wary of the police at first, the officers said.

But eventually, “they warmed up to us, and started engaging, started participating,” Ortiz-Rodriguez said. “They said, ‘Are you coming back next week?’ It’s seeing us in a different light, just trying to build that trust and those relationships with children.”

Gulli never imagined “music teacher” would be part of his policing resumé, but he loved his time at Hartranft, he said. (And it didn’t hurt that he earned the name “Rock Star” for his cool riffs and the street clothes he walked in with on his first day at the school.)

The classes gave students an outlet, an escape, Gulli said. And it helped them see officers in a different light.

“Now more than ever, this is important,” said Gulli. “Kids are scared, not just of the police. I really hope we keep this going.”

Campbell, who spent years as a Police Athletic League officer, gravitates to working with children. And at Hartranft, it was delightful to give kids what was most likely their first exposure to live music.

“This is one of the best experiences I’ve had in a long time,” said Campbell. (Teaching and playing for the kids was paramount, he said, but he also enjoyed meshing his style — Campbell usually plays a mix of R&B, jazz, and gospel — with Gulli’s punk.)

» READ MORE: ‘I’m never going back’: Philly teachers on why there’s a surge in midyear resignations.

No one suggests that the officers’ work replaced what a certified teacher would do, or that this model is scalable for the education system. But it undoubtedly represented a high point and brought moments of joy and collaboration in a really tough year.

“We have one common goal, to bring up our young people,” Arrington said.

The staffing squeeze continues, however. Arrington lost multiple teachers this year — in addition to the music teacher resignation, there were long-term absences. He still has three vacancies for full-time jobs in the fall, but there’s one bright spot.

“I did hire a full-time music teacher,” he said.