Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added iOS audio recording implementation #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions plyer/platforms/ios/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from plyer.facades import Audio
from pyobjus.dylib_manager import load_framework, INCLUDE
from pyobjus import autoclass
from os.path import join
from plyer.platforms.ios.storagepath import iOSStoragePath

load_framework(INCLUDE.Foundation)
load_framework(INCLUDE.AVFoundation)

AVAudioPlayer = autoclass("AVAudioPlayer")
AVAudioRecorder = autoclass("AVAudioRecorder")
AVAudioFormat = autoclass("AVAudioFormat")
NSString = autoclass('NSString')
NSURL = autoclass('NSURL')
NSError = autoclass('NSError').alloc()


class iOSAudio(Audio):
def __init__(self, file_path=None):
default_path = join(iOSStoragePath().get_music_dir(), 'audio.wav')
super(iOSAudio, self).__init__(file_path or default_path)

self._recorder = None
self._player = None
self._current_file = None

def _start(self):
# Conversion of Python file path string to Objective-C NSString
file_path_NSString = NSString.alloc()
file_path_NSString = file_path_NSString.initWithUTF8String_(
self._file_path
)

# Definition of Objective-C NSURL object for the output record file
# specified by NSString file path
file_NSURL = NSURL.alloc()
file_NSURL = file_NSURL.initWithString_(file_path_NSString)

# Internal audio file format specification
af = AVAudioFormat.alloc()
af = af.initWithCommonFormat_sampleRate_channels_interleaved_(
1, 44100.0, 2, True
)

# Audio recorder instance initialization with specified file NSURL
# and audio file format
self._recorder = AVAudioRecorder.alloc()
self._recorder = self._recorder.initWithURL_format_error_(
file_NSURL, af, NSError
)

if not self._recorder:
raise Exception(NSError.code, NSError.domain)

self._recorder.record()

# Setting the currently recorded file as current file
# for using it as a parameter in audio player
self._current_file = file_NSURL

def _stop(self):
if self._recorder:
self._recorder.stop()
self._recorder = None

if self._player:
self._player.stop()
self._player = None

def _play(self):
# Audio player instance initialization with the file NSURL
# of the last recorded audio file
self._player = AVAudioPlayer.alloc()
self._player = self._player.initWithContentsOfURL_error_(
self._current_file, NSError
)

if not self._player:
raise Exception(NSError.code, NSError.domain)

self._player.play()


def instance():
return iOSAudio()