LibreOffice Module slideshow (master) 1
soundplayer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <config_features.h>
23
24#include <com/sun/star/lang/NoSupportException.hpp>
25#include <com/sun/star/lang/XComponent.hpp>
26
27#include <tools/urlobj.hxx>
28
30#include <mediafilemanager.hxx>
31#include <soundplayer.hxx>
32
33#include <algorithm>
34
35using namespace ::com::sun::star;
36
37
38namespace slideshow::internal
39{
40 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
41
42 std::shared_ptr<SoundPlayer> SoundPlayer::create(
43 EventMultiplexer & rEventMultiplexer,
44 const OUString& rSoundURL,
45 const uno::Reference< uno::XComponentContext>& rComponentContext,
46 MediaFileManager& rMediaFileManager)
47 {
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;
56 }
57
58 bool SoundPlayer::handlePause( bool bPauseShow )
59 {
60 return bPauseShow ? stopPlayback() : startPlayback();
61 }
62
64 {
65 if( mThis )
66 {
68 mThis.reset();
69 }
70
71 if( mxPlayer.is() )
72 {
73 mxPlayer->stop();
75 mxPlayer, uno::UNO_QUERY );
76 if( xComponent.is() )
77 xComponent->dispose();
78 mxPlayer.clear();
79 }
80 }
81
83 EventMultiplexer & rEventMultiplexer,
84 const OUString& rSoundURL,
85 const uno::Reference< uno::XComponentContext>& rComponentContext,
86 MediaFileManager& rMediaFileManager)
87 : mrEventMultiplexer(rEventMultiplexer),
88 mThis(),
89 mxPlayer()
90 {
91 ENSURE_OR_THROW( rComponentContext.is(),
92 "SoundPlayer::SoundPlayer(): Invalid component context" );
93
94#if !HAVE_FEATURE_AVMEDIA
95 (void) rMediaFileManager;
96#else
97 try
98 {
99 if (rSoundURL.startsWithIgnoreAsciiCase("vnd.sun.star.Package:"))
100 {
101 mpMediaTempFile = rMediaFileManager.getMediaTempFile(rSoundURL);
102 }
103 const INetURLObject aURL( mpMediaTempFile ? mpMediaTempFile->m_TempFileURL : rSoundURL );
105 aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), ""/*TODO!*/ );
106 }
107 catch( uno::RuntimeException& )
108 {
109 throw;
110 }
111 catch( uno::Exception& )
112 {
113 }
114#endif
115
116 if( !mxPlayer.is() )
117 throw lang::NoSupportException(
118 "No sound support for " + rSoundURL );
119 }
120
122 {
123 try
124 {
125 dispose();
126 }
127 catch (uno::Exception &) {
128 TOOLS_WARN_EXCEPTION( "slideshow", "" );
129 }
130 }
131
133 {
134 if( !mxPlayer.is() )
135 return 0.0;
136
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;
143 }
144
146 {
147 if( !mxPlayer.is() )
148 return false;
149
150 if( mxPlayer->isPlaying() )
151 mxPlayer->stop();
152
153 mxPlayer->start();
154 return true;
155 }
156
158 {
159 if( mxPlayer.is() )
160 mxPlayer->stop();
161
162 return true;
163 }
164
166 {
167 if( mxPlayer.is() )
168 mxPlayer->setPlaybackLoop( bLoop );
169 }
170
172 {
173 return mxPlayer->isPlaying();
174 }
175}
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Reference< css::media::XPlayer > createPlayer(const OUString &rURL, const OUString &rReferer, const OUString *pMimeType=nullptr)
This class multiplexes user-activated and slide-show global events.
void removePauseHandler(const PauseEventHandlerSharedPtr &rHandler)
void addPauseHandler(const PauseEventHandlerSharedPtr &rHandler)
Register a handler that is called when the show enters or exits pause mode.
virtual std::shared_ptr< avmedia::MediaTempFile > getMediaTempFile(const OUString &aUrl)=0
css::uno::Reference< css::media::XPlayer > mxPlayer
virtual void dispose() override
Dispose all object references.
Definition: soundplayer.cxx:63
virtual bool handlePause(bool bPauseShow) override
Handle the event.
Definition: soundplayer.cxx:58
::std::shared_ptr< SoundPlayer > mThis
::std::shared_ptr<::avmedia::MediaTempFile > mpMediaTempFile
SoundPlayer(EventMultiplexer &rEventMultiplexer, const OUString &rSoundURL, const css::uno::Reference< css::uno::XComponentContext > &rComponentContext, MediaFileManager &rMediaFileManager)
Definition: soundplayer.cxx:82
EventMultiplexer & mrEventMultiplexer
double getDuration() const
Query duration of sound playback.
static ::std::shared_ptr< SoundPlayer > create(EventMultiplexer &rEventMultiplexer, const OUString &rSoundURL, const css::uno::Reference< css::uno::XComponentContext > &rComponentContext, MediaFileManager &rMediaFileManager)
Create a sound player object.
Definition: soundplayer.cxx:42
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_THROW(c, m)
URL aURL
EventMultiplexer & mrEventMultiplexer
Definition: slideview.cxx:728