레몬 생으로 씹어먹으면 맛있어요. :: ipython notebook에서 음악파일(mp3, wav등) 재생하기.

 구글에 검색하면 이런 저런 모듈을 소개 해줍니다만,


어떤건 운영체제에 의존적인 (윈도우 XP만 된다던가...) 모듈이 있는가 하면,


모듈을 애써 설치해 봤더니 사용법이 너무 어렵다던가 =_=.... 하더군요.


그러다가 HTML5를 이용하는 소스를 찾아서 소개합니다.

# 사용 하기 전에, pylab inline 모드가 켜져 있어야 합니다.
%pylab inline

# this is a wrapper that take a filename and publish an html <audio> tag to listen to it
from IPython.core.display import HTML

def audioPlayer(filepath):
    """ will display html 5 player for compatible browser
    before you call this function, '%pylab inline' must be sentenced

    Parameters :
    ------------
    filepath : relative filepath with respect to the notebook directory
              ( where the .ipynb are not cwd)
              of the file to play

    The browser need to know how to play wav through html5.

    """
    
    src = """
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simple Test</title>
    </head>
    
    <body>
    <audio controls="controls" style="width:600px" autoplay="autoplay">
      <source src="files/%s" type="audio/wav" />
      Your browser does not support the audio element.
    </audio>
    </body>
    """%(filepath)
    display(HTML(src))
    
# 파일 주소는 바꾸셔야 겠죠.    
audioPlayer('bgm_ivent7.mp3')


이 코드를 작성할때 참고한 노트북 파일 입니다.


the sound of hydrogen.ipynb




'프로그래밍 언어 > python' 카테고리의 다른 글

Python Duck Typing  (0) 2014.01.02
파이썬 빌트인/더블 언더바 정리  (0) 2013.12.19
WSGI 튜토리얼  (0) 2013.12.19
Posted by 레몬사과
,