St. Paul, Minnesota (CNN) Jeronimo Yanez, a former Minnesota police officer on trial for fatally shooting Philando Castile during a traffic stop in July 2016, testified Friday he fired his weapon because Castile had his hand on a gun, not his wallet and identification papers.

"I had no other choice. I didn't want to shoot Mr. Castile. That wasn't my intention," Yanez said while wiping tears from his eyes, CNN affiliate WCCO reported . "I thought I was going to die."

An audio recording captured Castile telling Yanez that he had a gun in the car, and Yanez telling Castile not to reach for it.

"It's your testimony today that you saw Mr. Castile pull out an object?" prosecutor Rick Dusterhoft said Friday.

"It was a gun," Yanez said.

"You said he pulled it out?"

"Correct," Yanez said.

"And he said he wasn't (pulling it out)?"

"Correct, but it doesn't always mean that's what he was doing," Yanez said.

The shooting became known across the nation because the aftermath was streamed live on Facebook by Castile's girlfriend. The video went viral, sparking protests and renewing criticism of the use of deadly force by police, especially against African-American men.

'Officers are not perfect'

Police firearms and training expert Emanuel Kapelsohn on the stand for the defense.

Earlier Friday, Emanuel Kapelsohn, a police training and firearms expert called as a defense witness, testified he thought Yanez acted appropriately while thinking Castile was reaching for a gun.

When prosecutors questioned him about the commands Yanez yelled at Castile, Kapelsohn said, "Officers are not perfect."

"Perfection is not a goal to achieve. It may be a goal to strive for," he said.

Yanez was working for the St. Anthony police department when he pulled over Castile on July 6 because he looked similar to a robbery suspect. Castile's girlfriend, Diamond Reynolds, was in the passenger seat and her young daughter was in the back.

Philando Castile, 32, was a school cafeteria worker.

Reynolds testified that Castile was reaching for his ID in his back pocket when he was shot. He had a permit to carry a firearm in his wallet.

The Facebook video that Reynolds shot captured the dying Castile insisting that he hadn't been reaching for his handgun.

But Yanez's lawyers say Castile ignored the officer's commands and reached for his gun when he was instructed not to do so.

Prosecutors say Yanez acted negligently in using deadly force and had not given clear instructions.

'Tunnel vision'

While Yanez remained firm that he saw a gun, he acknowledged he was under tremendous stress during the incident.

Yanez said he had "tunnel vision" after Castile said he had a gun.

JUST WATCHED Woman streams graphic video of boyfriend shot by police Replay More Videos ... MUST WATCH Woman streams graphic video of boyfriend shot by police 02:38

"What goes through your mind when someone tells you they have a firearm?" Dusterhoft asked.

"It changes the dynamic of the traffic stop," Yanez responded.

Prosecutors tried to show that Yanez didn't follow all the proper procedures and that his comments to investigators differed from day to day.

Dusterhoft ended his cross-examination by recounting the final moments of Yanez holding his gun over Castile before the first responding officers from another department arrived on scene soon after the incident.

"When Roseville police officers arrived you never warned them there was a gun did you?" Dusterhoft said.

"No," Yanez said.

Testimony ends

Yanez has not spoken publicly about the shooting before Friday, though his comments to investigators have been reported in the media.

Ramsey County Attorney John Choi announced the charges against Yanez in November, saying that "no reasonable officer knowing, seeing and hearing what Officer Yanez did at the time would have used deadly force under these circumstances."

Choi has said Castile's loaded pistol was found in his shorts pocket.

Yanez is charged with second-degree manslaughter and two felony counts of dangerous discharge of a firearm.

Testimony ended Friday; closing arguments are scheduled for Monday morning.

The incident was one of several similar shootings last summer: On July 5, Alton Sterling was shot and killed outside a convenience store in Baton Rouge, Louisiana, by police responding to a report of a man with a gun. A bystander filmed that deadly encounter.