johndyer/mediaelement — Issue #295: Source set entirely from code
Author: dwynne
Created: 2011-11-09 17:13:42+00:00
State: open

Hey - is it possible to set the source of the video element from code and not have any source specified in the HTML mark-up? I don't know the src of my video when the page is rendered, so I basically want to do this:

Page:

```html

```

JavaScript:

```javascript
var player = new MediaElementPlayer('#myVideo', {
success: function (mediaElement, domObject) {
var sources = [
{ src: "http://mysite.com/myvideo.mp4", type: 'video/mp4' }
];

mediaElement.setSrc(sources);
mediaElement.load();
mediaElement.play();
}
});
```

If I do that however, the following lines (868 in mediaelement-and-player.js) throw an error as mediaFiles.length === 0.

```javascript
// what if there's nothing to play? just grab the first available
if (result.method === '') {
result.url = mediaFiles[0].url;
}
```

At the moment I have to set the src to something and then change it later from JS - which isn't that elegant. I'm using version 2.3.1.

Thanks in advance.