diff --git a/src/mediaplayer.h b/src/mediaplayer.h --- a/src/mediaplayer.h +++ b/src/mediaplayer.h @@ -219,15 +219,29 @@ /// \param name name of the output to set /// \returns \c true when setting was successful, \c false otherwise bool setAudioOutput(const QByteArray &name) - { return libvlc_audio_output_set(m_player, name.data()) == 0; } + { + mOutputName = name; + return libvlc_audio_output_set(m_player, name.constData()) == 0; + } /** * Set audio output device by name. * \param outputName the aout name (pulse, alsa, oss, etc.) * \param deviceName the output name (aout dependent) */ void setAudioOutputDevice(const QByteArray &outputName, const QByteArray &deviceName) - { libvlc_audio_output_device_set(m_player, outputName.data(), deviceName.data()); } + { + // According to comments at https://forum.videolan.org/viewtopic.php?t=98766 + // the output device needs to be set after the media is set: that is, after + // the call to libvlc_media_player_set_media() in MediaPlayer::setMedia(). + // So we save away the 'outputName' and 'deviceName' parameters here and + // set them in MediaPlayer::setMedia() if provided. + mOutputName = outputName; + mDeviceName = deviceName; + + // Set them here anyway to cover any other possible usage scenarios. + libvlc_audio_output_device_set(m_player, outputName.constData(), deviceName.constData()); + } int audioTrack() const { return libvlc_audio_get_track(m_player); } @@ -243,6 +257,10 @@ void setEqualizer(libvlc_equalizer_t *equalizer); #endif +private: + QByteArray mOutputName; + QByteArray mDeviceName; + signals: void lengthChanged(qint64 length); void seekableChanged(bool seekable); diff --git a/src/mediaplayer.cpp b/src/mediaplayer.cpp --- a/src/mediaplayer.cpp +++ b/src/mediaplayer.cpp @@ -108,6 +108,17 @@ { m_media = media; libvlc_media_player_set_media(m_player, *m_media); + + if (!mOutputName.isEmpty()) { + libvlc_audio_output_set(m_player, mOutputName.constData()); + } + + if (!mDeviceName.isEmpty()) { + // VLC recommendation is that the second 'module' parameter + // is NULL, see API documentation of libvlc_audio_output_device_set() + // at https://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__audio.html + libvlc_audio_output_device_set(m_player, nullptr, mDeviceName.constData()); + } } bool MediaPlayer::play() @@ -399,6 +410,7 @@ libvlc_media_player_stop(m_player); m_media->setCdTrack(track); libvlc_media_player_set_media(m_player, *m_media); + // TODO: does this need to reset the output/device as in setMedia() above? libvlc_media_player_play(m_player); }