G
G
gevrog2021-04-09 19:17:35
Python
gevrog, 2021-04-09 19:17:35

How to play current audio?

Hello. My problem is that I can't play sound while capturing video data from the screen.

Screen capture:

import numpy as np
import cv2
from mss import mss
from PIL import Image
                                           
bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}

sct = mss()

while True:
    sct_img = sct.grab(bounding_box)
    cv2.imshow('screen', np.array(sct_img))

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexa2007, 2021-04-09
@gevrog

import numpy as np
import cv2
from mss import mss
from PIL import Image
import threading
from pygame import mixer
                                


class MyMss():
    def __init__(self,mss,mixer):
        super(MyMss,self).__init__()
        self.bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}
        self.mss = mss
        self.mixer = mixer

    def _grab(self):
        while True:
            sct_img = self.mss.grab(self.bounding_box)
            cv2.imshow('screen', np.array(sct_img))

            if (cv2.waitKey(1) & 0xFF) == ord('q'):
                cv2.destroyAllWindows()
                break

    def start_grab(self):
        thread = threading.Thread(target=self._grab)
        thread.start()

    def _music(self):
        self.mixer.init()
        self.mixer.music.load('fon.mp3')
        self.mixer.music.play()

    def play_music(self):
        m_th = threading.Thread(target=self._music)
        m_th.start()


sct = MyMss(mss(),mixer)
sct.start_grab()
sct.play_music()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question