Giter Site home page Giter Site logo

elm-audio's Introduction

Build Status

elm-audio

Basic interface for HTML5 Audio object.

Example

import Audio exposing (defaultPlaybackOptions)
import Dict
import Task


loadAndPlaySound : String -> Task.Task String ()
loadAndPlaySound soundUrl =
  Audio.loadSound soundUrl
  `Task.andThen`
  (Task.mapError (\_ -> "") << Audio.playSound Audio.defaultPlaybackOptions)


playSoundLoop : Audio.Sound -> Task.Task () ()
playSoundLoop =
  Audio.playSound { defaultPlaybackOptions | loop = True }

Managing multiple sounds

If an application uses several different sounds, they can be loaded together into a dictionary:

import Audio
import Dict
import Task


type alias SoundsDict comparable =
  Dict.Dict comparable Audio.Sound


loadSoundsIntoDictionary : List ( comparable, String ) -> Task.Task String (SoundsDict comparable)
loadSoundsIntoDictionary namesAndUris =
  let
    nameAndUriToTask ( name, uri ) =
      Task.map ((,) name) (Audio.loadSound uri)
  in
    Task.map Dict.fromList <| Task.sequence <| List.map nameAndUriToTask namesAndUris


loadAllSounds : Task.Task String (SoundsDict String)
loadAllSounds =
  loadSoundsIntoDictionary
    [ ( "Kaboom", "/assets/sounds/kaboom.ogg" )
    , ( "Crash", "/assets/sounds/crash.ogg" )
    ]


playSound : SoundsDict String -> String -> Task.Task String ()
playSound soundDictionary soundId =
  case Dict.get soundId soundDictionary of
    Just sound ->
      Task.mapError (\_ -> "") <| Audio.playSound Audio.defaultPlaybackOptions sound

    Nothing ->
      Task.fail <| soundId ++ " not loaded!"

elm-audio's People

Contributors

xarvh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

elm-audio's Issues

Publish package?

Nice work here, thanks!

It looks like there is a 1.0.0 release, but the package doesn't seem to be on http://package.elm-lang.org/?

I was wondering if you have plans to publish it so that it's easier for folks to play with it and contribute?

Thanks again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.