Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / engine / soundplayer.cxx
blob9dfffa60fa6085ce1eb722d0f164b4076b520105
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/diagnose_ex.h>
23 #include <comphelper/anytostring.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
26 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
27 #include <com/sun/star/lang/NoSupportException.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
30 #include <tools/urlobj.hxx>
32 #include <avmedia/mediawindow.hxx>
34 #include <soundplayer.hxx>
36 #include <algorithm>
38 using namespace ::com::sun::star;
41 namespace slideshow
43 namespace internal
45 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
47 std::shared_ptr<SoundPlayer> SoundPlayer::create(
48 EventMultiplexer & rEventMultiplexer,
49 const OUString& rSoundURL,
50 const uno::Reference< uno::XComponentContext>& rComponentContext )
52 std::shared_ptr<SoundPlayer> pPlayer(
53 new SoundPlayer( rEventMultiplexer,
54 rSoundURL,
55 rComponentContext ) );
56 rEventMultiplexer.addPauseHandler( pPlayer );
57 pPlayer->mThis = pPlayer;
58 return pPlayer;
61 bool SoundPlayer::handlePause( bool bPauseShow )
63 return bPauseShow ? stopPlayback() : startPlayback();
66 void SoundPlayer::dispose()
68 if( mThis )
70 mrEventMultiplexer.removePauseHandler( mThis );
71 mThis.reset();
74 if( mxPlayer.is() )
76 mxPlayer->stop();
77 uno::Reference<lang::XComponent> xComponent(
78 mxPlayer, uno::UNO_QUERY );
79 if( xComponent.is() )
80 xComponent->dispose();
81 mxPlayer.clear();
85 SoundPlayer::SoundPlayer(
86 EventMultiplexer & rEventMultiplexer,
87 const OUString& rSoundURL,
88 const uno::Reference< uno::XComponentContext>& rComponentContext )
89 : mrEventMultiplexer(rEventMultiplexer),
90 mThis(),
91 mxPlayer()
93 ENSURE_OR_THROW( rComponentContext.is(),
94 "SoundPlayer::SoundPlayer(): Invalid component context" );
96 try
98 const INetURLObject aURL( rSoundURL );
99 mxPlayer.set( avmedia::MediaWindow::createPlayer(
100 aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), ""/*TODO!*/ ),
101 uno::UNO_QUERY);
103 catch( uno::RuntimeException& )
105 throw;
107 catch( uno::Exception& )
111 if( !mxPlayer.is() )
112 throw lang::NoSupportException(
113 "No sound support for " + rSoundURL );
116 SoundPlayer::~SoundPlayer()
120 dispose();
122 catch (uno::Exception &) {
123 SAL_WARN( "slideshow", comphelper::anyToString( cppu::getCaughtException() ) );
127 double SoundPlayer::getDuration() const
129 if( !mxPlayer.is() )
130 return 0.0;
132 const double nDuration( mxPlayer->getDuration() );
133 if( mxPlayer->isPlaying() )
134 return ::std::max( 0.0,
135 nDuration - mxPlayer->getMediaTime() );
136 else
137 return nDuration;
140 bool SoundPlayer::startPlayback()
142 if( !mxPlayer.is() )
143 return false;
145 if( mxPlayer->isPlaying() )
146 mxPlayer->stop();
148 mxPlayer->start();
149 return true;
152 bool SoundPlayer::stopPlayback()
154 if( mxPlayer.is() )
155 mxPlayer->stop();
157 return true;
160 void SoundPlayer::setPlaybackLoop( bool bLoop )
162 if( mxPlayer.is() )
163 mxPlayer->setPlaybackLoop( bLoop );
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */