Line data Source code
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 <canvas/debug.hxx>
22 : #include <tools/diagnose_ex.h>
23 : #include <canvas/verbosetrace.hxx>
24 :
25 : #include <comphelper/anytostring.hxx>
26 : #include <cppuhelper/exc_hlp.hxx>
27 :
28 : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
29 : #include <com/sun/star/lang/NoSupportException.hpp>
30 : #include <com/sun/star/lang/XComponent.hpp>
31 :
32 : #include <tools/urlobj.hxx>
33 :
34 : #include <avmedia/mediawindow.hxx>
35 :
36 : #include "soundplayer.hxx"
37 :
38 : #include <algorithm>
39 :
40 : using namespace ::com::sun::star;
41 :
42 :
43 : namespace slideshow
44 : {
45 : namespace internal
46 : {
47 : // TODO(Q3): Move the whole SoundPlayer class to avmedia.
48 :
49 0 : boost::shared_ptr<SoundPlayer> SoundPlayer::create(
50 : EventMultiplexer & rEventMultiplexer,
51 : const OUString& rSoundURL,
52 : const uno::Reference< uno::XComponentContext>& rComponentContext )
53 : {
54 : boost::shared_ptr<SoundPlayer> pPlayer(
55 : new SoundPlayer( rEventMultiplexer,
56 : rSoundURL,
57 0 : rComponentContext ) );
58 0 : rEventMultiplexer.addPauseHandler( pPlayer );
59 0 : pPlayer->mThis = pPlayer;
60 0 : return pPlayer;
61 : }
62 :
63 0 : bool SoundPlayer::handlePause( bool bPauseShow )
64 : {
65 0 : return bPauseShow ? stopPlayback() : startPlayback();
66 : }
67 :
68 0 : void SoundPlayer::dispose()
69 : {
70 0 : if( mThis )
71 : {
72 0 : mrEventMultiplexer.removePauseHandler( mThis );
73 0 : mThis.reset();
74 : }
75 :
76 0 : if( mxPlayer.is() )
77 : {
78 0 : mxPlayer->stop();
79 : uno::Reference<lang::XComponent> xComponent(
80 0 : mxPlayer, uno::UNO_QUERY );
81 0 : if( xComponent.is() )
82 0 : xComponent->dispose();
83 0 : mxPlayer.clear();
84 : }
85 0 : }
86 :
87 0 : SoundPlayer::SoundPlayer(
88 : EventMultiplexer & rEventMultiplexer,
89 : const OUString& rSoundURL,
90 : const uno::Reference< uno::XComponentContext>& rComponentContext )
91 : : mrEventMultiplexer(rEventMultiplexer),
92 : mThis(),
93 0 : mxPlayer()
94 : {
95 0 : ENSURE_OR_THROW( rComponentContext.is(),
96 : "SoundPlayer::SoundPlayer(): Invalid component context" );
97 :
98 : try
99 : {
100 0 : const INetURLObject aURL( rSoundURL );
101 : mxPlayer.set( avmedia::MediaWindow::createPlayer(
102 : aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ), ""/*TODO!*/ ),
103 0 : uno::UNO_QUERY);
104 : }
105 0 : catch( uno::RuntimeException& )
106 : {
107 0 : throw;
108 : }
109 0 : catch( uno::Exception& )
110 : {
111 : }
112 :
113 0 : if( !mxPlayer.is() )
114 : throw lang::NoSupportException(
115 : OUString(
116 0 : "No sound support for ") + rSoundURL,
117 0 : uno::Reference<uno::XInterface>() );
118 0 : }
119 :
120 0 : SoundPlayer::~SoundPlayer()
121 : {
122 : try
123 : {
124 0 : dispose();
125 : }
126 0 : catch (uno::Exception &) {
127 : OSL_FAIL( OUStringToOString(
128 : comphelper::anyToString(
129 : cppu::getCaughtException() ),
130 : RTL_TEXTENCODING_UTF8 ).getStr() );
131 : }
132 0 : }
133 :
134 0 : double SoundPlayer::getDuration() const
135 : {
136 0 : if( !mxPlayer.is() )
137 0 : return 0.0;
138 :
139 0 : const double nDuration( mxPlayer->getDuration() );
140 0 : if( mxPlayer->isPlaying() )
141 : return ::std::max( 0.0,
142 0 : nDuration - mxPlayer->getMediaTime() );
143 : else
144 0 : return nDuration;
145 : }
146 :
147 0 : bool SoundPlayer::startPlayback()
148 : {
149 0 : if( !mxPlayer.is() )
150 0 : return false;
151 :
152 0 : if( mxPlayer->isPlaying() )
153 0 : mxPlayer->stop();
154 :
155 0 : mxPlayer->start();
156 0 : return true;
157 : }
158 :
159 0 : bool SoundPlayer::stopPlayback()
160 : {
161 0 : if( mxPlayer.is() )
162 0 : mxPlayer->stop();
163 :
164 0 : return true;
165 : }
166 :
167 0 : void SoundPlayer::setPlaybackLoop( bool bLoop )
168 : {
169 0 : if( mxPlayer.is() )
170 0 : mxPlayer->setPlaybackLoop( bLoop );
171 0 : }
172 : }
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|