title: How to find the current song duration in Flutter with audio_service and just_audio
body: <p>The when you set the <code>MediaItem</code> in <a href="https://pub.dev/packages/audio_service" rel="nofollow noreferrer">audio_service</a> you don't know the song duration yet because <a href="https://pub.dev/packages/just_audio" rel="nofollow noreferrer">just_audio</a> hasn't had a change to tell you at this point.</p>
  <p>The <a href="https://github.com/ryanheise/audio_service/wiki/FAQ#how-do-i-update-an-existing-mediaitem" rel="nofollow noreferrer">FAQs</a> say to update the <code>MediaItem</code> like this:</p>
  <pre class="lang-dart prettyprint-override"><code>modifiedMediaItem = mediaItem.copyWith(duration: duration);
  AudioServiceBackground.setMediaItem(modifiedMediaItem);
  </code></pre>
  <p>But it is unclear to me how or where to do this. The <a href="https://github.com/ryanheise/audio_service/tree/master/example" rel="nofollow noreferrer">example app in the GitHub repo</a> sidesteps this problem by providing precomputed times. (<a href="https://github.com/ryanheise/audio_service/issues/543" rel="nofollow noreferrer">GitHub issue</a>)</p>
  <p>How and where do I transfer the duration from just_audio to audio_service so that it can update the listeners?</p>
  <p><em>I found something that works so I'm adding an answer below.</em></p>