WMediaPlayer::WMediaPlayer(MediaType mediaType, WContainerWidget *parent) : WCompositeWidget(parent), mediaType_(mediaType), videoWidth_(0), videoHeight_(0), gui_(this), boundSignals_(0) { for (unsigned i = 0; i < 11; ++i) control_[i] = 0; for (unsigned i = 0; i < 3; ++i) display_[i] = 0; for (unsigned i = 0; i < 2; ++i) progressBar_[i] = 0; WTemplate *impl = new WMediaPlayerImpl(this, tr("Wt.WMediaPlayer.template")); impl->bindString("gui", std::string()); setImplementation(impl); WApplication *app = WApplication::instance(); LOAD_JAVASCRIPT(app, "js/WMediaPlayer.js", "WMediaPlayer", wtjs1); std::string res = WApplication::relativeResourcesUrl() + "jPlayer/"; if (!app->environment().ajax()) app->require(res + "jquery.min.js"); if (app->require(res + "jquery.jplayer.min.js")) app->useStyleSheet(res + "skin/jplayer.blue.monday.css"); if (mediaType_ == Video) setVideoSize(480, 270); #ifndef WT_TARGET_JAVA implementJavaScript(&WMediaPlayer::play, jsPlayerRef() + ".jPlayer('play');"); implementJavaScript(&WMediaPlayer::pause, jsPlayerRef() + ".jPlayer('pause');"); implementJavaScript(&WMediaPlayer::stop, jsPlayerRef() + ".jPlayer('stop');"); #endif }
void WMediaPlayer::play() { if (isRendered()) { /* * play is being delayed so that other changes (e.g. addSource() are * reflected first, see #2819 */ doJavaScript("setTimeout(function(){" + jsPlayerRef() + ".jPlayer('play'); }, 0);"); } else playerDo("play"); }
void WMediaPlayer::playerDoRaw(const std::string& jqueryMethod) { WStringStream ss; if (isRendered()) ss << jsPlayerRef(); ss << jqueryMethod; if (isRendered()) ss << ';'; if (isRendered()) doJavaScript(ss.str()); else initialJs_ += ss.str(); }
SoundManager::SoundManager() : WMediaPlayer(MediaType::Audio) { resize(0, 0); setAttributeValue("style", "overflow: hidden"); controlsWidget()->hide(); decorationStyle().setBorder(WBorder()); WStringStream ss; ss << "function() { " """var s = " << jsRef() << ", l = s.getAttribute('loops');" """if (l && l != '0') {" "" "s.setAttribute('loops', l - 1);" "" << jsPlayerRef() << ".jPlayer('play');" """}" "}"; ended().connect(ss.str()); ended().setNotExposed(); }
void WMediaPlayer::render(WFlags<RenderFlag> flags) { // XXX subtitles, chapters, stream ? static const char *mediaNames[] = { "poster", "mp3", "m4a", "oga", "wav", "webma", "fla", "m4v", "ogv", "webmv", "flv" }; WApplication *app = WApplication::instance(); if (mediaUpdated_) { WStringStream ss; ss << '{'; bool first = true; for (unsigned i = 0; i < media_.size(); ++i) { if (media_[i].link.isNull()) continue; if (!first) ss << ','; std::string url = app->resolveRelativeUrl(media_[i].link.url()); ss << const_cast<char *>(mediaNames[media_[i].encoding]) << ": " << WWebWidget::jsStringLiteral(url); first = false; } ss << '}'; if (!(flags & RenderFull)) playerDo("setMedia", ss.str()); else { initialJs_ = ".jPlayer('setMedia', " + ss.str() + ')' + initialJs_; } mediaUpdated_ = false; } if (flags & RenderFull) { if (gui_ == this) createDefaultGui(); WStringStream ss; ss << jsPlayerRef() << ".jPlayer({" << "ready: function () {"; if (!initialJs_.empty()) ss << "$(this)" << initialJs_ << ';'; initialJs_.clear(); ss << "}," << "swfPath: \"" << WApplication::resourcesUrl() << "jPlayer\"," << "supplied: \""; bool first = true; for (unsigned i = 0; i < media_.size(); ++i) { if (media_[i].encoding != PosterImage) { if (!first) ss << ','; ss << const_cast<char *>(mediaNames[media_[i].encoding]); first = false; } } ss << "\","; if (mediaType_ == Video) { ss << "size: {" << "width: \"" << videoWidth_ << "px\"," << "height: \"" << videoHeight_ << "px\"," << "cssClass: \"jp-video-" << videoHeight_ << "p\"" << "},"; } ss << "cssSelectorAncestor: " << (gui_ ? "'#" + id() + '\'' : "''") << ", cssSelector: {"; const char *controlSelectors[] = { "videoPlay", "play", "pause", "stop", "volumeMute", "volumeUnmute", "volumeMax", "fullScreen", "restoreScreen", "repeat", "repeatOff" }; first = true; for (unsigned i = VideoPlay; i < RepeatOff; ++i) { if (control_[i]) { if (!first) ss << ", "; ss << const_cast<char *>(controlSelectors[i]) << ":\"#" << control_[i]->id() << "\""; first = false; } } const char *displaySelectors[] = { "currentTime", "duration" }; for (unsigned i = CurrentTime; i < Duration; ++i) { if (control_[i]) { if (!first) ss << ", "; ss << const_cast<char *>(displaySelectors[i]) << ":\"#" << display_[i]->id() << "\""; first = false; } } if (progressBar_[Time]) { if (!first) ss << ", "; ss << "seekBar:\"#" << progressBar_[Time]->id() << "\", " << "playBar:\"#bar" << progressBar_[Time]->id() << "\""; first = false; } if (progressBar_[Volume]) { if (!first) ss << ", "; ss << "volumeBar:\"#" << progressBar_[Volume]->id() << "\", " << "volumeBarValue:\"#bar" << progressBar_[Volume]->id() << "\""; first = false; } ss << '}' << "});"; ss << "new " WT_CLASS ".WMediaPlayer(" << app->javaScriptClass() << ',' << jsRef() << ");"; doJavaScript(ss.str()); boundSignals_ = 0; } if (boundSignals_ < signals_.size()) { WStringStream ss; ss << jsPlayerRef(); for (unsigned i = boundSignals_; i < signals_.size(); ++i) ss << ".bind('" << signals_[i]->name() << "', function(o, e) { " << signals_[i]->createCall() << "})"; ss << ';'; doJavaScript(ss.str()); boundSignals_ = signals_.size(); } WCompositeWidget::render(flags); }