android: Update app-specific/MIME type icons
[LibreOffice.git] / slideshow / source / engine / soundplayer.cxx
blob96bf9d387e3a0c2e22e0e9ab93cdeea3f8e9ae3f
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 <config_features.h>
22 #include <comphelper/diagnose_ex.hxx>
24 #include <com/sun/star/lang/NoSupportException.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
27 #include <tools/urlobj.hxx>
29 #include <avmedia/mediawindow.hxx>
30 #include <mediafilemanager.hxx>
31 #include <soundplayer.hxx>
33 #include <algorithm>
35 using namespace ::com::sun::star;
38 namespace slideshow::internal
40 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
42 std::shared_ptr<SoundPlayer> SoundPlayer::create(
43 EventMultiplexer & rEventMultiplexer,
44 const OUString& rSoundURL,
45 const uno::Reference< uno::XComponentContext>& rComponentContext,
46 MediaFileManager& rMediaFileManager)
48 std::shared_ptr<SoundPlayer> pPlayer(
49 new SoundPlayer( rEventMultiplexer,
50 rSoundURL,
51 rComponentContext,
52 rMediaFileManager) );
53 rEventMultiplexer.addPauseHandler( pPlayer );
54 pPlayer->mThis = pPlayer;
55 return pPlayer;
58 bool SoundPlayer::handlePause( bool bPauseShow )
60 return bPauseShow ? stopPlayback() : startPlayback();
63 void SoundPlayer::dispose()
65 if( mThis )
67 mrEventMultiplexer.removePauseHandler( mThis );
68 mThis.reset();
71 if( mxPlayer.is() )
73 mxPlayer->stop();
74 uno::Reference<lang::XComponent> xComponent(
75 mxPlayer, uno::UNO_QUERY );
76 if( xComponent.is() )
77 xComponent->dispose();
78 mxPlayer.clear();
82 SoundPlayer::SoundPlayer(
83 EventMultiplexer & rEventMultiplexer,
84 const OUString& rSoundURL,
85 const uno::Reference< uno::XComponentContext>& rComponentContext,
86 MediaFileManager& rMediaFileManager)
87 : mrEventMultiplexer(rEventMultiplexer),
88 mThis(),
89 mxPlayer()
91 ENSURE_OR_THROW( rComponentContext.is(),
92 "SoundPlayer::SoundPlayer(): Invalid component context" );
94 #if !HAVE_FEATURE_AVMEDIA
95 (void) rMediaFileManager;
96 #else
97 try
99 if (rSoundURL.startsWithIgnoreAsciiCase("vnd.sun.star.Package:"))
101 mpMediaTempFile = rMediaFileManager.getMediaTempFile(rSoundURL);
103 const INetURLObject aURL( mpMediaTempFile ? mpMediaTempFile->m_TempFileURL : rSoundURL );
104 mxPlayer = avmedia::MediaWindow::createPlayer(
105 aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), ""/*TODO!*/ );
107 catch( uno::RuntimeException& )
109 throw;
111 catch( uno::Exception& )
114 #endif
116 if( !mxPlayer.is() )
117 throw lang::NoSupportException(
118 "No sound support for " + rSoundURL );
121 SoundPlayer::~SoundPlayer()
125 dispose();
127 catch (uno::Exception &) {
128 TOOLS_WARN_EXCEPTION( "slideshow", "" );
132 double SoundPlayer::getDuration() const
134 if( !mxPlayer.is() )
135 return 0.0;
137 const double nDuration( mxPlayer->getDuration() );
138 if( mxPlayer->isPlaying() )
139 return ::std::max( 0.0,
140 nDuration - mxPlayer->getMediaTime() );
141 else
142 return nDuration;
145 bool SoundPlayer::startPlayback()
147 if( !mxPlayer.is() )
148 return false;
150 if( mxPlayer->isPlaying() )
151 mxPlayer->stop();
153 mxPlayer->start();
154 return true;
157 bool SoundPlayer::stopPlayback()
159 if( mxPlayer.is() )
160 mxPlayer->stop();
162 return true;
165 void SoundPlayer::setPlaybackLoop( bool bLoop )
167 if( mxPlayer.is() )
168 mxPlayer->setPlaybackLoop( bLoop );
171 bool SoundPlayer::isPlaying() const
173 return mxPlayer->isPlaying();
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */