1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ex.variablespeed;
18
19import android.content.Context;
20import android.media.MediaPlayer;
21import android.net.Uri;
22
23import java.io.IOException;
24
25/**
26 * Simple wrapper around a {@link MediaPlayerProxy}, guaranteeing that every call made to the
27 * MediaPlayerProxy is single-threaded.
28 */
29public class SingleThreadedMediaPlayerProxy implements MediaPlayerProxy {
30    private final MediaPlayerProxy mDelegate;
31
32    public SingleThreadedMediaPlayerProxy(MediaPlayerProxy delegate) {
33        mDelegate = delegate;
34    }
35
36    @Override
37    public synchronized void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
38        mDelegate.setOnErrorListener(listener);
39    }
40
41    @Override
42    public synchronized void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
43        mDelegate.setOnCompletionListener(listener);
44    }
45
46    @Override
47    public synchronized void release() {
48        mDelegate.release();
49    }
50
51    @Override
52    public synchronized void reset() {
53        mDelegate.reset();
54    }
55
56    @Override
57    public synchronized void setDataSource(String path) throws IllegalStateException, IOException {
58        mDelegate.setDataSource(path);
59    }
60
61    @Override
62    public synchronized void setDataSource(Context context, Uri intentUri)
63            throws IllegalStateException, IOException {
64        mDelegate.setDataSource(context, intentUri);
65    }
66
67    @Override
68    public synchronized void prepare() throws IOException {
69        mDelegate.prepare();
70    }
71
72    @Override
73    public synchronized int getDuration() {
74        return mDelegate.getDuration();
75    }
76
77    @Override
78    public synchronized void seekTo(int startPosition) {
79        mDelegate.seekTo(startPosition);
80    }
81
82    @Override
83    public synchronized void start() {
84        mDelegate.start();
85    }
86
87    @Override
88    public synchronized boolean isPlaying() {
89        return mDelegate.isPlaying();
90    }
91
92    @Override
93    public synchronized int getCurrentPosition() {
94        return mDelegate.getCurrentPosition();
95    }
96
97    public void setVariableSpeed(float rate) {
98        ((VariableSpeed) mDelegate).setVariableSpeed(rate);
99    }
100
101    @Override
102    public synchronized void pause() {
103        mDelegate.pause();
104    }
105
106    @Override
107    public void setAudioStreamType(int streamType) {
108        mDelegate.setAudioStreamType(streamType);
109    }
110}
111