{"id":1191,"date":"2011-10-09T19:01:54","date_gmt":"2011-10-10T01:01:54","guid":{"rendered":"http:\/\/blog.eastfist.com\/?p=1191"},"modified":"2011-10-09T19:01:54","modified_gmt":"2011-10-10T01:01:54","slug":"adventures-in-2d-computer-game-development-qts-not-so-user-friendly-phonon-media-playing-class","status":"publish","type":"post","link":"https:\/\/eastfist.com\/blog\/2011\/10\/09\/adventures-in-2d-computer-game-development-qts-not-so-user-friendly-phonon-media-playing-class\/","title":{"rendered":"Adventures in 2D computer game development: Qt&#8217;s not-so-user-friendly Phonon media playing class"},"content":{"rendered":"<p>Phononnnnn! Khannn! Khan!<!--more--><\/p>\n<p>Here&#8217;s the code snippet Nokia provides at their <a href=\"http:\/\/doc.qt.nokia.com\/latest\/phonon-overview.html\">documentation site regarding the Phonon class<\/a>:<\/p>\n<blockquote>\n<pre><code>Phonon::MediaObject *music =\n         Phonon::createPlayer(Phonon::MusicCategory,\n                              Phonon::MediaSource(\"\/path\/mysong.wav\"));\n     music->play();<\/code><\/pre>\n<\/blockquote>\n<p>That&#8217;s just to play a simple audio file.  It&#8217;ll work provided the documentation was presented in proper order!  Ironically, I scoured the Internet for hours when I couldn&#8217;t get it to work.  But, surely enough, just a couple of paragraphs later in the documentation, it turns out you need to add a reference to the Qt project file (*.pro):<\/p>\n<blockquote>\n<pre><code>QT += phonon\n\nIt may also look like this, as you need to make sure you make all the necessary references for every class you use:\n\nQT += core gui phonon\n<\/code><\/pre>\n<\/blockquote>\n<p>And also, don&#8217;t forget those <code>#include<\/code> references, either.<\/p>\n<blockquote>\n<pre><code>#include \"phonon\/audiooutput.h\"\n#include \"phonon\/backendcapabilities.h\"\n#include \"phonon\/mediaobject.h\"<\/code>\n<\/pre>\n<\/blockquote>\n<p>So with all the necessary references in place, you can pass a QString of the filename you want as a Phonon::MediaSource to the Phonon:createPlayer function.  And the thing will play most audio formats beside the ubiquitous MP3 format (Ain&#8217;t that wizard, Ani?).  So this works when you implement the code in a new project.<\/p>\n<p>However, if you plug this same snippet of code into another project (say, one you&#8217;ve been working on for a while that you&#8217;re trying to incorporate instrinsic media playback as an aspect of the application), the application compiles fine, but there&#8217;s no audio to be heard.  You can try QSound, but it don&#8217;t work either, Holmes.  So, if you&#8217;re making a game engine and want all the bells and whistles, you&#8217;ll realize how wonky Qt can be: it will play fine one way, but won&#8217;t in another, using the SAME code!<\/p>\n<p>You could, however, just rip off their <a href=\"http:\/\/doc.qt.nokia.com\/latest\/demos-qmediaplayer.html\">mediaplayer demo<\/a>, but why bother with the extra stuff?  That thing compiles fine on my end, but again, when I try to implement their documentation&#8217;s simplest code, I got to get to SMDH (shaking my damn head).  I wonder if I should invest any more time into Phonon, as it seems Nokia might be working on another media playback class called <a href=\"http:\/\/doc.qt.nokia.com\/latest\/qtmultimedia.html\">QtMultimedia<\/a>.  Imagine if you built an entire app on Phonon, only having to scrap it to rewrite it with the new class.<\/p>\n<p>Lesson of the day, kids: if you go with Qt, go with caution.  It&#8217;s very good when it&#8217;s good.  But it comes to a deadlock when it&#8217;s bad.<\/p>\n<figure id=\"attachment_1192\" aria-describedby=\"caption-attachment-1192\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.eastfist.com\/wp-content\/uploads\/2011\/10\/shatnerQtPhonon.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.eastfist.com\/wp-content\/uploads\/2011\/10\/shatnerQtPhonon-360x208.jpg\" alt=\"Shatner Yells Phonon\" title=\"Shatner Yells Phonon\" width=\"360\" height=\"208\" class=\"size-medium wp-image-1192\" \/><\/a><figcaption id=\"caption-attachment-1192\" class=\"wp-caption-text\">Shatner Yells Phonon<\/figcaption><\/figure>\n<p>UPDATE: Oct. 11, 2011 &#8211; My bad. It will work as long as the QString points to a remote, separate file and not an embedded resource. Nokia might fix this in the future. Also, be wary that you&#8217;re using forward slashes &#8220;\/&#8221; rather than backslashes &#8220;\\&#8221; in your file path. As for QtMultimedia, it&#8217;s just a low-level class rather than a high-level class like Phonon. You see, in programming, low-level code is best utilized by a high-level programmer, whereas high-level code is easier for noobs.  Low-level means it&#8217;s closer to machine language and you get to play with file pointers, direct memory and stuff (and risk doing lots of harm to your computer if you don&#8217;t know what you&#8217;re doing). Anyway, QtMultimedia gives access to each frame from video.  That&#8217;s cool because it means Qt users might be able to implement an app that can draw sprites on top of video. \ud83d\ude09<\/p>\n<div style=\"text-align:center\">\n<a href=\"http:\/\/ads.cpxcenter.com\/cpxcenter\/s_click.php?nid=4&amp;pid=31141&amp;sid=32103&amp;cid=9389&amp;cuid=uct1qit&amp;adtype=7&amp;ad=127315&amp;subid=&amp;opt1=&amp;opt2=\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ads.cpxcenter.com\/cpxcenter\/cr_image.php?nid=4&amp;pid=31141&amp;sid=32103&amp;cid=9389&amp;cuid=uct1qit&amp;ad=127315&amp;adtype=7&amp;subid=&amp;opt1=&amp;opt2=\" border=\"0\" width=\"300\" height=\"250\"><\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Phononnnnn! Khannn! Khan!<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[205,357,763,1618,1773,1784,1913,2060,2157,2748],"class_list":["post-1191","post","type-post","status-publish","format-standard","hentry","category-artistic","category-tech","tag-audio","tag-bug","tag-doesnt-work","tag-media","tag-nokia","tag-not-playing","tag-phonon","tag-qt","tag-resource-file","tag-video"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/1191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/comments?post=1191"}],"version-history":[{"count":0,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/1191\/revisions"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/media?parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/categories?post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/tags?post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}