Created
June 11, 2021 20:41
-
-
Save akshayaurora/615a0189d56265da3a8ea46f8b410760 to your computer and use it in GitHub Desktop.
Hacky code to experiment with opening media on iOS , copy paste code below `plyer/platforms/ios/filechooser`
This file contains hidden or 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
''' | |
IOS file chooser | |
-------------------- | |
This module houses the iOS implementation of the plyer FileChooser. | |
.. versionadded:: 1.4.4 | |
''' | |
from plyer.facades import FileChooser | |
from pyobjus import autoclass, protocol | |
from pyobjus.dylib_manager import load_framework | |
import os | |
load_framework('/System/Library/Frameworks/MediaPlayer.framework') | |
AVURLAsset = autoclass('AVURLAsset') | |
AVAssetExportSession = autoclass('AVAssetExportSession') | |
NSURL = autoclass('NSURL') | |
class IOSFileChooser(FileChooser): | |
''' | |
FileChooser implementation for IOS using | |
the built-in file browser via UIImagePickerController. | |
.. versionadded:: 1.4.0 | |
''' | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self._on_selection = None | |
def _file_selection_dialog(self, *args, **kwargs): | |
""" | |
Function called when action is required, A "mode" parameter specifies | |
which and is one of "open", "save" or "dir". | |
""" | |
self._on_selection = kwargs["on_selection"] | |
if kwargs["mode"] == "open": | |
self._open() | |
else: | |
raise NotImplementedError() | |
def _get_picker(self): | |
""" | |
Return an instantiated and configured UIImagePickerController. | |
""" | |
picker = autoclass("MPMediaPickerController") | |
po = picker.alloc().init(4) | |
po.allowsPickingMultipleItems = True | |
po.showsCloudItems = False | |
po.showsItemsWithProtectedAssets = False | |
po.delegate = self | |
return po | |
def _open(self): | |
""" | |
Launch the native iOS file browser. Upon selection, the | |
`imagePickerController_didFinishPickingMediaWithInfo_` delegate is | |
called where we close the file browser and handle the result. | |
""" | |
picker = self._get_picker() | |
UIApplication = autoclass('UIApplication') | |
vc = UIApplication.sharedApplication().keyWindow.rootViewController() | |
vc.presentViewController_animated_completion_(picker, True, None) | |
@protocol('MPMediaPickerControllerDelegate') | |
def mediaPicker_didPickMediaItems_( | |
self, media_picker, mpmedia_item_collection): | |
""" | |
Delegate which handles the result of the image selection process. | |
""" | |
items = mpmedia_item_collection.items | |
urls = [] | |
for i in range(items.count()): | |
item = items.objectAtIndex_(i) | |
asseturl = item.assetURL() | |
url, persistentid = str(asseturl.absoluteString.cString()).split('?id=') | |
title = item.title().cString() | |
artist = item.artist().cString() | |
duration = item.playbackDuration() | |
print(f'{url}, {persistentid}, {title}, {artist}, {duration}') | |
asset = AVURLAsset.URLAssetWithURL_options_(asseturl, None) | |
exporter = AVAssetExportSession.exportSessionWithAsset_presetName_(asset, 'AVAssetExportPresetAppleM4A') | |
# print(dir(AVAssetExportSession)) | |
exporter.outputFileType = "com.apple.m4a-audio" | |
from kivy.app import App | |
app = App.get_running_app() | |
exportURLPath = f'{app.user_data_dir}/{title}.m4a' | |
exportURL = NSURL.fileURLWithPath_(exportURLPath) | |
exporter.outputURL = exportURL | |
if os.path.exists(exportURLPath): | |
os.remove(exportURLPath) | |
def doufus(*args): | |
pass | |
exporter.exportAsynchronouslyWithCompletionHandler_(doufus) | |
print('has_protected: ', item.hasProtectedAsset()) | |
print('librarylink: ', item._libraryLinkURL().absoluteString.cString()) | |
# urls.append(url) | |
# this is just to wait while the files selected are saved to app.user_data_dir | |
import time | |
while not os.path.exists(exportURLPath): | |
time.sleep(1) | |
print(exporter.status) | |
print(os.path.exists(exportURLPath), 'exists') | |
urls.append(exportURLPath) | |
media_picker.dismissViewControllerAnimated_completion_(True, None) | |
path = '.' | |
self._on_selection(urls) | |
def instance(): | |
return IOSFileChooser() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment