How to use _playback method in robotframework-ioslibrary

Best Python code snippet using robotframework-ioslibrary_python

video_player.py

Source:video_player.py Github

copy

Full Screen

1"""A video player class."""2import random3from .video_library import VideoLibrary, VideoLibraryError4from . import video_playlist_library5from .video import FlagError6from .video_playlist import VideoPlaylistError7from .video_playlist_library import VideoPlaylistLibraryError8from .video_playback import VideoPlayback, VideoPlaybackError, PlaybackState9class VideoPlayerError(Exception):10 pass11def _print_video_choice_list(videos):12 for i, video in enumerate(videos, start=1):13 print(f" {i}) {video})")14 print("Would you like to play any of the above? If yes, specify the number of the video.")15 print("If your answer is not a valid number, we will assume it's a no.")16 user_input = input("")17 try:18 num = int(user_input)19 except ValueError:20 num = 021 if 1 <= num <= len(videos):22 return videos[num - 1]23 else:24 return None25class VideoPlayer:26 """A class used to represent a Video Player."""27 def __init__(self):28 """The VideoPlayer class is initialized."""29 self._videos = VideoLibrary()30 self._playlists = video_playlist_library.VideoPlaylistLibrary()31 self._playback = VideoPlayback()32 def number_of_videos(self):33 num_videos = len(self._videos.get_all_videos())34 print(f"{num_videos} videos in the library")35 def show_all_videos(self):36 """Returns all videos."""37 print("Here's a list of all available videos:")38 for v in self._videos.get_all_videos():39 print(v)40 def play_video(self, video_id):41 """Plays the respective video.42 Args:43 video_id: The video_id to be played.44 """45 try:46 video = self._videos[video_id]47 video.check_allowed()48 except (VideoLibraryError, FlagError) as e:49 print(f"Cannot play video: {e}")50 return51 if self._playback.state != PlaybackState.STOPPED:52 self.stop_video()53 self._playback.play(video)54 print(f"Playing video: {video.title}")55 def stop_video(self):56 """Stops the current video."""57 try:58 video = self._playback.get_video()59 print(f"Stopping video: {video.title}")60 self._playback.stop()61 except VideoPlaybackError as e:62 print(f"Cannot stop video: {e}")63 def play_random_video(self):64 """Plays a random video from the video library."""65 random_video_id = self._videos.get_random_video_id()66 if random_video_id is None:67 print("No videos available")68 else:69 self.play_video(random_video_id)70 def pause_video(self):71 """Pauses the current video."""72 try:73 video = self._playback.get_video()74 except VideoPlaybackError as e:75 print(f"Cannot pause video: {e}")76 return77 if self._playback.state == PlaybackState.PAUSED:78 print(f"Video already paused: {video.title}")79 return80 print(f"Pausing video: {video.title}")81 self._playback.pause()82 def continue_video(self):83 """Resumes playing the current video."""84 try:85 video = self._playback.get_video()86 self._playback.resume()87 print(f"Continuing video: {video.title}")88 except VideoPlaybackError as e:89 print(f"Cannot continue video: {e}")90 def show_playing(self):91 """Displays video currently playing."""92 if self._playback.state == PlaybackState.PLAYING:93 print(f"Currently playing: {self._playback.get_video()}")94 elif self._playback.state == PlaybackState.PAUSED:95 print(f"Currently playing: {self._playback.get_video()} - PAUSED")96 else:97 print("No video is currently playing")98 def create_playlist(self, playlist_name):99 """Creates a playlist with a given name.100 Args:101 playlist_name: The playlist name.102 """103 try:104 self._playlists.create(playlist_name)105 print(f"Successfully created new playlist: {playlist_name}")106 except VideoPlaylistLibraryError as e:107 print(f"Cannot create playlist: {e}") 108 def add_to_playlist(self, playlist_name, video_id):109 """Adds a video to a playlist with a given name.110 Args:111 playlist_name: The playlist name.112 video_id: The video_id to be added.113 """114 try:115 playlist = self._playlists[playlist_name]116 video = self._videos[video_id]117 video.check_allowed()118 playlist.add_video(video)119 print(f"Added video to {playlist_name}: {video.title}")120 except (VideoPlaylistLibraryError, VideoPlaylistError, VideoLibraryError, FlagError) as e:121 print(f"Cannot add video to {playlist_name}: {e}")122 def show_all_playlists(self):123 """Display all playlists."""124 playlists = list(self._playlists.get_all())125 if not playlists:126 print("No playlists exist yet")127 return128 print("Showing all playlists:")129 for playlist in playlists:130 print(f" {playlist}")131 def show_playlist(self, playlist_name):132 """Display all videos in a playlist with a given name.133 Args:134 playlist_name: The playlist name.135 """136 try: 137 playlist = self._playlists[playlist_name]138 except VideoPlaylistLibraryError as e:139 print(f"Cannot show playlist {playlist_name}: {e}")140 return141 print(f"Showing playlist: {playlist_name}")142 if not playlist.videos:143 print("No videos here yet")144 return145 for video in playlist.videos:146 print(f" {video}")147 def remove_from_playlist(self, playlist_name, video_id):148 """Removes a video to a playlist with a given name.149 Args:150 playlist_name: The playlist name.151 video_id: The video_id to be removed.152 """153 try:154 playlist = self._playlists[playlist_name]155 video = self._videos[video_id]156 playlist.remove_video(video)157 print(f"Removed video from {playlist_name}: {video.title}")158 except (VideoPlaylistError, VideoLibraryError, VideoPlaylistLibraryError) as e:159 print(f"Cannot remove video from {playlist_name}: {e}")160 def clear_playlist(self, playlist_name):161 """Removes all videos from a playlist with a given name.162 Args:163 playlist_name: The playlist name.164 """165 try: 166 playlist = self._playlists[playlist_name]167 playlist.clear()168 print(f"Successfully removed all videos from {playlist_name}")169 except (VideoPlaylistError, VideoPlaylistLibraryError) as e:170 print(f"Cannot clear playlist {playlist_name}: {e}")171 def delete_playlist(self, playlist_name):172 """Deletes a playlist with a given name.173 Args:174 playlist_name: The playlist name.175 """176 try: 177 playlist = self._playlists[playlist_name]178 del self._playlists[playlist_name]179 print(f"Deleted playlist: {playlist_name}")180 except VideoPlaylistLibraryError as e:181 print(f"Cannot delete playlist {playlist_name}: {e}")182 def search_videos(self, search_term):183 """Display all the videos whose titles contain the search_term.184 Args:185 search_term: The query to be used in search.186 """187 188 results = self._videos.search_videos(search_term)189 if not results:190 print(f"No search results for {search_term}")191 return192 print(f"Here are the results for {search_term}:")193 chosen_video = _print_video_choice_list(results)194 if chosen_video is not None:195 self.play_video(chosen_video.video_id)196 def search_videos_tag(self, video_tag):197 """Display all videos whose tags contains the provided tag.198 Args:199 video_tag: The video tag to be used in search.200 """201 results = self._videos.get_videos_with_tag(video_tag)202 if not results:203 print(f"No search results for {video_tag}")204 return205 print(f"Here are the results for {video_tag}:")206 chosen_video = _print_video_choice_list(results)207 if chosen_video is not None:208 self.play_video(chosen_video.video_id)209 def flag_video(self, video_id, flag_reason=""):210 """Mark a video as flagged.211 Args:212 video_id: The video_id to be flagged.213 flag_reason: Reason for flagging the video.214 """215 if not flag_reason:216 flag_reason = "Not supplied"217 try:218 video = self._videos[video_id]219 if self._playback.state != PlaybackState.STOPPED and self._playback.get_video() == video:220 self.stop_video()221 video.flag(flag_reason)222 print(f"Successfully flagged video: {video.title} {video.formatted_flag_reason}")223 except (VideoPlayerError, FlagError, VideoLibraryError) as e:224 print(f"Cannot flag video: {e}")225 def allow_video(self, video_id):226 """Removes a flag from a video.227 Args:228 video_id: The video_id to be allowed again.229 """230 try:231 video = self._videos[video_id]232 video.unflag()233 print(f"Successfully removed flag from video: {video.title}")234 except (VideoPlayerError, FlagError, VideoLibraryError) as e:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run robotframework-ioslibrary automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful