Only with the purpose of learn, I made this script that allows you listen to a music while working in Blender. Cool

import bpy
import aud
class ManoMusicPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "Mano-Addons"
bl_label = "Music"
def draw(self, context):
layout = self.layout
row = layout.row()
col = layout.column()
col.prop(context.scene, 'conf_path')
TheCol = layout.column(align = True)
TheCol.operator("sound.mano_play_music", icon="PLAY")
TheCol.operator("sound.mano_pause_music", icon="PAUSE")
class ManoMusicPlayOperator(bpy.types.Operator):
bl_idname = "sound.mano_play_music"
bl_label = "Play"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
device = aud.device()
path = context.scene.conf_path
# load sound file (it can be a video file with audio)
factory = aud.Factory(path)
global handle
# play the audio, this return a handle to control play/pause
handle = device.play(factory)
return {'FINISHED'}
class ManoMusicPauseOperator(bpy.types.Operator):
bl_idname = "sound.mano_pause_music"
bl_label = "Pause"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# stop the sounds (otherwise they play until their ends)
#handle.pause() #Does not work!!!!!!!!!
handle.stop()
return {'FINISHED'}
def register():
bpy.utils.register_class(ManoMusicPanel)
bpy.utils.register_class(ManoMusicPlayOperator)
bpy.utils.register_class(ManoMusicPauseOperator)
bpy.types.Scene.conf_path = bpy.props.StringProperty \
(
name = "Root Path",
default = "*.mp3",
description = "Define the root path of the project",
subtype = 'FILE_PATH'
)
def unregister():
bpy.utils.unregister_class(ManoMusicPanel)
bpy.utils.unregister_class(ManoMusicPlayOperator)
bpy.utils.unregister_class(ManoMusicPauseOperator)
del bpy.types.Scene.conf_path
if __name__ == "__main__":
register()
So,
How to pause? I only know how to stop.
I also didn’t like having used globals in this case.