GithubHelp home page GithubHelp logo

elving / soundcloud-audio.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from voronianski/soundcloud-audio.js

1.0 2.0 0.0 135 KB

Soundcloud tracks and playslists in modern browsers with HTML5 Audio API

Home Page: http://caniuse.com/#feat=audio

JavaScript 82.99% HTML 17.01%

soundcloud-audio.js's Introduction

 soundcloud-audio.js

build status

Wrapper around HTML5 <audio> and SoundCloud tracks and playlists APIs. It could be treated as small replacement for official SoundCloud SDK.

Install

npm install soundcloud-audio --save

or

bower install soundcloud-audio --save

Usage

var SoundCloudAudio = require('soundcloud-audio');

// create new instance of audio
var scPlayer = new SoundCloudAudio('YOUR_CLIENT_ID');

// if you have an api stream url you can just play it like that
scPlayer.play({streamUrl: 'https://api.soundcloud.com/tracks/185533328/stream'});

// OR in other cases you need to load TRACK and resolve it's data
scPlayer.resolve('https://soundcloud.com/djangodjango/first-light', function (err, track) {
    // do smth with track object
    // e.g. display data in a view etc.
    console.log(track); 

    // once track is loaded it can be played
    scPlayer.play();

    // stop playing track and keep silence
    scPlayer.pause();
});

// OR to load PLAYLIST and resolve it's data
scPlayer.resolve('http://soundcloud.com/jxnblk/sets/yello', function (err, playlist) {
    // do smth with array of `playlist.tracks` or playlist's metadata
    // e.g. display playlist info in a view etc.
    console.log(playlist);

    // once playlist is loaded it can be played
    scPlayer.play();

    // for playlists it's possible to switch to another track in queue
    // e.g. we do it here when playing track is finished 
    scPlayer.on('ended', function () {
        scPlayer.next();
    });

    // play specific track from playlist by it's index
    scPlayer.play({playlistIndex: 2});
});

API

new SoundCloudAudio('YOUR_CLIENT_ID')

Create an instance of SoundCloudAudio, internally uses HTML5 <audio> element which is available under audio property.

Client ID string is required, so get it here - https://developers.soundcloud.com.

Methods

resolve('url', callback)

If you don't have SoundCloud stream_url (e.g. https://api.soundcloud.com/tracks/:id/stream) or you need track's metadata then this method is for you. Pass original track's or playlist's url as a first argument. Once data will be resolved without errors callback function will receive it as plain object as the only argument.

play([options])

Starts playing track if it's not playing right now. Accepts options object where all fields are completely optional:

  • streamUrl - SoundCloud API stream_url string, if it's passed it will be the main source from where to play audio.
  • playlistIndex - number that specifies the position of the track to play in resolved playlist tracks array.

preload(streamUrl)

Preloads track data without playing it.

pause()

Pause playing audio.

stop()

Stop playing audio and rewind it to start.

next()

Skip to the next track in playlist to play.

previous()

Return to the previous track in playlist.

seek(event)

Helper method for integrating with HTML <progress> element and its' polyfills. Changes playback position, just pass the DOM event as the only argument and all necessary computations will be done automagically.

Props

audio

Instance of raw <audio> element. There are several useful properties like currentTime (in seconds) or events you may want to listen with addEventListener (the full list of of them at HTMLMediaElement).

duration

SoundCloud track duration converted into seconds in order to be in sync with audio.currentTime.

playing

Shows the current state of the player, returns false or source of a currently streaming track.

Events

SoundCloudAudio provides shortcuts to subscribe or unsubscribe handler functions on native audio events. The list of supported events can be accessed here - https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events.

on('event', handler)

off('event', handler)

unbindAll()

var SoundCloudAudio = require('soundcloud-audio');

var scPlayer = new SoundCloudAudio('YOUR_CLIENT_ID');

scPlayer.play({streamUrl: 'https://api.soundcloud.com/tracks/185533328/stream'});
scPlayer.on('timeupdate', function (audio) {
    console.log(audio.currentTime);
});
scPlayer.on('ended', function (audio) {
    console.log(scPlayer.track.title + ' just ended!');
});

Browser Support

Chrome Firefox IE Opera Safari
3+ ✔ 3.5+ ✔ 9+ ✔ 10+ ✔ 3.1+ ✔

Next Goals

  • Add more methods of SoundCloud API (like getTrackById etc.)
  • Provide streaming options and mimic events in order to easily integrate with Waveform.js

License

MIT Licensed

Copyright (c) 2015, Dmitri Voronianski [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

soundcloud-audio.js's People

Contributors

voronianski avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.