# Playing a video with preroll and midroll ads

**URL:** <https://forum.developer.roku.com/t/playing-a-video-with-preroll-and-midroll-ads/7833>\
**Category:** Archived\
**Tags:** roku-developer-program\
**Created:** [September 8, 2016, 4:20am UTC](https://forum.developer.roku.com/t/playing-a-video-with-preroll-and-midroll-ads/7833 "2016-09-08T04:20:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blackhawk](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Blackhawk](https://forum.developer.roku.com/u/Blackhawk)\
**Post date:** [September 8, 2016, 4:20am UTC](https://forum.developer.roku.com/t/playing-a-video-with-preroll-and-midroll-ads/7833/1 "2016-09-08T04:20:45Z")

</div>

I had made a code that I wanted to make an easier way to play videos with preroll and midroll ads

```auto
​' create and display a blank roImageCanvas to prevent the
' underlying UI from flickering between videos
canvas = CreateObject("roImageCanvas")
canvas.SetLayer(0, "#000000")
canvas.Show()
' play the preroll video with trick play disabled
if ShowPreroll(preroll)
  ' only play the main content if the preroll completed without user intervention
  ShowVideoScreen(content)
end if

shouldPlayContent = true
adBreakIndex = 0
while shouldPlayContent
  videoMsg = wait(0, contentVideoScreen.GetMessagePort())
  if videoMsg.isPlaybackPosition()
    curPos = videoMsg.GetIndex()
    nextPod = scheduledPods[adBreakIndex]
    if curPos > nextPod.renderTime and not nextPod.viewed
      contentVideoScreen.Close() ' stop playback of content
      shouldPlayContent = adIface.showAds(nextPod) ' render next ad pod
      adBreakIndex = adBreakIndex + 1
      if shouldPlayContent
        ' *** Insert client app’s resume-playback code here
      end if
    end if
  end if
  ' *** Insert client app’s video event handler code here
preroll = {
    streamFormat: "mp4"
    stream: {
      url:  "[url=http://www.archive.org/download/kelloggs_variety_pak/kelloggs_variety_pak_512kb.mp4]http://www.archive.org/download/kelloggs_variety_pak/kelloggs_variety_pak_512kb.mp4[/url]"
    }
  }
content ={
    title:        "TEDTalks : David Brooks: The social animal"
    sdPosterURL:  "[url=https://images.ted.com/images/ted/78e8d94d1d2a81cd182e0626dc8e96a43c88d760_132x99.jpg]https://images.ted.com/images/ted/78e8d94d1d2a81cd182e0626dc8e96a43c88d760_132x99.jpg[/url]"
    hdPosterURL:  "[url=https://images.ted.com/images/ted/78e8d94d1d2a81cd182e0626dc8e96a43c88d760_132x99.jpg]https://images.ted.com/images/ted/78e8d94d1d2a81cd182e0626dc8e96a43c88d760_132x99.jpg[/url]"
    description:  "Tapping into the findings of his latest book, NYTimes columnist David Brooks unpacks new insights into human nature from the cognitive sciences -- insights with massive implications for economics and politics as well as our own self-knowledge. In a talk full of humor, he shows how you can't hope to understand humans as separate individuals making choices based on their conscious awareness."
    contentType:  "episode"
    streamFormat: "mp4"
    stream: {
      url:  "[url=http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4]http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4[/url]"
    }
  }
end while

' close the blank canvas and return the user to the previous UI screen
canvas.Close()

```

should I set up the code like this?
