Exemplo n.º 1
0
class MainWindow(QObject):

    #class constructor
    def __init__(self, ui_file, parent=None):

        #reference to our music player
        self.music_player = QMediaPlayer()
        self.music_player.setVolume(50)

        #call parent QObject constructor
        super(MainWindow, self).__init__(parent)

        #load the UI file into Python
        ui_file = QFile(ui_file)
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()
        self.window = loader.load(ui_file)

        #always remember to close files
        ui_file.close()

        #add playlist object
        self.playlist = QMediaPlaylist()
        self.playlist.setPlaybackMode(QMediaPlaylist.Sequential)
        self.music_player.setPlaylist(self.playlist)
        self.music_player.setNotifyInterval(50)

        #add a listener to change audio filename displayed

        #add playlist display object
        self.playlistDisplay = self.window.findChild(QListWidget,
                                                     'PlayListWidget')
        #self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemOnce)
        #self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
        #self.playlist.setPlaybackMode(QMediaPlaylist.Random)
        #self.playlist.setPlaybackMode(QMediaPlaylist.Loop)

        #add event listeners
        self.add_media_action = self.window.findChild(QAction,
                                                      'action_add_media')
        self.add_media_action.triggered.connect(self.add_media_triggered)

        self.quit_action = self.window.findChild(QAction, 'action_quit')
        self.quit_action.triggered.connect(self.quit_action_triggered)

        self.NextButton = self.window.findChild(QPushButton, 'NextButton')
        self.NextButton.clicked.connect(self.next_button_clicked)

        self.PauseButton = self.window.findChild(QPushButton, 'PauseButton')
        self.PauseButton.clicked.connect(self.pause_button_clicked)

        #PlayAllRadioButton = self.window.findChild(QRadioButton, 'PlayAllRadioButton')
        #PlayAllRadioButton.clicked.connect(self.play_all_button_clicked)

        self.PlayButton = self.window.findChild(QPushButton, 'PlayButton')
        self.PlayButton.clicked.connect(self.play_button_clicked)

        self.PreviousButton = self.window.findChild(QPushButton,
                                                    'PreviousButton')
        self.PreviousButton.clicked.connect(self.previous_button_clicked)

        #ProgressBar = self.window.findChild(QProgressBar, 'ProgressBar')
        #ProgressBar.valueChanged.connect(self.progress_bar_moved)

        self.RepeatOnceRadioButton = self.window.findChild(
            QRadioButton, 'RepeatOnceRadioButton')
        self.RepeatOnceRadioButton.clicked.connect(
            self.repeat_once_button_clicked)

        self.RepeatRadioButton = self.window.findChild(QRadioButton,
                                                       'RepeatRadioButton')
        self.RepeatRadioButton.clicked.connect(self.repeat_button_clicked)

        #ShuffleCheckBox = self.window.findChild(QCheckBox, 'ShuffleCheckBox')
        #ShuffleCheckBox.clicked.connect(self.shuffle_checkbox_clicked)

        #ShuttleSlider = self.window.findChild(QSlider, 'ShuttleSlider')
        #ShuttleSlider.valueChanged.connect(self.shuttle_slider_moved)

        self.VolumeSlider = self.window.findChild(QSlider, 'VolumeSlider')
        self.VolumeSlider.setValue(50)
        self.VolumeSlider.valueChanged.connect(self.change_volume_level)

        self.ProgressBar = self.window.findChild(QProgressBar, 'ProgressBar')
        self.music_player.durationChanged.connect(
            self.progress_bar_maximum_changed)
        self.music_player.positionChanged.connect(
            self.progress_bar_position_changed)

        #self.Playlist = self.window.findChild(QMediaPlaylist, 'Playlist')
        #self.Playlist.itemDoubleClicked.connect(self.volume_slider_moved)

        #show window to user
        self.window.show()

    #I referenced code from Jordan Abbott to complete this function
    def add_media_triggered(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        files, _ = QFileDialog.getOpenFileNames(None,
                                                "Select Media Files",
                                                "",
                                                "All Files (*)",
                                                options=options)
        if files:
            for file in files:
                self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(file)))
                self.playlistDisplay.addItem(os.path.basename(file))

    def quit_action_triggered(self):
        self.window.close()

    def pause_button_clicked(self):
        self.music_player.pause()

    def next_button_clicked(self):
        if self.music_player.currentMedia().isNull():
            self.playlist.setCurrentIndex(1)
            self.music_player.play()
        else:
            self.playlist.next()

    #def play_all_button_clicked(self):
    #

    def play_button_clicked(self):
        if self.music_player.currentMedia().isNull():
            self.playlist.setCurrentIndex(1)
            self.next_button_clicked()
            self.music_player.play()
        else:
            self.music_player.play()

        print(QMediaPlayer.EndOfMedia)

    def previous_button_clicked(self):
        self.playlist.previous()

    def progress_bar_maximum_changed(self, maximum):
        self.ProgressBar.setMaximum(maximum)
    # = self.music_player.duration()

    #def durationChanged(self, duration):
    #   self.positionSlider.setRange(0, duration)

    def progress_bar_position_changed(self, position):
        self.ProgressBar.setValue(position)

    def repeat_once_button_clicked(self, status):
        self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemOnce)
        self.play_button_clicked()
        self.RepeatOnceRadioButton.setAutoExclusive(False)
        self.RepeatOnceRadioButton.setChecked(False)
        self.RepeatOnceRadioButton.setAutoExclusive(True)

    def repeat_button_clicked(self):
        self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemLoop)

    #def shuffle_checkbox_clicked(self):
    #add code

    #def shuttle_slider_moved(self):
    #add code

    def change_volume_level(self):
        self.music_player.setVolume(self.VolumeSlider.value())
Exemplo n.º 2
0
class MainWindow(QObject):

    #class constructor
    def __init__(self, ui_file, parent=None):

        #reference to our music player
        self.music_player = QMediaPlayer()

        self.music_playlist = QMediaPlaylist()
        self.music_player.setVolume(80)

        #call parent QObject constructor
        super(MainWindow, self).__init__(parent)

        #load the UI file into Python
        ui_file = QFile(ui_file)
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()
        self.window = loader.load(ui_file)

        self.window.setWindowFlags(Qt.MSWindowsFixedSizeDialogHint)
        self.window.setWindowTitle("SynthWAV")

        #always remember to close files
        ui_file.close()

        #add event listeners
        open_action = self.window.findChild(QAction, 'action_open')
        open_action.triggered.connect(self.open_action_triggered)

        quit_action = self.window.findChild(QAction, 'action_quit')
        quit_action.triggered.connect(self.quit_action_triggered)

        open_button = self.window.findChild(QPushButton, 'open_button')
        open_button.clicked.connect(self.open_action_triggered)

        quit_button = self.window.findChild(QPushButton, 'quit_button')
        quit_button.clicked.connect(self.quit_action_triggered)

        play_button = self.window.findChild(QPushButton, 'play_button')
        play_button.clicked.connect(self.play_button_clicked)

        pause_button = self.window.findChild(QPushButton, 'pause_button')
        pause_button.clicked.connect(self.pause_button_clicked)

        stop_button = self.window.findChild(QPushButton, 'stop_button')
        stop_button.clicked.connect(self.stop_button_clicked)

        progress_slider = self.window.findChild(QSlider, 'progress_slider')
        self.music_player.positionChanged.connect(self.update_progress)
        progress_slider.sliderMoved.connect(self.scrub_progress)

        volume_slider = self.window.findChild(QSlider, 'volume_slider')
        volume_slider.setValue(self.music_player.volume())
        volume_slider.sliderMoved.connect(self.adjust_volume)

        next_button = self.window.findChild(QPushButton, 'next_button')
        next_button.clicked.connect(self.next_button_clicked)

        previous_button = self.window.findChild(QPushButton, 'previous_button')
        previous_button.clicked.connect(self.previous_button_clicked)

        fforward_button = self.window.findChild(QPushButton, 'fforward_button')
        fforward_button.clicked.connect(self.fforward_button_clicked)

        fbackward_button = self.window.findChild(QPushButton,
                                                 'fbackward_button')
        fbackward_button.clicked.connect(self.fbackward_button_clicked)

        self.music_playlist.currentMediaChanged.connect(self.change_title)

        #show window to user
        self.window.show()

    def open_action_triggered(self):
        files = QFileDialog.getOpenFileNames(self.window,
                                             "Add songs to playlist")
        for i in range(len(files[0])):
            self.music_playlist.addMedia(QUrl.fromLocalFile(str(files[0][i])))
        self.music_playlist.setCurrentIndex(0)
        self.music_player.setPlaylist(self.music_playlist)

    def change_title(self):
        title_label = self.window.findChild(QLabel, 'media_title')
        show_title_path = self.music_playlist.currentMedia().canonicalUrl(
        ).fileName()
        show_title = os.path.splitext(show_title_path)
        title_label.setText(show_title[0])

    def quit_action_triggered(self):
        self.window.close()

    def play_button_clicked(self):
        self.music_player.play()

    def pause_button_clicked(self):
        self.music_player.pause()

    def stop_button_clicked(self):
        self.music_player.stop()

    def next_button_clicked(self):
        self.music_playlist.next()

    def previous_button_clicked(self):
        self.music_playlist.previous()

    def fforward_button_clicked(self):
        self.music_player.setPosition(self.music_player.position() + 10000)

    def fbackward_button_clicked(self):
        self.music_player.setPosition(self.music_player.position() - 10000)

    def update_progress(self):
        progress_slider = self.window.findChild(QSlider, 'progress_slider')

        if self.music_player.duration != 0:
            progress_slider.setMaximum(self.music_player.duration())
            total_sec = (self.music_player.duration() / 1000) % 60
            total_min = (self.music_player.duration() / (1000 * 60)) % 60
            if (total_sec < 10):
                total_time = ("%d:0%d" % (int(total_min), int(total_sec)))
            else:
                total_time = ("%d:%d" % (int(total_min), int(total_sec)))
            track_duration_label = self.window.findChild(
                QLabel, 'track_duration_label')
            track_duration_label.setText(total_time)

        progress = self.music_player.position()
        progress_slider.setValue(progress)
        cur_sec = (self.music_player.position() / 1000) % 60
        cur_min = (self.music_player.position() / (1000 * 60)) % 60
        if (cur_sec < 10):
            cur_time = ("%d:0%d" % (int(cur_min), int(cur_sec)))
        else:
            cur_time = ("%d:%d" % (int(cur_min), int(cur_sec)))
        track_current_label = self.window.findChild(QLabel,
                                                    'track_current_label')
        track_current_label.setText(cur_time)

    def scrub_progress(self):
        progress_slider = self.window.findChild(QSlider, 'progress_slider')
        self.music_player.setPosition(progress_slider.sliderPosition())
        cur_min = (self.music_player.position() / 1000) % 60
        cur_sec = (self.music_player.position() / (1000 * 60)) % 60
        if (cur_sec < 10):
            cur_time = ("%d:0%d" % (int(cur_min), int(cur_sec)))
        else:
            cur_time = ("%d:%d" % (int(cur_min), int(cur_sec)))
        track_current_label = self.window.findChild(QLabel,
                                                    'track_current_label')
        track_current_label.setText(cur_time)

    def adjust_volume(self):
        volume_slider = self.window.findChild(QSlider, 'volume_slider')
        self.music_player.setVolume(volume_slider.sliderPosition())