Multiple variables, so you need to do some process of elimination to narrow it down until you know which of the 3 pieces is causing the problem. If you want to assume AppleTV is fine, change the wiring for a test. It reads like you are wired AppleTV to Samsung to Sonos, so switch to AppleTV (through Extractor or Receiver) Optical to Sonos Ray and HDMI to Samsung. Then go a few weeks to see if the problem happens again. If:
- not, the TV is likely at fault and you need to look through settings to see if you can adjust anything that might make the difference for that optical OUT.
- so, the soundbar is likely at fault and you need to look through settings to see if you can adjust anything that may make the difference for that optical IN.
If you try that for the few weeks and neither applies, it could be AppleTV. If you perhaps have an older one (or maybe a friend will swap theirs with you for a few weeks to rule out/in this variable?), slug it in for a few weeks and see if the problem can be repeated. If not, it is likely the AppleTV.
Else, if the TV is a smart TV, drop the AppleTV for a few weeks and use the TVs version of YouTube app. With AppleTV disconnected, see if you can make the problem happen again only between TV and soundbar. if so, you know the problem is in those 2 links. If not, that would be more evidence against AppleTV.
Another test: next time it does it, hard reboot (unplug, wait maybe a few minutes, plug back in) only ONE piece of the 3 pieces and see if the problem repeats. For example, hard reboot the soundbar since it is the far end of the audio chain. If all is well again for a while, wait & see if the problem occurs again. If so, try the same remedy. If hard reboot solves the problem a few times in a row, the bar likely has the issue.
Next try the same with only the middle link- the TV. Again, if it resolves, wait for it to happen again and do the same through a cycle of maybe 3 times in a row.
Lastly, try the first link in the chain with rebooting only AppleTV. If it resolves, wait for it to happen again and do the same. Again, maybe repeat this through 3 cycles.
This process can help you narrow in on which of the 3 is likely causing the issue too. Once you know which, you can dig into menus and instructions for that link in search of perhaps a setting change or similar to resolve the issue.
My wild guess is the TV optical out is causing this: changes in audio format in videos in YouTube and Computers are reaching the optical out and not getting forwarded to the soundbar... like one video is mono or stereo and the next is 5.1 Dolby or vice versa.
So one more test: the next time this happens, try to repeat the events that preceded the problem. For example, after you get it working again, go back to the SAME video that worked correctly before the event and then try to play the SAME video that wouldn't play after it. If the same 2 videos cause the problem again, perhaps repeat this procedure one more time, so you know this combination of videos will trigger the problem. Then dig into the details of the 2 videos- particularly their audio track details- and see if something changes.
For example, was video 1 in stereo or mono and video 2 in 5.1 (or vice versa)? If so, find 2 other videos that have identical audio formats and play them in the same order to see if that will also cause the problem. If so, you'll learn that the change in audio format is triggering the event and this will give you something to check in TV audio settings (such as changing pass-through audio to stereo only (or vice versa), which might resolve the issue).
One more guess is that you switching from Sonos to TV speakers and back again often is "confusing" the optical out port on the TV, which leads to these events when using the Sonos bar. Generally, stick with the Sonos and just turn the volume down low enough so it is no louder than when you switch to the TV speakers, effectively retiring the TV speakers from any use at all. This + what was shared in the prior 2 paragraphs in combination might be "confusing" the optical port from time to time when audio formats in select videos change. TV speakers will be stereo and when they are in charge, certain setting may change in support of stereo playback. Sonos Ray may accommodate pass through 5.1 signals but those changes to stereo may not revert back when you switch back to Ray. If so, stereo may pass through just fine but then it chokes on 5.1 audio.