code help

Permalink
Hi

I'm wanting to add to a single page some code that will pull the file link from an audio file attached to the page (handle 'audio'), and then for that link to be playable through say an html code such as:

<?php
$DownloadURL = $c->getAttribute('audio');
?>
<audio controls>
  <source src="<a href="<?php echo $DownloadURL; ?>"></a>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>


There is an audio file attached to the pages attribute, and the player is displayed on the page, but it is for ever loading. I've tried this using the PHP Add-on Block and an error gets displayed which says:

An unexpected error occurred.
Call to a member function getAttribute() on null

I'm running on 5.8.1. Thanks for any pearls of wisdom.

Justyn

 
hutman replied on at Permalink Reply
hutman
Couple of things, 1 if you are using a block you will need to put this code in it so that it knows what $c is

$c = Page::getCurrentPage();

As for the code you have I believe what you need is this

<?php
$DownloadURL = $c->getAttribute('audio');
?>
<audio controls>
  <source src="<?php echo $DownloadURL; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
justynpride replied on at Permalink Reply
Thanks. So code now is
<?php
$c = Page::getCurrentPage();
$DownloadURL = $c->getAttribute('audio');
?>
<audio controls>
  <source src="<a href="<?php echo $DownloadURL; ?>"></a>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>


Error message is now:
An unexpected error occurred.
Object of class Concrete\Core\Entity\File\File could not be converted to string
hutman replied on at Permalink Best Answer Reply
hutman
Try this

<?php
$c = Page::getCurrentPage();
$file = $c->getAttribute('audio');
?>
<audio controls>
  <source src="<?php echo $file->getURL(); ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
justynpride replied on at Permalink Reply
Very helpful thanks. It is a huge step forward. The player displays and there is no error message. Unfortunately it appears to be constantly trying to load the file so doesn't play. Any helpful thoughts?
hutman replied on at Permalink Reply
hutman
Can you send a link to the page? There is no way to tell what might be wrong from just seeing this code.
justynpride replied on at Permalink Reply
hutman replied on at Permalink Reply
hutman
You need to remove the a tag from the src, if you look at my example it's been removed.
justynpride replied on at Permalink Reply
That's spot on. Sorry I thought I had copied and pasted the whole code, but obviously not. It works perfectly. Thank you SO much.