Use o3tl::convert in Math
[LibreOffice.git] / slideshow / source / engine / soundplayer.cxx
blobc9386192e8d679c83d148e3544e03daef8f150ca
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 <com/sun/star/lang/NoSupportException.hpp>
24 #include <com/sun/star/lang/XComponent.hpp>
26 #include <tools/urlobj.hxx>
28 #include <avmedia/mediawindow.hxx>
29 #include <mediafilemanager.hxx>
30 #include <soundplayer.hxx>
32 #include <algorithm>
34 using namespace ::com::sun::star;
37 namespace slideshow::internal
39 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
41 std::shared_ptr<SoundPlayer> SoundPlayer::create(
42 EventMultiplexer & rEventMultiplexer,
43 const OUString& rSoundURL,
44 const uno::Reference< uno::XComponentContext>& rComponentContext,
45 MediaFileManager& rMediaFileManager)
47 std::shared_ptr<SoundPlayer> pPlayer(
48 new SoundPlayer( rEventMultiplexer,
49 rSoundURL,
50 rComponentContext,
51 rMediaFileManager) );
52 rEventMultiplexer.addPauseHandler( pPlayer );
53 pPlayer->mThis = pPlayer;
54 return pPlayer;
57 bool SoundPlayer::handlePause( bool bPauseShow )
59 return bPauseShow ? stopPlayback() : startPlayback();
62 void SoundPlayer::dispose()
64 if( mThis )
66 mrEventMultiplexer.removePauseHandler( mThis );
67 mThis.reset();
70 if( mxPlayer.is() )
72 mxPlayer->stop();
73 uno::Reference<lang::XComponent> xComponent(
74 mxPlayer, uno::UNO_QUERY );
75 if( xComponent.is() )
76 xComponent->dispose();
77 mxPlayer.clear();
81 SoundPlayer::SoundPlayer(
82 EventMultiplexer & rEventMultiplexer,
83 const OUString& rSoundURL,
84 const uno::Reference< uno::XComponentContext>& rComponentContext,
85 MediaFileManager& rMediaFileManager)
86 : mrEventMultiplexer(rEventMultiplexer),
87 mThis(),
88 mxPlayer()
90 ENSURE_OR_THROW( rComponentContext.is(),
91 "SoundPlayer::SoundPlayer(): Invalid component context" );
93 try
95 if (rSoundURL.startsWithIgnoreAsciiCase("vnd.sun.star.Package:"))
97 mpMediaTempFile = rMediaFileManager.getMediaTempFile(rSoundURL);
99 const INetURLObject aURL( mpMediaTempFile ? mpMediaTempFile->m_TempFileURL : rSoundURL );
100 mxPlayer = avmedia::MediaWindow::createPlayer(
101 aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), ""/*TODO!*/ );
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 TOOLS_WARN_EXCEPTION( "slideshow", "" );
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 );
166 bool SoundPlayer::isPlaying() const
168 return mxPlayer->isPlaying();
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */