-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaPlayer.java
More file actions
345 lines (299 loc) · 9.33 KB
/
Copy pathMediaPlayer.java
File metadata and controls
345 lines (299 loc) · 9.33 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.lcdui.*;
import java.io.InputStream;
public class MediaPlayer {
private Player player;
private VideoControl videoControl;
private VolumeControl volumeControl;
private Canvas canvas;
private String currentFilePath = "";
private boolean isPlaying = false;
private boolean isPaused = false;
// ✅ NEW: Player state constants
public static final int STATE_CLOSED = 0;
public static final int STATE_UNREALIZED = 1;
public static final int STATE_REALIZED = 2;
public static final int STATE_PREFETCHED = 3;
public static final int STATE_STARTED = 4;
public MediaPlayer(Canvas c) {
canvas = c;
}
/**
* ✅ CHANGED: Enhanced play method with better error handling
*/
public boolean play(String filePath) {
try {
// Stop any existing playback
stop();
currentFilePath = filePath;
// ✅ NEW: Verify file exists before attempting to play
if (!StorageManager.fileExists(filePath)) {
throw new Exception("File not found: " + filePath);
}
// ✅ CHANGED: Get input stream with proper error handling
InputStream is = null;
try {
is = StorageManager.openInputStream(filePath);
} catch (Exception e) {
throw new Exception("Cannot open file: " + e.getMessage());
}
// ✅ CHANGED: Detect MIME type based on file extension
String mimeType = detectMimeType(filePath);
// Create player
player = Manager.createPlayer(is, mimeType);
player.realize();
// Get video control if available
videoControl = (VideoControl) player.getControl("VideoControl");
if (videoControl != null && canvas instanceof VideoCanvas) {
((VideoCanvas) canvas).setVideoControl(videoControl);
}
// Get volume control
volumeControl = (VolumeControl) player.getControl("VolumeControl");
if (volumeControl != null) {
volumeControl.setLevel(75); // Default volume
}
// Prefetch and start
player.prefetch();
player.start();
isPlaying = true;
isPaused = false;
return true;
} catch (Exception e) {
// ✅ NEW: Cleanup on error
cleanup();
// ✅ NEW: Show error to user
if (canvas instanceof PlayerCanvas || canvas instanceof AudioPlayerCanvas) {
Alert error = new Alert("Erreur Lecture",
"Impossible de lire le fichier:\n" +
e.getMessage() + "\n\n" +
"Fichier: " + getFileName(filePath) + "\n" +
"Stockage: " + StorageManager.getDownloadPath(),
null, AlertType.ERROR);
error.setTimeout(5000);
// Note: Cannot show alert here without Display reference
// This should be handled by the calling Canvas
}
return false;
}
}
/**
* ✅ NEW: Detect MIME type from file extension
*/
private String detectMimeType(String filePath) {
String lower = filePath.toLowerCase();
// Video formats
if (lower.endsWith(".3gp")) {
return "video/3gpp";
} else if (lower.endsWith(".mp4")) {
return "video/mp4";
} else if (lower.endsWith(".avi")) {
return "video/x-msvideo";
}
// Audio formats
else if (lower.endsWith(".mp3")) {
return "audio/mpeg";
} else if (lower.endsWith(".wav")) {
return "audio/wav";
} else if (lower.endsWith(".aac")) {
return "audio/aac";
} else if (lower.endsWith(".amr")) {
return "audio/amr";
}
// Default
return "video/3gpp";
}
/**
* ✅ NEW: Extract filename from path
*/
private String getFileName(String path) {
int lastSlash = path.lastIndexOf('/');
if (lastSlash != -1 && lastSlash < path.length() - 1) {
return path.substring(lastSlash + 1);
}
return path;
}
/**
* ✅ NEW: Pause playback
*/
public boolean pause() {
if (player != null && isPlaying) {
try {
player.stop();
isPaused = true;
isPlaying = false;
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
/**
* ✅ NEW: Resume playback
*/
public boolean resume() {
if (player != null && isPaused) {
try {
player.start();
isPlaying = true;
isPaused = false;
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
/**
* ✅ CHANGED: Renamed to cleanup for clarity
*/
public void stop() {
cleanup();
}
/**
* ✅ NEW: Proper cleanup method
*/
private void cleanup() {
isPlaying = false;
isPaused = false;
if (player != null) {
try {
player.stop();
} catch (Exception e) {}
try {
player.deallocate();
} catch (Exception e) {}
try {
player.close();
} catch (Exception e) {}
player = null;
}
videoControl = null;
volumeControl = null;
currentFilePath = "";
}
/**
* ✅ CHANGED: Enhanced volume control
*/
public void setVolume(int level) {
// Clamp level between 0 and 100
if (level < 0) level = 0;
if (level > 100) level = 100;
if (volumeControl != null) {
try {
volumeControl.setLevel(level);
} catch (Exception e) {
// Ignore volume control errors
}
}
}
/**
* ✅ NEW: Get current volume level
*/
public int getVolume() {
if (volumeControl != null) {
try {
return volumeControl.getLevel();
} catch (Exception e) {
return 75; // Default
}
}
return 75;
}
/**
* ✅ NEW: Get media duration in microseconds
*/
public long getDuration() {
if (player != null) {
try {
return player.getDuration();
} catch (Exception e) {
return Player.TIME_UNKNOWN;
}
}
return Player.TIME_UNKNOWN;
}
/**
* ✅ NEW: Get current playback time in microseconds
*/
public long getMediaTime() {
if (player != null) {
try {
return player.getMediaTime();
} catch (Exception e) {
return 0;
}
}
return 0;
}
/**
* ✅ NEW: Seek to specific time (microseconds)
*/
public boolean setMediaTime(long time) {
if (player != null) {
try {
player.setMediaTime(time);
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
/**
* ✅ NEW: Get player state
*/
public int getState() {
if (player == null) {
return STATE_CLOSED;
}
try {
return player.getState();
} catch (Exception e) {
return STATE_CLOSED;
}
}
/**
* ✅ NEW: Check if currently playing
*/
public boolean isPlaying() {
return isPlaying;
}
/**
* ✅ NEW: Check if paused
*/
public boolean isPaused() {
return isPaused;
}
/**
* ✅ NEW: Get current file path
*/
public String getCurrentFilePath() {
return currentFilePath;
}
/**
* ✅ NEW: Get video control (if available)
*/
public VideoControl getVideoControl() {
return videoControl;
}
/**
* ✅ NEW: Get volume control (if available)
*/
public VolumeControl getVolumeControl() {
return volumeControl;
}
/**
* ✅ NEW: Check if video control is available
*/
public boolean hasVideoControl() {
return videoControl != null;
}
/**
* ✅ NEW: Check if volume control is available
*/
public boolean hasVolumeControl() {
return volumeControl != null;
}
}