メニュー

pygame.mixer.music

ストリーミング再生を操作するpygameモジュールです

このmusicモジュールはpygame.mixerと密接につながっています。musicモジュールを使用することでサウンドミキサーでの音楽の再生を制御できます。

音楽再生と通常の音声再生の違いについてですが、音楽はストリーミング再生なので再生前に一度にファイル全てを読み込むことはありません。 このミキサーでは一度に一つの音楽しか再生することができません。

MP3ファイルのサポートには制限があるので気をつけてください。使用環境によっては、サポートされていないファイル形式を使用するとプログラムがクラッシュする可能性もあります(例えば、Debian Linuxなど)。MP3ファイルがサポートされていない環境では、代わりにOGGファイルの使用を検討してください。

pygame module for controlling streamed audio

The music module is closely tied to pygame.mixer. Use the music module to control the playback of music in the sound mixer.

The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.

Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.


pygame.mixer.music.load

再生する音楽ファイルを読み込みます。
pygame.mixer.music.load(filename): return Nonepygame.mixer.music.load(object): return None

この命令で音楽ファイル名や音楽オブジェクトを読み込み、再生の準備をします。既に再生中の音楽があった場合、その再生は停止されます。この命令だけでは音楽の再生は始まりません。

Load a music file for playback
pygame.mixer.music.load(filename): return Nonepygame.mixer.music.load(object): return None

This will load a music filename/file object and prepare it for playback. If a music stream is already playing it will be stopped. This does not start the music playing.


pygame.mixer.music.play

音楽のストリーミング再生を開始します。
pygame.mixer.music.play(loops=0, start=0.0): return None

読み込んだ音楽をストリーミング再生します。既に再生中の音楽だった場合は、もう一度最初から再生が始まります。

loops引数に値を設定することで、音楽の繰り返し再生回数を制御します。play(5)で実行すると、一度再生を行った後に5回の繰り返し再生が行われるので合計6回の再生されます。loops引数に-1が設定されていた場合、音楽再生は無限に繰り返されます。

start引数を設定することで、音楽の再生開始位置を制御することができます。再生開始位置は、再生する音楽ファイルの形式によって左右されます。MP3ファイルやOGGファイルの場合、start引数に設定した開始位置はミリセカンド単位の時間として扱われます。MODファイルの場合だと再生するパターンの番号として扱われます(MODファイルではパターンと呼ばれる演奏データを順序良く並べて音楽を再生する)。設定できない値をstart引数に設定した場合、エラー:NotImplementedErrorが発生します。

Start the playback of the music stream
pygame.mixer.music.play(loops=0, start=0.0): return None

This will play the loaded music stream. If the music is already playing it will be restarted.

The loops argument controls the number of repeats a music will play. play(5) will cause the music to played once, then repeated five times, for a total of six. If the loops is -1 then the music will repeat indefinitely.

The starting position argument controls where in the music the song starts playing. The starting position is dependent on the format of music playing. MP3 and OGG use the position as time (in seconds). MOD music it is the pattern order number. Passing a startpos will raise a NotImplementedError if it cannot set the start position


pygame.mixer.music.rewind

音楽を最初から再生します。
pygame.mixer.music.rewind(): return None

現在再生されている音楽をリセットして、もう一度最初から再生を開始します。

restart music
pygame.mixer.music.rewind(): return None

Resets playback of the current music to the beginning.


pygame.mixer.music.stop

音楽の再生を終了します。
pygame.mixer.music.stop(): return None

音楽が再生中の場合、それを終了させます。

stop the music playback
pygame.mixer.music.stop(): return None

Stops the music playback if it is currently playing.


pygame.mixer.music.pause

音楽の再生を一時停止します。
pygame.mixer.music.pause(): return None

再生中の音楽を一時停止します。pygame.mixer.music.unpause命令を実行することで一時停止した音楽を再開できます。

temporarily stop music playback
pygame.mixer.music.pause(): return None

Temporarily stop playback of the music stream. It can be resumed with the pygame.mixer.music.unpause - resume paused music function.


pygame.mixer.music.unpause

停止した音楽を再開させます。
pygame.mixer.music.unpause(): return None

一時停止させた音楽の再生を再開させます。

resume paused music
pygame.mixer.music.unpause(): return None

This will resume the playback of a music stream after it has been paused.


pygame.mixer.music.fadeout

再生中の音楽の音を徐々に小さくしていった上で終了させます。
pygame.mixer.music.fadeout(time): return None

time引数で設定した時間(ミリ秒単位)の中で再生中の音楽の音を徐々に小さくしていった上で、再生を終了します。

※原文には「再生が終了するまでの間、この命令はプログラムのブロックを行います(Note, that this function blocks until the music has faded out.)」と記載されていますが、フェードアウト中でも音楽の停止や再生が行えるようなので、その部分の記載は省いています。

stop music playback after fading out
pygame.mixer.music.fadeout(time): return None

This will stop the music playback after it has been faded out over the specified time (measured in milliseconds).

Note, that this function blocks until the music has faded out.


pygame.mixer.music.set_volume

音楽のボリュームを設定します。
pygame.mixer.music.set_volume(value): return None

再生する音楽のボリュームを設定します。value引数には0.0から1.0までの値を設定します。現在再生中のものとは別の音楽が新たに読み込まれた場合、設定したボリュームの値はリセットされます。

set the music volume
pygame.mixer.music.set_volume(value): return None

Set the volume of the music playback. The value argument is between 0.0 and 1.0. When new music is loaded the volume is reset.


pygame.mixer.music.get_volume

音楽のボリュームを取得します。
pygame.mixer.music.get_volume(): return value

ミキサーの現在のボリュームを戻り値として取得します。取得されるボリュームは0.0から1.0までの値となります。

get the music volume
pygame.mixer.music.get_volume(): return value

Returns the current volume for the mixer. The value will be between 0.0 and 1.0.


pygame.mixer.music.get_busy

音楽が再生中かどうかを確認します。
pygame.mixer.music.get_busy(): return bool

音楽が再生中の場合はTrueが戻り値として返されます。再生されていない場合はFalseが戻り値として返されます。

check if the music stream is playing
pygame.mixer.music.get_busy(): return bool

Returns True when the music stream is actively playing. When the music is idle this returns False.


pygame.mixer.music.get_pos

音楽の再生時間を取得します。
pygame.mixer.music.get_pos(): return time

音楽が再生された時間をミリセカンド単位の値で取得します。ここで取得される時間は、単純に再生された時間のみを表します。;再生開始位置によって時間が加算されるようなことはありません。

get the music play time
pygame.mixer.music.get_pos(): return time

This gets the number of milliseconds that the music has been playing for. The returned time only represents how long the music has been playing; it does not take into account any starting position offsets.


pygame.mixer.music.queue

現在再生中の音楽が終了した後に再生されるように、音楽ファイルの再生準備をします。
pygame.mixer.music.queue(filename): return None

この命令では音楽ファイルを読み込み、再生の順番待ちに設定します。順番待ちの音楽は、現在再生中の音楽が正常に終了した後に再生が開始されます。再生中の音楽が途中で終了したり変更されたりした場合、順番待ち音楽の再生は行われません。

下記の例ではbach.oggを6回再生した後にmozart.oggを1回再生します:

    pygame.mixer.music.load('bach.ogg')
    pygame.mixer.music.play(5)        #5回ではなく6回再生が行われます! 
    pygame.mixer.music.queue('mozart.ogg')
queue a music file to follow the current
pygame.mixer.music.queue(filename): return None

This will load a music file and queue it. A queued music file will begin as soon as the current music naturally ends. If the current music is ever stopped or changed, the queued song will be lost.

The following example will play music by Bach six times, then play music by Mozart once:

    pygame.mixer.music.load('bach.ogg')
    pygame.mixer.music.play(5)        # Plays six times, not five!
    pygame.mixer.music.queue('mozart.ogg')

pygame.mixer.music.set_endevent

音楽の再生が終了した時に、指定したイベントを発生させます。
pygame.mixer.music.set_endevent(): return Nonepygame.mixer.music.set_endevent(type): return None

この命令によって、音楽の再生が終了した時にPygameがイベントキューの信号を発生させます。設定する引数によってキューに送られるイベントのタイプを決定します。

設定されたイベントは一度限りのものではなく、音楽の再生が終了するたびに毎回発生します。イベントの発生を解除するためには、引数を設定せずにこの命令を実行します。

have the music send an event when playback stops
pygame.mixer.music.set_endevent(): return Nonepygame.mixer.music.set_endevent(type): return None

This causes Pygame to signal (by means of the event queue) when the music is done playing. The argument determines the type of event that will be queued.

The event will be queued every time the music finishes, not just the first time. To stop the event from being queued, call this method with no argument.


pygame.mixer.music.get_endevent

音楽の再生が終了した時に、発生するイベントを取得します。
pygame.mixer.music.get_endevent(): return type

音楽の再生が終了した時に発生するイベントの種類を戻り値として返します。終了イベントが設定されていない場合、pygame.NOEVENTの値が戻り値として返されます。

get the event a channel sends when playback stops
pygame.mixer.music.get_endevent(): return type

Returns the event type to be sent every time the music finishes playback. If there is no endevent the function returns pygame.NOEVENT.