So I’m putting some finishing touches to a plugin ive working in and I’ve run into a weird issue when i use it in file loops. It claims the track index in the array does not exist, but if i hit the file directly, it works!!
If i do this, i can get the track information just fine:
<?= $page->file('01-signs-of-life.mp3')->id3('track')?>
If i do this, it tells me the track index does not exist. it DOES work if there is only 1 mp3 file in the content folder. if its looping over more then one, it fails
<?php foreach($page->audio() as $mp3): ?>
<div class="album">
<img src="<?= $mp3->id3('cover')?>">
<ul>
<li><?= $mp3->id3('album')?></li>
<li><?= $mp3->id3('artist')?></li>
<li><?= $mp3->id3('title')?></li>
<li><?= $mp3->id3('track')?></li>
<li><?= $mp3->id3('trackno')?></li>
<li><?= $mp3->id3('composer')?></li>
<li><?= $mp3->id3('genre')?></li>
<li><?= $mp3->id3('year')?></li>
<li><?= $mp3->id3('duration')?></li>
</ul>
</div>
<?php endforeach ?>
Heres my whole plugin…
<?php
require('lib/getid3/getid3.php');
Kirby::plugin('hashandsalt/id3', [
'fileMethods' => [
'id3' => function ($mediainfo = 'title') {
$getID3 = new getID3;
$mediaid = $getID3->analyze($this->root());
getid3_lib::CopyTagsToComments($mediaid);
$title = $mediaid['comments_html']['title'];
$artist = $mediaid['comments_html']['artist'];
$album = $mediaid['comments_html']['album'];
$genre = $mediaid['comments_html']['genre'];
$yearraw = $mediaid['comments_html']['year'];
$year = implode('', $yearraw);
$composer = $mediaid['comments_html']['composer'];
$duration = $mediaid['playtime_string'];
$coverimg = $mediaid['comments']['picture'][0]['data'];
$coverdata = base64_encode($coverimg);
$cover = 'data:image/jpeg;base64,'.$coverdata;
$tracknum = $mediaid['comments_html']['track'];
$tracknumof = $mediaid['comments_html']['track_number'];
$mediadata = array(
'title' => implode('', $title),
'artist' => implode('', $artist),
'album' => implode('', $album),
'genre' => implode('', $genre),
'year' => substr($year, 0, 4),
'composer' => implode('', $composer),
'duration' => $duration,
'track' => implode('', $tracknum),
'trackno' => implode('', $tracknumof),
'cover' => $cover,
);
return $mediadata[$mediainfo];
// return $mediaid;
}
]
]);
Here is the array im pulling info from:
array(10) {
["title"]=>
string(13) "Signs of Life"
["artist"]=>
string(10) "Pink Floyd"
["album"]=>
string(27) "A Momentary Lapse of Reason"
["genre"]=>
string(4) "Rock"
["year"]=>
string(4) "1987"
["composer"]=>
string(23) "David Gilmour/Bob Ezrin"
["duration"]=>
string(4) "4:24"
["track"]=>
string(1) "1"
["trackno"]=>
string(4) "1/10"
["cover"]=>
string(20147) "data:image/jpeg;base64"
}