{"id":2338,"date":"2020-10-28T21:17:07","date_gmt":"2020-10-29T02:17:07","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2338"},"modified":"2020-10-28T21:19:31","modified_gmt":"2020-10-29T02:19:31","slug":"logging-audio-playback-actions","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2020\/10\/28\/logging-audio-playback-actions\/","title":{"rendered":"Logging audio playback actions"},"content":{"rendered":"<p>If you have a website with a large number of audio files you might be interested in collecting stats like who listened to what and for how long and where they paused and started again etc. Turns out that collecting data like this and saving in the log table is easier than you would think. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/10\/mp3_tutorial.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/10\/mp3_tutorial-300x220.png\" alt=\"\" width=\"300\" height=\"220\" class=\"alignnone size-medium wp-image-2339\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/10\/mp3_tutorial-300x220.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/10\/mp3_tutorial-768x563.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/10\/mp3_tutorial.png 976w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<!--more--><\/p>\n<p>In this example we will be using MySQL database and here is how our log table looks like. If you enable security in your project then the &#8220;username&#8221; field will contain the username of the current user. &#8220;current_time&#8221; column stores the position of audio file where play or pause was pressed.<\/p>\n<pre name=\"code\" class=\"sql:nocontrols\">\r\nCREATE TABLE `audio_log` (\r\n  `id` int(11) NOT NULL,\r\n  `current_time` double DEFAULT NULL,\r\n  `date` datetime DEFAULT current_timestamp(),\r\n  `username` varchar(50) DEFAULT NULL,\r\n  `action` varchar(50) DEFAULT NULL,\r\n  `file` varchar(50) DEFAULT NULL\r\n);\r\nALTER TABLE `audio_log`\r\n  ADD PRIMARY KEY (`id`);\r\nALTER TABLE `audio_log`\r\n  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;\r\n<\/pre>\n<p>Field with audio content needs to have &#8216;View as&#8217; type Audio. The following code goes to Javascript OnLoad event of the page where Audio is played, i.e. to List or to View page.<\/p>\n<pre name=\"code\" class=\"js:nocontrols\">\r\n$('audio').on(\"play pause\",function(event) {\r\n\tsend_data(event);\r\n});\r\n\r\nfunction send_data(audio_event) {\r\n  \/\/ parsing audio attributes to get the audio file name\r\n\tvar fileinfo = parse_file_props(audio_event.currentTarget.src);\r\n\t\/\/ building array with data to send to the server\r\n  var data = {\r\n\t\taction:audio_event.type,\r\n\t\tfile:fileinfo.file,\r\n\t\tcurrent_time:audio_event.currentTarget.currentTime\r\n\t}\r\n  \/\/ sending data to the server \r\n\t$.get(\"\",{a:\"log\",data:data});\r\n\r\n}\r\n\r\nfunction parse_file_props(src) {\r\n\treturn src.split(\"?\").pop().split(\"&\").reduce(\r\n        function(p,e){\r\n            var a = e.split('=');\r\n            p[ decodeURIComponent(a[0])] = decodeURIComponent(a[1]);\r\n            return p;\r\n        },\r\n        {}\r\n   );\r\n}\r\n<\/pre>\n<p>And the following code goes to the BeforeProcess event of the page with audio files. <\/p>\n<p><b>PHP:<\/b><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\nif( postvalue(\"a\") === \"log\" ){\r\n\t$data = postvalue(\"data\");\r\n\t$data[\"username\"] = Security::getUserName();\r\n\tDB::Insert(\"audio_log\",$data);\r\n\texit();\r\n}\r\n<\/pre>\n<p><b>C#:<\/b><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nif( MVCFunctions.postvalue(\"a\") == \"log\" ){\r\n\tdynamic data = MVCFunctions.my_json_decode(MVCFunctions.postvalue(\"data\"));\r\n\tdata.InitAndSetArrayItem(Security.getUserName(),\"username\");\r\n\tDB.Insert(\"audio_log\",data);\r\n\tMVCFunctions.Exit();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have a website with a large number of audio files you might be interested in collecting stats like who listened to what and for how long and where they paused and started again etc. Turns out that collecting data like this and saving in the log table is easier than you would think.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2338"}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2338"}],"version-history":[{"count":5,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2338\/revisions"}],"predecessor-version":[{"id":2344,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2338\/revisions\/2344"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}