blob: 82f051a4f8e9c3edf6322dc8954730f60880cadc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package com.mapswithme.maps.base;
import android.app.Application;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import com.mapswithme.maps.MwmApplication;
public class MediaPlayerWrapper
{
private static final int UNDEFINED_SOUND_STREAM = -1;
@NonNull
private final Application mApp;
@Nullable
private MediaPlayer mPlayer;
@Nullable
private MediaPlayer.OnCompletionListener mCompletionListener;
private int mStreamResId = UNDEFINED_SOUND_STREAM;
public MediaPlayerWrapper(@NonNull Application application)
{
mApp = application;
}
private boolean isCurrentSoundStream(@RawRes int streamResId)
{
return mStreamResId == streamResId;
}
@NonNull
private Application getApp()
{
return mApp;
}
private void onInitializationCompleted(@NonNull InitializationResult initializationResult)
{
releaseInternal();
mStreamResId = initializationResult.getStreamResId();
mPlayer = initializationResult.getPlayer();
if (mPlayer == null)
return;
mPlayer.setOnCompletionListener(mCompletionListener);
mPlayer.start();
}
private void releaseInternal()
{
if (mPlayer == null)
return;
stop();
mPlayer.release();
mPlayer = null;
mStreamResId = UNDEFINED_SOUND_STREAM;
}
@NonNull
private static AsyncTask<Integer, Void, InitializationResult> makeInitTask(@NonNull MediaPlayerWrapper wrapper)
{
return new InitPlayerTask(wrapper);
}
public void release()
{
releaseInternal();
mCompletionListener = null;
}
public void playback(@RawRes int streamResId,
@Nullable MediaPlayer.OnCompletionListener completionListener)
{
if (isCurrentSoundStream(streamResId) && mPlayer == null)
return;
if (isCurrentSoundStream(streamResId) && mPlayer != null)
{
mPlayer.start();
return;
}
mCompletionListener = completionListener;
mStreamResId = streamResId;
AsyncTask<Integer, Void, InitializationResult> task = makeInitTask(this);
task.execute(streamResId);
}
public void stop()
{
if (mPlayer == null)
return;
mPlayer.stop();
}
public boolean isPlaying()
{
return mPlayer != null && mPlayer.isPlaying();
}
@NonNull
public static MediaPlayerWrapper from(@NonNull Context context)
{
MwmApplication app = (MwmApplication) context.getApplicationContext();
return app.getMediaPlayer();
}
private static class InitPlayerTask extends AsyncTask<Integer, Void, InitializationResult>
{
@NonNull
private final MediaPlayerWrapper mWrapper;
InitPlayerTask(@NonNull MediaPlayerWrapper wrapper)
{
mWrapper = wrapper;
}
@Override
protected InitializationResult doInBackground(Integer... params)
{
if (params.length == 0)
throw new IllegalArgumentException("Params not found");
int resId = params[0];
MediaPlayer player = MediaPlayer.create(mWrapper.getApp(), resId);
return new InitializationResult(player, resId);
}
@Override
protected void onPostExecute(InitializationResult initializationResult)
{
super.onPostExecute(initializationResult);
mWrapper.onInitializationCompleted(initializationResult);
}
}
private static class InitializationResult
{
@Nullable
private final MediaPlayer mPlayer;
@RawRes
private final int mStreamResId;
private InitializationResult(@Nullable MediaPlayer player, @RawRes int streamResId)
{
mPlayer = player;
mStreamResId = streamResId;
}
@RawRes
private int getStreamResId()
{
return mStreamResId;
}
@Nullable
private MediaPlayer getPlayer()
{
return mPlayer;
}
}
}
|