배우고픈 공돌이

6. pyaudio를 사용하여 음악 재생하기 본문

ARM/RasberryPi3

6. pyaudio를 사용하여 음악 재생하기

내 마음 아홉수 2017. 9. 27. 23:07

1.  필요한 라이브러리를 설치한다.


2. pygame 모듈을 사용하여 mp3를 재생하는 코드를 제작한다.


import sys

import pygame as pg

def play_music(music_file, volume=0.8):


    # set up the mixer

    freq = 16000    # audio CD quality

    bitsize = -16    # unsigned 16 bit

    channels = 1     # 1 is mono, 2 is stereo

    buffer = 1024 * 2  # number of samples (experiment to get best sound)

    #pg.mixer.quit()

    pg.mixer.init(freq, bitsize, channels, buffer)

    #pg.mixer.init()

    # volume value 0.0 to 1.0

    pg.mixer.music.set_volume(volume)

    clock = pg.time.Clock()

    try:

        pg.mixer.music.load(music_file)

        print("Music file {} loaded!".format(music_file))

    except pg.error:

        print("File {} not found! ({})".format(music_file, pg.get_error()))

        return

    pg.mixer.music.play()

    while pg.mixer.music.get_busy():

        # check if playback has finished

        clock.tick(10)

    pg.quit()

# pick a MP3 music file you have in the working folder

# otherwise give the full file path

# (try other sound file formats too)


if __name__ == '__main__':


    music_file = sys.argv[1]

    # optional volume 0 to 1.0

    volume = 0.8

    play_music(music_file, volume)


3. 재생!



* 재생 주파수를 낮게해서 웅장한 나팔바지가 .... 적정히 Freq를 조절하자.

'ARM > RasberryPi3' 카테고리의 다른 글

8. usb 메모리 마운트하기  (0) 2017.09.27
7. 마이크로 녹음을 해보자.  (0) 2017.09.27
5. 한글 폰트를 다운받고 사용해보자.  (0) 2017.09.20
4. 자동 실행.  (0) 2017.09.11
3. 인터넷 접속이 끊기는 이유.  (0) 2017.09.11
Comments