import kivy from kivy.lang import Builder from kivymd.app import MDApp from kivy.uix.screenmanager import ScreenManager from kivy.factory import Factory from kivy.properties import NumericProperty,ObjectProperty,StringProperty from kivymd.uix.list import OneLineIconListItem from kivymd.uix.boxlayout import MDBoxLayout from kivy.core.audio import SoundLoader from kivymd.toast import toast from kivy.core.window import Window from kivy.clock import Clock from PIL import Image import io,time, threading, os, random import gc from os.path import abspath from tinytag import TinyTag from os import getcwd try: from android.storage import app_storage_path,primary_external_storage_path, secondary_external_storage_path from android.permissions import request_permissions, check_permission, Permission except: pass from functools import partial Builder.load_string(""" #:include kv/main.kv #: import utils kivy.utils """) class CustomOneLineIconListItem(OneLineIconListItem): icon = StringProperty() class SongCover(MDBoxLayout): pass class MainApp(MDApp): principal = ObjectProperty() title = StringProperty() artist = StringProperty() pause_play = StringProperty() time_info = StringProperty() repeat = StringProperty() sufle = StringProperty() slide =NumericProperty() val = NumericProperty() image = StringProperty() def __init__(self, **kwargs): self.sm = ScreenManager() self.song = 0 self.liste = [] gc.collect() gc.enable() try: request_permissions([Permission.WRITE_EXTERNAL_STORAGE],self.permission_callback) except: pass self.principal = SongCover() self.loop = None self.sufle = 'shuffle' self.pause_play = 'play-outline' try: self.song_info(self.liste[self.song]) except: pass self.state = 'pause' self.logic = None self.repeat = 'repeat' self.image = 'logo.png' super().__init__(**kwargs) def permission_callback(self,permissions,grants): if check_permission(Permission.WRITE_EXTERNAL_STORAGE): print("Permission Granted") else: print("Permission Denied") return doc = join(primary_external_storage_path(),'./') for root, dirs, files in os.walk(doc, topdown=False): for name in files: path = os.path.join(root, name) if os.path.isfile(path): if ((str(path.split('.')[-1]) == 'mp3') or (str(path.split('.')[-1]) == 'amr')): self.liste.append(path) def pause_or_play(self): if self.state =='pause': self.sound.play() self.pause_play = 'pause' self.cancel self.state = 'play' else: self.sound.stop() self.pause_play = 'play-outline' self.cancel.cancel() self.state = 'pause' def next(self): if self.song <= len(self.liste): self.sound.unload() self.song += 1 self.song_info(self.liste[self.song]) self.sound.play() def prev(self): if self.song != 0: self.sound.unload() self.song -= 1 self.song_info(self.liste[self.song]) self.sound.play() def song_info(self, source): self.sound=SoundLoader.load(source) self.sound.bind(on_play=self.times) self.time_info = '00:00 / 00:00' self.slide = int(self.sound.length) self.val = 0 tag = TinyTag.get(source, image=True) if tag.get_image() != None: image_data = tag.get_image() image = Image.open(io.BytesIO(image_data)) image.save("artwork.png", format="png") self.image = 'artwork.png' else: self.image = 'logo.png' if tag.title != None and tag.artist != None: self.artist = tag.artist self.title = tag.title def change_screen(self, screen_name): if self.sm.has_screen(screen_name): self.sm.current = screen_name else: print("Screen [" + screen_name + "] does not exist.") def manage_screens(self, screen_name, action): scns = { "music_screen": Factory.MusicScreen, "list_screen": Factory.ListScreen, } try: if action == "remove": if self.sm.has_screen(screen_name): self.sm.remove_widget(self.sm.get_screen(screen_name)) elif action == "add": if self.sm.has_screen(screen_name): print("Screen [" + screen_name + "] already exists") else: self.sm.add_widget(scns[screen_name](name=screen_name)) print(screen_name + " added") except Exception as e: print(e) def seek(self, widget): self.sound.seek(int(widget.value)) self.sound.play() self.pause_play = 'pause' self.cancel self.state = 'play' def dataM(self,widget,text,search=False): def add_icon_item(name1,action): widget.data.append( { "viewclass": "CustomOneLineIconListItem", "icon": "account", "text": name1, "callback": lambda x: x, "on_press": action } ) def println(data): self.sound.unload() self.song_info(data) self.song = self.liste.index(data) self.sound.play() self.pause_play = 'pause' self.cancel self.state = 'play' widget.data = [] for row in self.liste: if search: if text in row: add_icon_item(os.path.basename(row).lower(),partial(println,row)) else: add_icon_item(os.path.basename(row).lower(),partial(println,row)) def screen_manager(self,widget,add): self.manage_screens(widget, add) self.change_screen(widget) def times(self,dt): self.val = int(self.sound.get_pos()) self.time_info = self.coverting(self.sound.get_pos())+'/'+self.coverting(self.sound.length) if self.sound.state != 'play' and self.state != 'pause': if self.loop != None: self.val = 0 self.pause_play = 'play-outline' self.state = 'pause' self.cancel.cancel() self.song_info(self.liste[self.song]) self.pause_or_play() elif self.sufle != 'shuffle': self.val = 0 self.pause_play = 'play-outline' self.state = 'pause' self.cancel.cancel() song = random.choice(self.liste) self.song = self.liste.index(song) self.song_info(song) self.pause_or_play() elif self.repeat != 'repeat-off': self.val = 0 self.pause_play = 'play-outline' self.state = 'pause' self.cancel.cancel() try: self.song += 1 except: self.song = 0 self.song_info(self.liste[self.song]) self.pause_or_play() self.cancel = Clock.schedule_once(self.times, 1.0) def loop_play(self): if self.loop == None: toast("Loop is activated") self.loop = 'loop' self.sufle = 'shuffle-disabled' self.repeat = 'repeat-off' else: toast("Loop is deactivated") self.loop = None self.sufle = 'shuffle' self.repeat = 'repeat' def suffle(self): if self.sufle == 'shuffle': self.sufle = 'shuffle-disabled' else: self.sufle = 'shuffle' def repeat_all(self): if self.repeat == 'repeat': self.repeat = 'repeat-off' else: self.repeat = 'repeat' def coverting(self,seconds): seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 if seconds >=3600: return "%d:%02d:%02d" % (hour, minutes, seconds) else: return "%02d:%02d" % (minutes, seconds) def build(self): self.sm.add_widget(Factory.MusicScreen(name="music_screen")) return self.sm if __name__ == "__main__": MainApp().run()
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |