사용자 도구

사이트 도구


research:rpi_radio

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
research:rpi_radio [2013/07/28 09:00] – [제어 구문 작성 (PHP)] stop.php에서 잘못된 -u 옵션 수정. changwooresearch:rpi_radio [2014/10/09 21:24] (현재) – 바깥 편집 127.0.0.1
줄 67: 줄 67:
 </code> </code>
 웹서버의 주소를 통해 음악이 재생되는지 확인해본다! 웹서버의 주소를 통해 음악이 재생되는지 확인해본다!
 +
 +===== 개선안 =====
 +위 방법은 테스트이고, 그나마 조금 쓰기 편하게 하려면 다음과 같은 PHP 구문으로 대체하는 것이 좋다. <del>이번의 PHP는 difm이라는 디렉토리 내부의 모든 PLS 파일을 순서대로 목록화하여 보여준다.</del>
 +
 +아래 스크립트에서 각 방송국(스테이션)은 하위 디렉토리 하나를 점유하고 있으며 각 하위 디렉토리에는 각 채널의 그림 파일과 pls 파일을 보유하고 있다.
 +
 +<code php ipcheck.php>
 +<?php
 +function ipcheck()
 +{
 + $allow = array("^192.168.0.*");
 +
 + foreach($allow as $ip)
 + if(eregi($ip, $_SERVER['REMOTE_ADDR']))
 + return;
 +
 + die('Unauthorized');
 +}
 +?>
 +</code>
 +
 +<code php index.php>
 +<?php
 +        include('ipcheck.php');
 +        ipcheck();
 +
 +        function get_channels($sitename)
 +        {
 +                $handle = opendir("./$sitename");
 +                $channels = array();
 +                while(false !== ($entry = readdir($handle)))
 +                {
 +                        $path = pathinfo($entry);
 +                        $name = $path['filename'];
 +                        $ext = $path['extension'];
 +
 +                        if($ext == 'pls')
 +                                array_push($channels, $name);
 +                }
 +                sort($channels);
 +                return $channels;
 +        }
 +
 +        function get_img($channel)
 +        {
 +                $exts = array('.png', '.jpg');
 +                foreach($exts as $e)
 +                {
 +                        $imgfile = $channel.$e;
 +                        if(file_exists($imgfile))
 +                                return $imgfile;
 +                }
 +
 +                return "";
 +        }
 +?>
 +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 +<html>
 +        <head>
 +                <title>di.fm@PI-0 Server!</title>
 +                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 +                <style type="text/css">
 +                .station {margin-right:10px; float:left; }
 +                </style>
 +        </head>
 +        <body>
 +                <h1>Radio at Raspberry Pi Server!</h1>
 +                <h2>Choose your channel!</h2>
 +                <h3><a href="control.php?action=stop">Just stop playing!</a><br></h3>
 +                <div class="station">
 +                        <h3>Digitally Imported</h3>
 +                        <ul>
 +<?php
 +// print all .pls file in the current directory
 +$difm = get_channels('di.fm');
 +foreach($difm as $ch)
 +{
 +        $url = urlencode("di.fm/".$ch);
 +        $img = get_img("di.fm/".$ch);
 +        echo "<li><a href=\"control.php?action=play&channel=$url\"><img src=\"$img\" width=\"48\"> $ch</a>\n";
 +}
 +?>
 +                        </ul>
 +                </div>
 +                <div class="station">
 +                        <h3>SKY.FM</h3>
 +                        <ul>
 +<?php
 +$skyfm = get_channels('sky.fm');
 +foreach($skyfm as $ch)
 +{
 +        $url = urlencode("sky.fm/".$ch);
 +        $img = get_img("sky.fm/".$ch);
 +        echo "<li><a href=\"control.php?action=play&channel=$url\"><img src=\"$img\" width=\"48\"> $ch</a>\n";
 +}
 +?>
 +                        </ul>
 +                </div>
 +        </body>
 +</html>
 +</code>
 +
 +<code php control.php>
 +<?php
 +include('ipcheck.php');
 +ipcheck();
 +?>
 +<html>
 +        <head>
 +        </head>
 +<body>
 +<?php
 +function play($channel)
 +{
 +        $filename = str_replace(' ', '\\ ', $channel);
 +        $cmd = "sudo -u radio mplayer -playlist $filename.pls 1>/dev/null 2>&1 &";
 +        exec($cmd);
 +        echo "<p>Playing ".$channel."... Request from ".$_SERVER["REMOTE_ADDR"]."</p>";
 +}
 +
 +function stop()
 +{
 +        exec('sudo -u radio pkill mplayer > /dev/null 2>&1');
 +        echo "<p>Music stopped</p>";
 +}
 +
 +if($_GET["action"]=="play")
 +{
 +        $channel = $_GET['channel'];
 +        echo "Channel name: ".$channel;
 +        if(file_exists($channel.".pls"))
 +        {
 +                stop();
 +                play($channel);
 +        }
 +        else
 +        {
 +            echo "Channel ".$channel." not found!";
 +        }
 +}
 +else if($_GET["action"]=="stop")
 +{
 +        stop();
 +}
 +else
 +{
 +        echo "Unknown action!";
 +}
 +
 +?>
 +<h1><a href="index.php">Go to index</a></h1>
 +</body>
 +</html>
 +</code>
 +
 +이제는 각 스테이션 별 디렉토리 내부에 pls 파일만 채워 주면 알아서 웹페이지가 갱신되는 편리함이 추가되었다.
 +
 +{{ :research:rpi_radio.png?nolink |}}
 +
 +===== 또다른 개선안 =====
 +  * <del>pls 파일과 같은 이름의 png 파일이 있으면 로딩할 수도 있을 것이다.</del> 각 채널의 png, jpg 그림 파일을 같이 보여주도록 추가하였다.
 +  * <del>사실 이 주소가 외부에서 노출되면 안된다. 외부 IP의 요청인 경우 작동하지 않도록 프로그램을 수정.</del> 내부망에서만 동작하도록 프로그램 수정.
 +  * 일지 중지 (pause)
 +  * 볼륨 조정 (up, down, mute)
 +
  
research/rpi_radio.1375002030.txt.gz · 마지막으로 수정됨: 2014/10/09 21:23 (바깥 편집)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki