diff --git a/config/.routes.rb.swp b/app/assets/javascripts/.music_player.js.swo similarity index 81% rename from config/.routes.rb.swp rename to app/assets/javascripts/.music_player.js.swo index a2762380866c3e4446801c7487f5c6b7ff484868..ba44fcc7887c8b7050c2e22fb655f17bf2412c79 100644 Binary files a/config/.routes.rb.swp and b/app/assets/javascripts/.music_player.js.swo differ diff --git a/app/assets/javascripts/music_player.js b/app/assets/javascripts/music_player.js index c05bf4f942cf30871a4a1d81569ef5679ea92ba7..18f3312983ff98bbfd2c608873409869075ba221 100644 --- a/app/assets/javascripts/music_player.js +++ b/app/assets/javascripts/music_player.js @@ -1,9 +1,52 @@ -function music_player() { - $.get("/musics/"+$(this).data().id, function( data ) { - $( "#music_player" ).html( data ); - }); -} +var music_info; +var player; $( ".clickable_links" ).each(function() { - $(this).on( 'click', music_player ); + if ($(this).data().player == "youtube" ) { + $(this).on( 'click', youtube_click); + } }); + +function youtube_click() { + music_info = $(this); + youtube_player(); +} + +function youtube_player() { + if (player && $("div#music_player").length == 0) { + player.loadVideoById(music_info.data().url); + } + else { + player = new YT.Player('music_player', { + height: '390', + width: '640', + videoId: music_info.data().url, + events: { + 'onReady': player_launch, + 'onStateChange': onPlayerStateChange, + 'onError': player_error + } + }); + } +} + +function next_music() { + music_info = $("li[data-id=\'"+music_info.data().id+"\']").next(); + if (music_info.data().player == "youtube" ) { + youtube_player(); + } +} + +function onPlayerStateChange(event) { + if (event.data == 0) { + next_music(); + } +} + +function player_error(event) { + next_music(); +} + +function player_launch(event) { + event.target.playVideo(); +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2c912a7b98ee876b10c4dcfef457879d3639e668..2ed556e2970fdcf33ee1adf3c25f504d4833eaad 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,5 +10,6 @@ <%= yield %> </body> - <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> +<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> +<script src="https://www.youtube.com/iframe_api"></script> </html> diff --git a/app/views/musics/.index.html.erb.swo b/app/views/musics/.index.html.erb.swo index ef74791dc89e70492f4f2e9dc0f020eb5844a54c..8e8aba757e7a34f84ca6450aee0cb795a37e0352 100644 Binary files a/app/views/musics/.index.html.erb.swo and b/app/views/musics/.index.html.erb.swo differ diff --git a/app/views/musics/index.html.erb b/app/views/musics/index.html.erb index d34d47067545d7dc1ebd074ce7cad01f4928ad46..fa3cd8e81fd13f99baa1ec436f7224d22f971ab4 100644 --- a/app/views/musics/index.html.erb +++ b/app/views/musics/index.html.erb @@ -2,7 +2,20 @@ <ul> <% @musics.each do |music| %> - <li class="clickable_links" data-id="<%= music.id %>" > + <li class="clickable_links" + data-id="<%= music.id %>" + data-player="<%= music.type %>" + data-url="<%= + if music.type == "youtube" + track = music.url.sub(/.*v=/,'') + elsif music.type == "soundcloud" + begin + track = $soundcloud.get('/resolve', :url => music.url) + track.id + rescue + "Link down" + end + end %>"> <span class="music_id"><%= music.id %></span> <span class="music_title"><%= music.title%></span> <span class="music_tags"><%= music.tags.map{|t| t.tag}.join(", ") %></span> @@ -11,4 +24,3 @@ <% end %> </ul> <%= paginate @musics %> - diff --git a/app/views/musics/show.html.erb b/app/views/musics/show.html.erb index daf572a97dd60ccc9e84f872ebdfc3dd71e73a4c..3d5e0f829fa65b71d3ec3925d0e8dd9857c12dd1 100644 --- a/app/views/musics/show.html.erb +++ b/app/views/musics/show.html.erb @@ -1,14 +1,21 @@ <ul> - <li> - <span> - <%= @music.id %> - </span> - <span> - <a href="<%= @music.url %>"><%= @music.title %></a> - </span> - <span> - <%= @music.sender %> - </span> + <li class="clickable_links" data-url=" + <%= + if @music.type == "youtube" + track = @music.url.sub(/.*v=/,'') + elsif @music.type == "soundcloud" + begin + track = $soundcloud.get('/resolve', :url => @music.url) + track.id + rescue + "Link down" + end + end %> + "> + <span class="music_id"><%= @music.id %></span> + <span class="music_title"><%= @music.title%></span> + <span class="music_tags"><%= @music.tags.map{|t| t.tag}.join(", ") %></span> + <span class="music_sender"><%= @music.sender_irc %></span> </li> </ul> <%=if @music.type == "youtube" @@ -16,10 +23,9 @@ raw("<iframe id=\"ytplayer\" type=\"text/html\" width=\"640\" height=\"390\" src=\""+track+"?autoplay=1\" frameborder=\"0\"/>") elsif @music.type == "soundcloud" begin - track = $soundcloud.get('/oembed', :url => @music.url) - raw(track['html']) - #track = $soundcloud.get('/resolve', :url => @music.url) - #raw("<iframe width=\"100%\" height=\"400\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/"+track.id+"&auto_play=true&hide_related=false&show_comments=false&show_user=false&show_reposts=false&visual=true\"></iframe>") + track = $soundcloud.get('/oembed', :url => @music.url, :maxwidth => "800", :maxheight => "150", :auto_play => "true") + raw(track['html'].gsub("visual=true&","")) + #The Soundcloud API doesn't enable modification of the parameter 'visual' rescue "Link down" end diff --git a/app/controllers/.tags_controller.rb.swp b/app/views/tags/.show.html.erb.swp similarity index 90% rename from app/controllers/.tags_controller.rb.swp rename to app/views/tags/.show.html.erb.swp index 9c1bd6d5d4b0925b6b28d8ccfd1f21957fe4b8e4..bb4ae67f0b2dceccda59dab28c28f35e349ca1b2 100644 Binary files a/app/controllers/.tags_controller.rb.swp and b/app/views/tags/.show.html.erb.swp differ diff --git a/app/views/tags/show.html.erb b/app/views/tags/show.html.erb index 4aacd270a00d0877c3359a87aaa3824eaab8eb4a..7d464485f060ab6b3dda48e30b214d3becd4f4d3 100644 --- a/app/views/tags/show.html.erb +++ b/app/views/tags/show.html.erb @@ -2,7 +2,20 @@ <ul> <% @musics.each do |music| %> - <li class="clickable_links" data-id="<%= music.id %>" > + <li class="clickable_links" + data-id="<%= music.id %>" + data-player="<%= music.type %>" + data-url="<%= + if music.type == "youtube" + track = music.url.sub(/.*v=/,'') + elsif music.type == "soundcloud" + begin + track = $soundcloud.get('/resolve', :url => music.url) + track.id + rescue + "Link down" + end + end %>"> <span class="music_id"><%= music.id %></span> <span class="music_title"><%= music.title %></span> <span class="music_tags"><%= music.tags.map{|t| t.tag}.join(", ") %></span> @@ -10,4 +23,4 @@ </li> <% end %> </ul> - +<%= paginate @musics %>