I have an ASP.NET MVC site and JPlayer plugin, and I want to add a history entry to database.

When I send by AJAX title of current track when ended event occurs I get the title of next track.

For example:

Playlist :

  • 1) First song.mp3
  • 2) Second song.mp3

When first song ends, I get in controller Second song.mp3

View:

@Html.Raw("<script>$(document).ready(function() {");
@Html.Raw("var cssSelector = { jPlayer: '#jquery_jplayer_1', cssSelectorAncestor: '#jp_container_1' };");
@Html.Raw("var playlist = [");
foreach(var song in Model.Song) {
    @Html.Raw("{ title : '" + @song.song_name + "' ," + "mp3 : '/Content/Uploads/" + @song.song_path +  "'},")
}
@Html.Raw("];");
@Html.Raw("var actionUrl = '" + @Url.Action("History", "Music") + "';"); 
@Html.Raw("var options = { swfPath : '/Scripts/jplayer', supplied : 'mp3', ");
if(HttpContext.Current.User.Identity.IsAuthenticated) {
    @Html.Raw(" ended : function () {" +
    "$.ajax({" +
    "type: 'POST'," +
    "url : actionUrl," +
    "data : { isEnded: 'true', trackname: myPlaylist.playlist[myPlaylist.current].title, albumId : " + "\"" + @Model.album_id.ToString() + "\"}," +
    "success: function (data) {}," +
    "error: function (xhr, ajaxOptions, thrownError) {" +
        "alert('Cant add to history');" +
    "}});}");
}
@Html.Raw("};");
@Html.Raw("var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);");

Thanks in advance :)

有帮助吗?

解决方案

Hmm, solution, better solutions can be the answer :)

@Html.Raw("var actionUrl = '" + @Url.Action("History", "Music") + "';"); 
@Html.Raw("var curTrack;");
@Html.Raw("var options = { swfPath : '/Scripts/jplayer', supplied : 'mp3', ");
if(HttpContext.Current.User.Identity.IsAuthenticated) {
    @Html.Raw(" play : function () {" + "curTrack = myPlaylist.playlist[myPlaylist.current].title;},");
    @Html.Raw(" ended : function () {" +
    "$.ajax({" +
    "type: 'POST'," +
    "url : actionUrl," +
    "data : { isEnded: 'true', trackname: curTrack, albumId : " + "\"" + @Model.album_id.ToString() + "\"}," +
    "success: function (data) {}," +
    "error: function (xhr, ajaxOptions, thrownError) {" +
        "alert('Cant add to history');" +
    "}});}");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top