Created
June 21, 2020 09:07
-
-
Save jsbain/43ae4d9e8cbfb4a09601f477f3a1e135 to your computer and use it in GitHub Desktop.
simple_recorder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sound,ui | |
from sound import Recorder | |
from sound import Player | |
sound.set_volume(1) | |
filename='4.m4a' #could have this come from a textfield in the ui... | |
play_image=ui.Image('iob:play_256') | |
stop_image=ui.Image('iob:stop_256') | |
record_image=ui.Image('iob:ios7_circle_filled_256') | |
class RecorderView(ui.View): | |
def __init__(self, *args,**kwargs): | |
super().__init__(*args, **kwargs) | |
'''create two buttons, and assign actions. if using ui designer, you would use load_view instead''' | |
self.recordButton=ui.Button(frame=(0,0,256,256),image=record_image, tint_color='#ff3333') | |
self.recordButton.action=self.record | |
self.playButton=ui.Button(frame=(0,256,256,256),image=play_image, tint_color='#3345ff') | |
self.playButton.action=self.play | |
self.playButton.enabled=False | |
self.statuslabel=ui.Label(frame=(0,512,300,40)) | |
self.statuslabel.text='Nothing recorded...' | |
self.add_subview(self.recordButton) | |
self.add_subview(self.playButton) | |
self.add_subview(self.statuslabel) | |
self.recorder=None | |
self.player=None | |
def record(self,sender): | |
'''record action. if recorder is refording, we stop. otherwise, we start recording''' | |
if self.recorder and self.recorder.recording: | |
self.recorder.stop() | |
sender.image=record_image | |
self.recorder=None | |
self.playButton.enabled=True | |
'''we can create the player now, just dont play it yet. this is slightly more efficient if pressing play multiple times. also, create the finish handler, to reset everything once the player is done playing''' | |
self.player=Player(filename) | |
self.player.finished_handler=self.play_finished | |
self.update_interval=0.0 #stop updates | |
self.statuslabel.text='Current recording: {:0.2f} seconds'.format(self.player.duration) | |
else: | |
sender.image=stop_image | |
self.recorder = Recorder(filename) | |
self.playButton.enabled=False #so you can't press play while recording... | |
self.recorder.record() | |
self.update_interval=0.1 #start updates | |
def play_finished(self): | |
'''resets play button, when player is done, or stop pressed''' | |
self.player.stop() | |
self.playButton.image=play_image | |
self.recordButton.enabled=True | |
self.update_interval=0 #stop updates | |
self.statuslabel.text='Current recording: {:0.2f} seconds'.format(self.player.duration) | |
def play(self,sender): | |
'''button handler for play/play stop button. | |
if player is playing, then we stop the player. otherwise, play''' | |
if self.player and self.player.playing: | |
self.play_finished() | |
else: | |
''' play: set the image to stop, and disable recorder button, then fire the player''' | |
sender.image=stop_image | |
self.recordButton.enabled=False | |
self.player.play() | |
self.update_interval=0.1 #start updates | |
def update(self): | |
''' show current recording / play time | |
Exercise for reader: add a graphic display of recorder.meters, by implementing a draw method. | |
''' | |
if self.player and self.player.playing: | |
self.statuslabel.text='Playing: {:0.2f} of {:0.2f} sec'.format(self.player.current_time, self.player.duration) | |
elif self.recorder and self.recorder.recording: | |
self.statuslabel.text='Recording: {:0.2f} sec'.format(self.recorder.current_time) | |
elif self.player and not self.player.playing: | |
self.statuslabel.text='Current recording: {:0.2f} seconds'.format(self.player.duration) | |
else: | |
self.statuslabel.text='I should not be here...' | |
v=RecorderView(frame=(0,0,256,600), bg_color='white') | |
v.present('sheet') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment