A former longtime Maine police officer has been sentenced to four years in prison for gun-related charges and providing a teenager with opioid pills.