merge the formfield patch from ooo-build
[ooovba.git] / slideshow / source / engine / soundplayer.cxx
blobe9e6738457d8dc914251e8aa3bbf460dcf8cd68b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: soundplayer.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 #include <canvas/debug.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <canvas/verbosetrace.hxx>
38 #include <comphelper/anytostring.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
41 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42 #include <com/sun/star/lang/NoSupportException.hpp>
43 #include <com/sun/star/lang/XComponent.hdl>
45 #include <tools/urlobj.hxx>
47 #include <avmedia/mediawindow.hxx>
49 #include "soundplayer.hxx"
51 #include <algorithm>
53 using namespace ::com::sun::star;
56 namespace slideshow
58 namespace internal
60 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
62 boost::shared_ptr<SoundPlayer> SoundPlayer::create(
63 EventMultiplexer & rEventMultiplexer,
64 const ::rtl::OUString& rSoundURL,
65 const uno::Reference< uno::XComponentContext>& rComponentContext )
67 boost::shared_ptr<SoundPlayer> pPlayer(
68 new SoundPlayer( rEventMultiplexer,
69 rSoundURL,
70 rComponentContext ) );
71 rEventMultiplexer.addPauseHandler( pPlayer );
72 pPlayer->mThis = pPlayer;
73 return pPlayer;
76 bool SoundPlayer::handlePause( bool bPauseShow )
78 return bPauseShow ? stopPlayback() : startPlayback();
81 void SoundPlayer::dispose()
83 if( mThis )
85 mrEventMultiplexer.removePauseHandler( mThis );
86 mThis.reset();
89 if( mxPlayer.is() )
91 mxPlayer->stop();
92 uno::Reference<lang::XComponent> xComponent(
93 mxPlayer, uno::UNO_QUERY );
94 if( xComponent.is() )
95 xComponent->dispose();
96 mxPlayer.clear();
100 SoundPlayer::SoundPlayer(
101 EventMultiplexer & rEventMultiplexer,
102 const ::rtl::OUString& rSoundURL,
103 const uno::Reference< uno::XComponentContext>& rComponentContext )
104 : mrEventMultiplexer(rEventMultiplexer),
105 mThis(),
106 mxPlayer()
108 ENSURE_OR_THROW( rComponentContext.is(),
109 "SoundPlayer::SoundPlayer(): Invalid component context" );
113 const INetURLObject aURL( rSoundURL );
114 mxPlayer.set( avmedia::MediaWindow::createPlayer(
115 aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ),
116 uno::UNO_QUERY);
118 catch( uno::RuntimeException& )
120 throw;
122 catch( uno::Exception& )
126 if( !mxPlayer.is() )
127 throw lang::NoSupportException(
128 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
129 "No sound support for ") ) + rSoundURL,
130 uno::Reference<uno::XInterface>() );
133 SoundPlayer::~SoundPlayer()
137 dispose();
139 catch (uno::Exception &) {
140 OSL_ENSURE( false, rtl::OUStringToOString(
141 comphelper::anyToString(
142 cppu::getCaughtException() ),
143 RTL_TEXTENCODING_UTF8 ).getStr() );
147 double SoundPlayer::getDuration() const
149 if( !mxPlayer.is() )
150 return 0.0;
152 const double nDuration( mxPlayer->getDuration() );
153 if( mxPlayer->isPlaying() )
154 return ::std::max( 0.0,
155 nDuration - mxPlayer->getMediaTime() );
156 else
157 return nDuration;
160 bool SoundPlayer::startPlayback()
162 if( !mxPlayer.is() )
163 return false;
165 if( mxPlayer->isPlaying() )
166 mxPlayer->stop();
168 mxPlayer->start();
169 return true;
172 bool SoundPlayer::stopPlayback()
174 if( mxPlayer.is() )
175 mxPlayer->stop();
177 return true;
180 void SoundPlayer::setPlaybackLoop( bool bLoop )
182 if( mxPlayer.is() )
183 mxPlayer->setPlaybackLoop( bLoop );