{"id":1357,"date":"2013-10-08T20:43:54","date_gmt":"2013-10-08T20:43:54","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1357"},"modified":"2014-10-07T12:46:40","modified_gmt":"2014-10-07T12:46:40","slug":"playing-audio","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/audio\/playing-audio","title":{"rendered":"Playing Audio"},"content":{"rendered":"<h4>Using Omxplayer\u00a0And System<\/h4>\n<p>This will play an audio file but it won&#8217;t return until playing is complete.<\/p>\n<pre><code>\r\n#include &lt;stdlib.h&gt;\r\n\r\n\r\n\tsystem(\"omxplayer \/home\/pi\/projects\/my_project\/audio\/my_audio.wav\");\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>Play On A Background Thread<\/h4>\n<p>This will trigger omxplayer\u00a0on a separate\u00a0thread and return immediately so your program execution doesn&#8217;t stall until the file has finished playing. \u00a0 You can even use this multiple times and have multiple instances of omxplayer playing on top of each other.<\/p>\n<pre><code>\r\n\tint pid;\r\n\tpid=fork();\r\n\tif(pid==0)\r\n\t{\r\n\t\t\/\/printf(\"I am the child\\n\");\r\n\t\texeclp(\"\/usr\/bin\/omxplayer\", \" \", \"\/home\/pi\/projects\/my_project\/audio\/my_file.wav\", NULL);\t\t\/\/Execute file: file, arguments (1 or more strings followed by NULL)\r\n\t\t_exit(0);\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/printf(\"I am the parent\\n\");\r\n\t\twait();\r\n\t} \r\n<\/code><\/pre>\n<h5>Closing Player On Background Thread<\/h5>\n<pre><code>\r\n\tsystem(\"killall omxplayer.bin\");\r\n<\/code><\/pre>\n<h4>Adjusting Playback Of Omxplayer On A Background Thread<\/h4>\n<p>Resources:<\/p>\n<p><a href=\"http:\/\/www.raspberrypi.org\/forums\/viewtopic.php?f=38&amp;t=7987\">http:\/\/www.raspberrypi.org\/forums\/viewtopic.php?f=38&amp;t=7987<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Omxplayer\u00a0And System This will play an audio file but it won&#8217;t return until playing is complete. #include &lt;stdlib.h&gt; system(&#8220;omxplayer \/home\/pi\/projects\/my_project\/audio\/my_audio.wav&#8221;); &nbsp; Play On A Background Thread This will trigger omxplayer\u00a0on a separate\u00a0thread and return immediately so your program execution doesn&#8217;t stall until the file has finished playing. \u00a0 You can even use this multiple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-1357","post","type-post","status-publish","format-standard","hentry","category-audio"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1357"}],"version-history":[{"count":5,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1357\/revisions"}],"predecessor-version":[{"id":1818,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1357\/revisions\/1818"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}