-1

I try to make mp3 mixer program for my school project. I thought that we can find new song with mixing two songs

I have trouble about play putton. Whenever I click it, error occurs

import sys,os
from PyQt5.QtWidgets import QWidget,QApplication,QPushButton,QVBoxLayout,QFileDialog,QHBoxLayout
from pygame import mixer
class Window(QWidget):
    def __init__(self):

        super().__init__()

        self.init_ui()
    def init_ui(self):
        self.song1 = QPushButton("song1")
        self.song2 = QPushButton("song2")
        self.play_it = QPushButton("Play")
        h_box = QHBoxLayout()
        h_box.addWidget(self.song1)
        h_box.addWidget(self.song2)
        h_box.addWidget(self.play_it)
        v_box = QVBoxLayout()
        v_box.addLayout(h_box)
        self.setLayout(v_box)
        self.setWindowTitle("Song Mixer 1.0")
        self.song1.clicked.connect(self.song1_open)
        self.play_it.clicked.connect(self.play_the_songs)
        self.show()
    def song2_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data2 = file_name[0]

    def song1_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data1 = file_name[0]

    def play_the_songs(self):
        mixer.init()

        s1 =mixer.Sound(self.data1)
        s2 =mixer.Sound(self.data2)
        s1.play()

app = QApplication(sys.argv)

pencere = Window()


sys.exit(app.exec_())

I'm gratefull for your help!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Is the error secret? If not, why not copy & paste it into the question? – Patrick Artner Mar 17 '18 at 22:15
  • 1
    @PatrickArtner it is not responding like a windows error – Real Slim Shady 112 Mar 17 '18 at 22:50
  • For those cases, encapsulate your code like this: `Try: your code Except Exception as e: print(e)` (snippet 2 at https://stackoverflow.com/questions/18982610/difference-between-except-and-except-exception-as-e-in-python) It will print you what fails. – Saelyth Mar 19 '18 at 08:37

1 Answers1

0

Try it:

!!! The mixer also has a special streaming channel. This is for music playback and is accessed through the pygame.mixer.musicpygame module for controlling streamed audio module.

import sys,os
from PyQt5.QtWidgets import (QWidget,QApplication,QPushButton,
                             QVBoxLayout,QFileDialog,QHBoxLayout)
#from pygame import mixer                                         # ---
import pygame                                                     # +++
pygame.init()                                                     # +++

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.playsound = None                                     # +++
        self.pause     = None                                     # +++

        self.init_ui()

    def init_ui(self):
        self.song1 = QPushButton("LoadingSound")
        self.pause = QPushButton("Pause")
        self.play_it = QPushButton("Play")
        h_box = QHBoxLayout()
        h_box.addWidget(self.song1)
        h_box.addWidget(self.play_it)
        h_box.addWidget(self.pause)
        v_box = QVBoxLayout()
        v_box.addLayout(h_box)
        self.setLayout(v_box)
        self.setWindowTitle("Song Mixer 1.0")

        self.song1.clicked.connect(self.song1_open)
        self.pause.clicked.connect(self.pause_the_songs)            
        self.play_it.clicked.connect(self.play_the_songs)

        self.show()

    def pause_the_songs(self):
        if self.playsound is None:
            self.pause.setText("UnPause")
            self.playsound = "pause"
            pygame.mixer.music.pause()
        else:
            self.pause.setText("Pause")
            self.playsound = None  
            pygame.mixer.music.unpause()            

    def song1_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data1 = file_name[0]

    def play_the_songs(self):                                     # +++
        self.playsound = pygame.mixer.init()        
        pygame.mixer.music.load(self.data1)
        pygame.mixer.music.play()


app = QApplication(sys.argv)
pencere = Window()
sys.exit(app.exec_())
S. Nick
  • 12,879
  • 8
  • 25
  • 33