Changed way to handle media ending. Previous version didnt work with Safari