Internetradio Browser html / php

  • Hallo,
    ich habe mit Hilfe einer Anleitung ein Internetradio "gebastelt".

    Das Radio kann ich über den Browser über die IP des Pi's steuern. Dazu benutze ich z.B.:

    HTML
    index.html
    <html>
    <body>
    <br><br>
    <center>
    <a href="sender1.php"><img src="logoSender1.jpg"></a>
    ...
    </center>
    </body></html>
    PHP
    sender1.php
    <?php
    system ( "mpc play 1");
    header("Location:index.html");
    ?>[/php]
    für die Senderauswahl.
    
    
    Über:
    [code]mpc current


    und

    Code
    mpc volume


    kann ich mir am Pi das aktuelle Lied (bzw. Infos vom Radiosender) und die eingestellte Lautstärke anzeigen lassen.

    Wie füge ich diese Funktionen in die "html" ein?

  • Du hast vermutlich http://forum.loxone.com/dede/software/…-raspberry.html befolgt?

    Versuchs mal damit:

    index.php
    [code=php]
    <html>
    <body>
    <br/><br/>
    <center>
    <a href="<?php system("mpc play 1"); ?>"><img src="logoSender1.jpg"></a>
    <a href="<?php system("mpc current"); ?>">current</a>
    </center>
    </body>
    </html>
    [/php]

    //EDIT:

    Wobei ich glaub das wäre besser:

    &quot;index.html&quot;

    [code=php]<html>
    <head>
    <title>Internetradio</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jque…1/jquery.min.js"></script>
    <script type="text/javascript">
    function control(command){
    $('#result').load("functions.php?cmd="+command);
    }
    function sender(num){
    $('#result').load("functions.php?sender="+num);
    }
    </script>
    </head>
    <body>
    <p>
    <button type="button" onclick="control('current')" value="">Current playing</button>
    <button type="button" onclick="control('volume')" value="">Current volume</button>
    <button type="button" onclick="control('leiser')" value="">Leiser</button>
    <button type="button" onclick="control('lauter')" value="">Lauter</button>
    <button type="button" onclick="control('stop')" value="">Stop</button>
    </p>
    <center>
    <button type="button" onclick="sender('1')" value=""><img src="logoSender1.jpg"></button><br/>
    <button type="button" onclick="sender('2')" value=""><img src="logoSender2.jpg"></button><br/>
    </center>
    <p><div id="result"></div></p>
    </body>
    </html>
    [/php]

    &quot;functions.php&quot;

    [code=php]<?php

    if (isset($_GET['cmd'])) {
    $command = $_GET['cmd'];
    if ($command == "current") {
    exec("mpc current", $output, $return_var);
    } elseif ($command == "volume") {
    exec("mpc volume", $output, $return_var);
    } elseif ($command == "leiser") {
    exec("mpc volume -5", $output, $return_var);
    } elseif ($command == "lauter") {
    exec("mpc volume +5", $output, $return_var);
    } elseif ($command == "stop") {
    exec("mpc stop", $output, $return_var);
    }
    }
    if (isset($_GET['sender'])) {
    $number = $_GET['sender'];
    exec("mpc play ".$number, $output, $return_var);
    }

    if (isset($return_var) AND $return_var >= 1) {
    echo "ERROR: <br/>";
    echo exitcode($return_var)."\n";
    } else if (isset($output) AND !empty($output)) {
    foreach($output AS $line) { echo $line."<br/>\n"; }
    }

    function exitcode($code) {
    switch($code) {
    case 0: $Reason = "Successful"; break;
    case 1: $Reason = "General Error (Miscellaneous errors, such as 'divide by zero' and other impermissible operations)"; break;
    case 2: $Reason = "Incorrect Usage"; break;
    case 126: $Reason = "Command found but not executable (Permission problem)"; break;
    case 127: $Reason = "Command not found (Possible problem with \$PATH or a typo)"; break;
    case 128: $Reason = "Invalid argument to exit (exit takes only integer args in the range 0 - 255)"; break;
    #case 130: $ReturnCode = "Script terminated by Control-C"; break;
    default:
    # http://de.wikipedia.org/wiki/Signal_(Unix)
    $Signal = $code - 128;
    switch($Signal) {
    #case 0: $Reason = ""; break;
    case 1: $Reason = "Hangup detected on controlling terminal or death of controlling process"; break;
    case 2: $Reason = "Interrupt from keyboard; interactive attention signal. Script terminated by Control-C"; break;
    case 3: $Reason = "Quit from keyboard."; break;
    case 4: $Reason = "Illegal instruction."; break;
    case 5: $Reason = "Trace/breakpoint trap."; break;
    case 6: $Reason = "Abnormal termination; abort signal from abort(3)."; break;
    case 7: $Reason = "BUS error (bad memory access)."; break;
    case 8: $Reason = "'Floating-point exception': erroneous arithmetic operation."; break;
    case 9: $Reason = "Kill, unblockable."; break;
    case 10: $Reason = "User-defined signal 1."; break;
    case 11: $Reason = "'Segmentation violation': invalid memory reference."; break;
    default: $Reason = $code;
    }
    }
    return $Reason;
    }

    ?>[/php]

  • Ahja sorry, " innerhalb " geht nicht, also dann so: <a href="<?php system('mpc current'); ?>">current</a>

    Aber guck dir auch mal mein Edit oben an ;)


    //EDIT:

    Oder hier, eine wie ich finde noch bessere Version - du brauchst eigentlich nur die $Playlist Variable anpassen und ggf $MaxRowCounter für die Anzahl der Einträge pro Zeile in der Tabelle. Den Rest liest das Script dann aus der Playlist aus usw :D

    &quot;index.php&quot;

    [code=php]
    <?php
    //------------------------------------------------------------------------------
    $Playlist = "/var/lib/mpd/playlists/internetradio.m3u";
    $MaxRowCounter = 3;
    //------------------------------------------------------------------------------
    /*
    Edit: /var/lib/mpd/playlists/internetradio.m3u
    and add something like:

    http://stream.laut.fm/best_of_80s
    http://stream.laut.fm/radiofunclub80
    http://stream.laut.fm/maximix
    http://stream.laut.fm/eurosmoothjazz
    http://stream.laut.fm/jahfari
    http://stream.laut.fm/just80s
    http://stream.laut.fm/rockin_c

    */

    //------------------------------------------------------------------------------
    require_once("functions.php");
    exec("mpc load ".basename($Playlist, '.m3u'), $output, $return_var);
    if (isset($return_var) AND $return_var >= 1) {
    echo "ERROR: <br/>";
    echo exitcode($return_var)."<br/>\n";
    }
    if (isset($output) AND !empty($output)) {
    echo "output: <br/>";
    foreach($output AS $line) { echo $line."<br/>\n"; }
    }
    ?>
    <html>
    <head>
    <title>Internetradio</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jque…1/jquery.min.js"></script>
    <script type="text/javascript">
    function control(command){
    $('#result').load("functions.php?cmd="+command);
    }
    function sender(num){
    $('#result').load("functions.php?sender="+num);
    }
    </script>
    </head>
    <body>
    <p>
    <button type="button" onclick="control('current')" value="">Spiele aktuell</button>
    <button type="button" onclick="control('volume')" value="">Derzeitige Lautstarke</button>
    <button type="button" onclick="control('leiser')" value="">Leiser</button>
    <button type="button" onclick="control('lauter')" value="">Lauter</button>
    <button type="button" onclick="control('stop')" value="">Stop</button>
    </p>
    <center>
    <table border="1" cellpadding="7" cellspacing="1" bordercolorlight>
    <?php
    $StreamFile = file($Playlist);
    $Counter = 0;
    foreach($StreamFile AS $line_num => $line) {
    if ($Counter == 0) { echo "<tr>\n"; }
    $Sender = basename($line);
    $SenderCount = $line_num + 1;
    echo "<td><a href='' onclick='sender('".$SenderCount."')' title='".$SenderCount."'>".$Sender."</a></td>\n";
    $Counter++;
    if ($MaxRowCounter == $Counter) { $Counter = 0; echo "</tr>\n"; }
    }
    echo "</tr>\n";
    ?>
    </table>
    </center>
    <p><div id="result"></div></p>
    </body>
    </html>
    [/php]

    &quot;functions.php&quot;

    [code=php]<?php

    if (isset($_GET['cmd'])) {
    $command = $_GET['cmd'];
    if ($command == "current") {
    exec("mpc current", $output, $return_var);
    } elseif ($command == "volume") {
    exec("mpc volume", $output, $return_var);
    } elseif ($command == "leiser") {
    exec("mpc volume -5", $output, $return_var);
    } elseif ($command == "lauter") {
    exec("mpc volume +5", $output, $return_var);
    } elseif ($command == "stop") {
    exec("mpc stop", $output, $return_var);
    }
    }
    if (isset($_GET['sender'])) {
    $number = $_GET['sender'];
    exec("mpc play ".$number, $output, $return_var);
    }

    if (isset($return_var) AND $return_var >= 1) {
    echo "ERROR: <br/>";
    echo exitcode($return_var)."\n";
    }
    if (isset($output) AND !empty($output)) {
    foreach($output AS $line) { echo $line."<br/>\n"; }
    }

    function exitcode($code) {
    switch($code) {
    case 0: $Reason = "Successful"; break;
    case 1: $Reason = "General Error (Miscellaneous errors, such as 'divide by zero' and other impermissible operations)"; break;
    case 2: $Reason = "Incorrect Usage"; break;
    case 126: $Reason = "Command found but not executable (Permission problem)"; break;
    case 127: $Reason = "Command not found (Possible problem with \$PATH or a typo)"; break;
    case 128: $Reason = "Invalid argument to exit (exit takes only integer args in the range 0 - 255)"; break;
    #case 130: $ReturnCode = "Script terminated by Control-C"; break;
    default:
    # http://de.wikipedia.org/wiki/Signal_(Unix)
    $Signal = $code - 128;
    switch($Signal) {
    #case 0: $Reason = ""; break;
    case 1: $Reason = "Hangup detected on controlling terminal or death of controlling process"; break;
    case 2: $Reason = "Interrupt from keyboard; interactive attention signal. Script terminated by Control-C"; break;
    case 3: $Reason = "Quit from keyboard."; break;
    case 4: $Reason = "Illegal instruction."; break;
    case 5: $Reason = "Trace/breakpoint trap."; break;
    case 6: $Reason = "Abnormal termination; abort signal from abort(3)."; break;
    case 7: $Reason = "BUS error (bad memory access)."; break;
    case 8: $Reason = "'Floating-point exception': erroneous arithmetic operation."; break;
    case 9: $Reason = "Kill, unblockable."; break;
    case 10: $Reason = "User-defined signal 1."; break;
    case 11: $Reason = "'Segmentation violation': invalid memory reference."; break;
    default: $Reason = $code;
    }
    }
    return $Reason;
    }

    ?>[/php]

    wenn du Fragen hast dann bitte gezielt fragen ;)


    //EDIT: Eine etwas verbesserte Version in der ein paar Fehler behoben sind (kein seiten reload nach klick auf einer der Buttons bzw allgemein keine Buttons mehr)

    &quot;index.php&quot;

    [code=php]
    <?php
    //------------------------------------------------------------------------------
    $Playlist = "/var/lib/mpd/playlists/internetradio.m3u";
    $MaxRowCounter = 3;
    //------------------------------------------------------------------------------
    /*
    Edit: /var/lib/mpd/playlists/internetradio.m3u
    and add something like:

    http://stream.laut.fm/best_of_80s
    http://stream.laut.fm/radiofunclub80
    http://stream.laut.fm/maximix
    http://stream.laut.fm/eurosmoothjazz
    http://stream.laut.fm/jahfari
    http://stream.laut.fm/just80s
    http://stream.laut.fm/rockin_c

    */

    //------------------------------------------------------------------------------
    require_once("functions.php");

    exec("mpc load ".basename($Playlist, '.m3u'), $output, $return_var);
    if (isset($return_var) AND $return_var >= 1) {
    echo "ERROR: <br/>";
    echo exitcode($return_var)."<br/>\n";
    }
    /*
    if (isset($output) AND !empty($output)) {
    echo "output: <br/>";
    foreach($output AS $line) { echo $line."<br/>\n"; }
    }
    */
    ?>
    <html>
    <head>
    <title>Internetradio</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jque…1/jquery.min.js"></script>
    <script type="text/javascript">
    function control(command){
    $('#result').load("functions.php?cmd="+command);
    }
    function sender(num){
    $('#result').load("functions.php?sender="+num);
    }
    function currentTitle(){
    $('#currentTitle').load("functions.php?cmd=current");
    }
    var intervalID = false;
    function init() {
    if (intervalID) { clearInterval(intervalID); }
    intervalID = setInterval(currentTitle, 1000);
    }
    </script>
    </head>
    <body onload="init();">
    <p>
    <a href="#" onclick="currentTitle();">Spiele aktuell</a> •
    <a href="#" onclick="control('volume');">Derzeitige Lautstarke</a> •
    <a href="#" onclick="control('leiser');">Leiser</a> •
    <a href="#" onclick="control('lauter');">Lauter</a> •
    <a href="#" onclick="control('stop');">Stop</a>
    </p>
    <center>
    <table border="1" cellpadding="7" cellspacing="1" bordercolorlight>
    <?php
    $StreamFile = file($Playlist);
    $Counter = 0;
    foreach($StreamFile AS $line_num => $line) {
    if ($Counter == 0) { echo "<tr>\n"; }
    $Sender = basename($line);
    $SenderCount = $line_num + 1;
    echo "<td><a href='#' onclick='sender(".$SenderCount.");' title=".$SenderCount.">".$Sender."</a></td>\n";
    $Counter++;
    if ($MaxRowCounter == $Counter) { $Counter = 0; echo "</tr>\n"; }
    }
    echo "</tr>\n";
    ?>
    </table>
    </center>
    <p><div id="result"></div></p>
    <p><div id="currentTitle"></div></p>
    </body>
    </html>
    [/php]


  • Wow, sehr viele Infos für einen HTML und php Neuling... da muss ich mich erst durch arbeiten.

    Wie gesagt, wenn Fragen sind erklär ich dir das gerne


    //EDIT: Siehe dazu auch mal hier: [Tutorial] Raspberry über Webinterface steuern - Shellskripte ausführen

  • Also ich glaub ich habs soweit verstanden... :-/
    Gibt es noch eine Möglichkeit den Titel (also current) in gewissen Zeiträumen automatisch zu aktualisieren?

  • Ja, indem du in Javascript einen Intervall einstellst der die entsprechende function dann aufruft: http://www.w3schools.com/jsref/met_win_setinterval.asp

    Beispiel:
    [code=php]
    <script type="text/javascript">
    function control(command){
    $('#result').load("functions.php?cmd="+command);
    }
    function sender(num){
    $('#result').load("functions.php?sender="+num);
    }

    setInterval(control, 1000, 'current');
    </script>
    [/php]

  • Schon mal vielen Dank für die tolle Hilfe!

    Ich will jetzt ein Icon für "lauter", "leiser" und "stop" verwenden. Es schaut blöd aus, wenn ich das Bild des Icons auf einen rechtechigen Button lege. Kann ich das Bild des Icons klickbar machen?

  • Ja so ein button Bild kannst dann ja genau so wie beim "a href" mithilfe <img ... einbinden und die Größe selber festlegen:

    [code=php]<button type="button" onclick="control('stop')" value=""> <img src="stop.jpg" width="106" height="109" alt="Stop"> </button>[/php]

    Es gibt auch für ein " a href " das Attribut "onclick", aber ein Hyperlink verhält sich eben anders, das läd dann die Seite neu (siehe automatisch generierte Senderliste in Beitrag#4)

  • Jein. Also nur den Rahmen transparent machen ist aufwendiger - glaube das würde sich hier nicht lohnen.. Das müsstest du glaub ich wenn dann mit einem Bildbarbeitungsprogramm wie z.B. GIMP machen.

    Du könntest aber auch das ganze Bild transparent machen lassen - was zumindest bei solchen Bildern eventuell den selben Effekt hätte. Allerdings sind solche Sachen leider nicht einheitlich standardisiert, manche Browser erfordern einen anderen Code... Siehe dazu zB.: http://www.css4you.de/trickkiste/tr00018.html

    Aber was meinst du mit Rahmen? Die weiße Kante da zum schwarzen Absatz wo es dann zum Grau wird? Wenn ja dann schneid das halt einfach weg, zum Beispiel mit Paint :D

  • Border auf 0 setzen, dann verschindet der Rahmen.

    Code
    <img src="stop.jpg" width="106" height="109" alt="Stop"  border="0">

    Offizieller Schmier und Schmutzfink des Forum.
    Warum einfach wenn's auch schwer geht ?

    Kein Support per PN !
    Fragen bitte hier im Forum stellen. So hat jeder etwas davon.

    Einmal editiert, zuletzt von Der_Imperator (10. Februar 2015 um 19:06)

  • Zitat

    ...

    &quot;functions.php&quot;


    [/quote]

    hmm... das hat bei meinen Button keine Auswirkung

    Einmal editiert, zuletzt von Schl87 (10. Februar 2015 um 19:44)


  • Wie bzw. wo setzt sich die Ausgabe für den Browser zusammen? Wenn ich einen Button drücke bekomme ich drei Zeilen mit Infos. Wenn ich das Javaskript mir


    [/quote]

    hmm... das hat bei meinen Button keine Auswirkung
    [/quote]

    border hat hierbei afaik keinen Einfluss da es sich bei diesem "Problem" um ein Teil des Bildes handelt

    Wir wissen aber leider nicht was genau du mit Rahmen meinst... Welchen Teil des Bildes bezeichnest du als Rahmen welchen du weg haben willst?

  • Die Frage: "Wie bzw. wo setzt sich die Ausgabe für den Browser zusammen? Wenn ich einen Button drücke bekomme ich drei Zeilen mit Infos. Wenn ich das Javaskript mir
    [code=php]
    setInterval(control, 1000, 'current'); [/php]
    aktualisiere bekomme ich nur die erste Zeile (der drei vorherigen)."
    ... bezog sich auf functions.php - wie setzt sich hier die Ausgabe beim drücken eines Kopfes zusammen?
    [code=php]
    <?php

    if (isset($_GET['cmd'])) {
    $command = $_GET['cmd'];
    if ($command == "current") {
    exec("mpc current", $output, $return_var);
    } elseif ($command == "volume") {
    exec("mpc volume", $output, $return_var);
    } elseif ($command == "leiser") {
    exec("mpc volume -5", $output, $return_var);
    } elseif ($command == "lauter") {
    exec("mpc volume +5", $output, $return_var);
    } elseif ($command == "stop") {
    exec("mpc stop", $output, $return_var);
    }
    }
    if (isset($_GET['sender'])) {
    $number = $_GET['sender'];
    exec("mpc play ".$number, $output, $return_var);
    }

    if (isset($return_var) AND $return_var >= 1) {
    echo "ERROR: <br/>";
    echo exitcode($return_var)."\n";
    }
    if (isset($output) AND !empty($output)) {
    foreach($output AS $line) { echo $line."<br/>\n"; }
    }

    function exitcode($code) {
    switch($code) {
    case 0: $Reason = "Successful"; break;
    case 1: $Reason = "General Error (Miscellaneous errors, such as 'divide by zero' and other impermissible operations)"; break;
    case 2: $Reason = "Incorrect Usage"; break;
    case 126: $Reason = "Command found but not executable (Permission problem)"; break;
    case 127: $Reason = "Command not found (Possible problem with \$PATH or a typo)"; break;
    case 128: $Reason = "Invalid argument to exit (exit takes only integer args in the range 0 - 255)"; break;
    #case 130: $ReturnCode = "Script terminated by Control-C"; break;
    default:
    # http://de.wikipedia.org/wiki/Signal_(Unix)
    $Signal = $code - 128;
    switch($Signal) {
    #case 0: $Reason = ""; break;
    case 1: $Reason = "Hangup detected on controlling terminal or death of controlling process"; break;
    case 2: $Reason = "Interrupt from keyboard; interactive attention signal. Script terminated by Control-C"; break;
    case 3: $Reason = "Quit from keyboard."; break;
    case 4: $Reason = "Illegal instruction."; break;
    case 5: $Reason = "Trace/breakpoint trap."; break;
    case 6: $Reason = "Abnormal termination; abort signal from abort(3)."; break;
    case 7: $Reason = "BUS error (bad memory access)."; break;
    case 8: $Reason = "'Floating-point exception': erroneous arithmetic operation."; break;
    case 9: $Reason = "Kill, unblockable."; break;
    case 10: $Reason = "User-defined signal 1."; break;
    case 11: $Reason = "'Segmentation violation': invalid memory reference."; break;
    default: $Reason = $code;
    }
    }
    return $Reason;
    }

    ?>[/php]


    Den Rand den ich meine habe ich hier makiert:

Jetzt mitmachen!

Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!