I'm currently testing Elm out, and I decided to make a little video playlist... The goal is to load a new video as soon as the previous one ended... In Javascript I would have used the "ended" event the video tag sends to load the next one.. Since it does not exist on Elm, I have tried to create it with no success... the first video ends, and the update is not called (guessing I haven't binded correctly the event listener...)
Does anyone know how to do this ? This is my code so far...
module Video exposing (main)
import Html exposing (Html, div, text, video, source, a)
import Html.Attributes as Att exposing (preload, autoplay, src, type)
import Html.App
import Json.Decode as Json
import Html.Events exposing (onClick, on)
import Array exposing (..)
main : Program Never
main =
Html.App.beginnerProgram
{ model = model
, update = update
, view = view
}
-- Eventlistener
onEnded msg =
Html.Events.on "ended" (Json.succeed msg)
-- MODEL
videos : Array String
videos =
Array.fromList ["/videos/vid_home_1.mp4", "/videos/vid_home_2.mp4", "/videos/vid_home_3.mp4"]
type alias Model =
{ current: Int}
model : Model
model =
{ current = 0 }
-- UPDATE
type Msg
= Noop
| OnEnded
update : Msg -> Model -> Model
update msg model =
case msg of
Noop ->
model
OnEnded ->
{ model | current = ((model.current + 1) % 3) }
-- VIEW
view : Model -> Html Msg
view model =
case Array.get model.current videos of
Nothing ->
div [][text "Error"]
Just url ->
div []
[
div []
[
video [ preload "auto", autoplay True, onEnded OnEnded ]
[ source [src url, type' "video/mp4" ][] ]
]
]