How can I play a video with kivy v2.0.0? - python

I want to play a video with Kivy 2.0.0 and Python 3.6, but running this code will give me a load error because Kivy's video player won't recognize the file. How can I get Kivy's video player to recognize the file?
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.videoplayer import VideoPlayer
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.lang.builder import *
from kivy.app import App
import kivy
import os
kivy.require('2.0.0')
# Main page code
class MainPage(Screen):
""" This is the code for the Main Screen """
# Initialization
def __init__(self, **var_args):
# Constructor for the Main Page class
super(MainPage, self).__init__(**var_args)
# Layout
self.layout = FloatLayout()
# Video player
self.filename = '/'
self.player = VideoPlayer(source=self.filename,state='play',options={'allow_stretch': True})
self.layout.add_widget(self.player)
# Add the layout to the screen
self.add_widget(self.layout)
# Main app class
class Video_Viewer(App):
""" Main build """
# Main build
def build(self, **kwargs):
# Create the screen manager
self.manager = ScreenManager()
# Add screens to screen manager
self.manager.add_widget(MainPage(name='Main'))
# Set the screen to the main sceren
self.manager.current = 'Main'
# Return the current screen of the screen manager
return self.manager
# Run the file
if __name__ == '__main__':
Video_Viewer().run()
This is the error that happens as a result of the code:
[ERROR ] [Image ] Error loading <C:/Users/My Laptop/Downloads/video.mp4>

Try to install ffpyplayer
python -m pip install ffpyplayer

Related

kivy app shows black screen on android phone

The kivy app that I have coded up won't work on my samsung galaxy s10e but will work on my computer. Maybe I need to add somthing to my .spec file but I dont know
Here is the code for the app:
import kivy
kivy.require('2.1.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import random
class MyRoot(BoxLayout):
def __init__(self):
super(MyRoot, self).__init__()
def generate_number(self):
self.random_label.text = str(random.randint(0, 1000))
class randomnumber(App):
def build(self):
return MyRoot()
randomint = randomnumber()
randomint.run()

Kivy(framework): blank window after running code

After running this code python idle gave me a blank window without any kivy widgets and python idle does not show any error.
what is the problem in this code?
screen shot of blank window
code:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
m=me('b','g','l','t')
m.run()
Building the App should be done in a build() method of the App, and that method should return the root widget for the App. Like this:
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
def build(self):
return self.g

How to access android's camera using opencv and kivy?

I have been trying to make an android apk in kivy to access mobile camera through opencv, but my application crashes in my phone. It is probably not able to access my phone's camera!
import imp
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton
from kivy.uix.image import Image
from kivy.clock import Clock
import cv2
from kivy.graphics.texture import Texture
class MainApp(MDApp):
def build(self):
layout = MDBoxLayout(orientation='vertical')
self.image = Image()
layout.add_widget(self.image)
layout.add_widget(MDRaisedButton(
text='Click Here',
pos_hint={'center_x':.5,'center_y':.5},
size_hint=(None,None))
)
self.capture=cv2.VideoCapture(0)
Clock.schedule_interval(self.load_video, 1.0/30.0)
return layout
def load_video(self, *args):
ret, frame= self.capture.read()
self.frame=frame
buffer=cv2.flip(frame, 0).tostring()
texture=Texture.create(size=(frame.shape[1],frame.shape[0]),colorfmt='bgr')
texture.blit_buffer(buffer, colorfmt='bgr',bufferfmt='ubyte')
self.image.texture=texture
if __name__ == '__main__':
MainApp().run()
Also the changes I have made in my buildozer.spec folder are as follows:

How to change screen by swiping in kivy python

I am trying to change to another screen by swiping the screen. I've tried carousel but it seems that it only works with images, so I've tried detecting a swipe motion and changing the screen after it has been detected.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class HomeScreen(Screen):
def on_touch_move(self, touch):
if touch.x < touch.ox: # this line checks if a left swipe has been detected
MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen
class ImageButton(ButtonBehavior, Image):
pass
class LabelButton(ButtonBehavior, Label):
pass
class SettingsScreen(Screen):
pass
class SwipeDhikrScreen(Screen):
pass
#def quit_verification():
# pop = Popup(title="verification", content=Label(text= "Are you sure?"))
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
# get the screen manager from the kv file
screen_manager = self.root.ids["screen_manager"]
screen_manager.transition.direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()
MainApp().run()
I got an attribute error: "None type object has no attribute 'ids'"
MainApp().change_screen(screen_name="swipedhikr_screen")
This line creates a new instance of MainApp, which doesn't have any widgets and therefore naturally fails when you try to access them.
Use the existing instance of MainApp, i.e. the one that you're actually running, via MainApp.get_running_app().
Also you are not correct that Carousel works only with images.

FactoryException when trying to create a "IconButton" in Kivy

I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.

Categories

Resources