31
31
import android .widget .TextView ;
32
32
33
33
import com .connectsdk .core .MediaInfo ;
34
+ import com .connectsdk .core .SubtitleInfo ;
34
35
import com .connectsdk .device .ConnectableDevice ;
35
36
import com .connectsdk .sampler .R ;
36
37
import com .connectsdk .sampler .util .TestResponseObject ;
56
57
import java .util .concurrent .TimeUnit ;
57
58
58
59
public class MediaPlayerFragment extends BaseFragment {
60
+ public static final String URL_SUBTITLES_WEBVTT =
61
+ "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/sintel_en.vtt" ;
62
+ public static final String URL_SUBTITLE_SRT =
63
+ "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/sintel_en.srt" ;
64
+ public static final String URL_VIDEO_MP4 =
65
+ "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/video.mp4" ;
66
+ public static final String URL_IMAGE_ICON =
67
+ "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/videoIcon.jpg" ;
68
+
59
69
public Button photoButton ;
60
70
public Button videoButton ;
61
71
public Button audioButton ;
@@ -71,8 +81,9 @@ public class MediaPlayerFragment extends BaseFragment {
71
81
public Button nextButton ;
72
82
public Button jumpButton ;
73
83
public CheckBox loopingButton ;
84
+ public CheckBox subtitlesButton ;
74
85
75
- public static LaunchSession launchSession ;
86
+ public LaunchSession launchSession ;
76
87
77
88
public TextView positionTextView ;
78
89
public TextView durationTextView ;
@@ -87,14 +98,14 @@ public class MediaPlayerFragment extends BaseFragment {
87
98
88
99
public boolean mSeeking ;
89
100
public Runnable mRefreshRunnable ;
90
- public static final int REFRESH_INTERVAL_MS = (int ) TimeUnit .SECONDS .toMillis (1 );
101
+ public final int REFRESH_INTERVAL_MS = (int ) TimeUnit .SECONDS .toMillis (1 );
91
102
public Handler mHandler ;
92
103
public long totalTimeDuration ;
93
104
public boolean mIsGettingPlayPosition ;
94
105
95
106
96
- static boolean isPlayingImage = false ;
97
- static boolean isPlaying = false ;
107
+ boolean isPlayingImage = false ;
108
+ boolean isPlaying = false ;
98
109
99
110
private MediaControl mMediaControl = null ;
100
111
private PlaylistControl mPlaylistControl = null ;
@@ -138,6 +149,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
138
149
nextButton = (Button ) rootView .findViewById (R .id .nextButton );
139
150
jumpButton = (Button ) rootView .findViewById (R .id .jumpButton );
140
151
loopingButton = (CheckBox ) rootView .findViewById (R .id .loopingButton );
152
+ subtitlesButton = (CheckBox ) rootView .findViewById (R .id .subtitlesButton );
141
153
142
154
positionTextView = (TextView ) rootView .findViewById (R .id .stream_position );
143
155
durationTextView = (TextView ) rootView .findViewById (R .id .stream_duration );
@@ -163,6 +175,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
163
175
nextButton ,
164
176
jumpButton ,
165
177
loopingButton ,
178
+ subtitlesButton ,
166
179
};
167
180
168
181
mHandler = new Handler ();
@@ -216,6 +229,8 @@ public void onClick(View view) {
216
229
totalTimeDuration = -1 ;
217
230
218
231
loopingButton .setEnabled (getTv ().hasCapability (MediaPlayer .Loop ));
232
+ subtitlesButton .setEnabled (getTv ().hasCapability (MediaPlayer .Subtitle_SRT )
233
+ || getTv ().hasCapability (MediaPlayer .Subtitle_WebVTT ));
219
234
220
235
if (getTv ().hasCapability (MediaPlayer .Play_Video )) {
221
236
videoButton .setEnabled (true );
@@ -303,25 +318,23 @@ private void playAudio() {
303
318
String mimeType = "audio/mp3" ;
304
319
boolean shouldLoop = loopingButton .isChecked ();
305
320
306
- getMediaPlayer ().playMedia (mediaURL , mimeType , title , description , iconURL , shouldLoop , new MediaPlayer .LaunchListener () {
321
+ MediaInfo mediaInfo = new MediaInfo .Builder (mediaURL , mimeType )
322
+ .setTitle (title )
323
+ .setDescription (description )
324
+ .setIcon (iconURL )
325
+ .build ();
326
+
327
+ getMediaPlayer ().playMedia (mediaInfo , shouldLoop , new MediaPlayer .LaunchListener () {
307
328
308
329
@ Override
309
330
public void onError (ServiceCommandError error ) {
310
- Log .d ("LG" , "Error playing audio" );
311
- if (launchSession != null ) {
312
- launchSession .close (null );
313
- launchSession = null ;
314
- testResponse = new TestResponseObject (false , error .getCode (), error .getMessage ());
315
- stopUpdating ();
316
- disableMedia ();
317
- isPlaying = isPlayingImage = false ;
318
- }
331
+ Log .d ("LG" , "Error playing audio" , error );
332
+ stopMediaSession ();
319
333
}
320
334
321
335
@ Override
322
336
public void onSuccess (MediaLaunchObject object ) {
323
337
Log .d ("LG" , "Started playing audio" );
324
-
325
338
launchSession = object .launchSession ;
326
339
testResponse = new TestResponseObject (true , TestResponseObject .SuccessCode , TestResponseObject .Play_Audio );
327
340
mMediaControl = object .mediaControl ;
@@ -330,8 +343,6 @@ public void onSuccess(MediaLaunchObject object) {
330
343
stopUpdating ();
331
344
enableMedia ();
332
345
isPlaying = true ;
333
- disconnectWebAppSession ();
334
-
335
346
}
336
347
});
337
348
}
@@ -344,18 +355,18 @@ private void playM3U() {
344
355
String mimeType = "application/x-mpegurl" ;
345
356
boolean shouldLoop = loopingButton .isChecked ();
346
357
347
- getMediaPlayer ().playMedia (mediaURL , mimeType , title , description , iconURL , shouldLoop , new MediaPlayer .LaunchListener () {
358
+ MediaInfo mediaInfo = new MediaInfo .Builder (mediaURL , mimeType )
359
+ .setTitle (title )
360
+ .setDescription (description )
361
+ .setIcon (iconURL )
362
+ .build ();
363
+
364
+ getMediaPlayer ().playMedia (mediaInfo , shouldLoop , new MediaPlayer .LaunchListener () {
348
365
349
366
@ Override
350
367
public void onError (ServiceCommandError error ) {
351
- Log .d ("LG" , "Error playing audio" );
352
- if (launchSession != null ) {
353
- launchSession .close (null );
354
- launchSession = null ;
355
- stopUpdating ();
356
- disableMedia ();
357
- isPlaying = isPlayingImage = false ;
358
- }
368
+ Log .d ("LG" , "Error playing audio" , error );
369
+ stopMediaSession ();
359
370
}
360
371
361
372
@ Override
@@ -367,7 +378,6 @@ public void onSuccess(MediaLaunchObject object) {
367
378
stopUpdating ();
368
379
enableMedia ();
369
380
isPlaying = true ;
370
- disconnectWebAppSession ();
371
381
}
372
382
});
373
383
}
@@ -381,71 +391,84 @@ private void showImage() {
381
391
String description = "Blender Open Movie Project" ;
382
392
String icon = "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/photoIcon.jpg" ;
383
393
384
- getMediaPlayer ().displayImage (imagePath , mimeType , title , description , icon , new MediaPlayer .LaunchListener () {
394
+ MediaInfo mediaInfo = new MediaInfo .Builder (imagePath , mimeType )
395
+ .setTitle (title )
396
+ .setDescription (description )
397
+ .setIcon (icon )
398
+ .build ();
399
+
400
+ getMediaPlayer ().displayImage (mediaInfo , new MediaPlayer .LaunchListener () {
401
+
402
+ @ Override
403
+ public void onError (ServiceCommandError error ) {
404
+ Log .e ("Error" , "Error displaying Image" , error );
405
+ stopMediaSession ();
406
+ }
385
407
386
408
@ Override
387
409
public void onSuccess (MediaLaunchObject object ) {
388
410
launchSession = object .launchSession ;
389
411
closeButton .setEnabled (true );
390
- testResponse = new TestResponseObject (true , TestResponseObject .SuccessCode , TestResponseObject .Display_image );
412
+ testResponse = new TestResponseObject (true , TestResponseObject .SuccessCode ,
413
+ TestResponseObject .Display_image );
391
414
closeButton .setOnClickListener (closeListener );
392
415
stopUpdating ();
393
416
isPlayingImage = true ;
394
- disconnectWebAppSession ();
395
-
396
- }
397
-
398
- @ Override
399
- public void onError (ServiceCommandError error ) {
400
- Log .e ("Error" , "Error displaying Image" );
401
- if (launchSession != null ) {
402
- launchSession .close (null );
403
- launchSession = null ;
404
- testResponse = new TestResponseObject (false , error .getCode (), error .getMessage ());
405
- stopUpdating ();
406
- disableMedia ();
407
- isPlaying = isPlayingImage = false ;
408
-
409
- }
410
417
}
411
418
});
412
419
}
413
420
414
421
private void playVideo () {
415
- String videoPath = "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/video.mp4" ;
416
- String mimeType = "video/mp4" ;
417
- String title = "Sintel Trailer" ;
418
- String description = "Blender Open Movie Project" ;
419
- String icon = "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/videoIcon.jpg" ;
420
422
boolean shouldLoop = loopingButton .isChecked ();
421
423
422
- getMediaPlayer ().playMedia (videoPath , mimeType , title , description , icon , shouldLoop , new MediaPlayer .LaunchListener () {
424
+ SubtitleInfo .Builder subtitleBuilder = null ;
425
+ if (subtitlesButton .isChecked ()) {
426
+ subtitleBuilder = new SubtitleInfo .Builder (
427
+ getTv ().hasCapability (MediaPlayer .Subtitle_WebVTT ) ? URL_SUBTITLES_WEBVTT :
428
+ URL_SUBTITLE_SRT );
429
+ subtitleBuilder .setLabel ("English" ).setLanguage ("en" );
430
+ }
431
+
432
+ MediaInfo mediaInfo = new MediaInfo .Builder (URL_VIDEO_MP4 , "video/mp4" )
433
+ .setTitle ("Sintel Trailer" )
434
+ .setDescription ("Blender Open Movie Project" )
435
+ .setIcon (URL_IMAGE_ICON )
436
+ .setSubtitleInfo (subtitleBuilder == null ? null : subtitleBuilder .build ())
437
+ .build ();
438
+
439
+ getMediaPlayer ().playMedia (mediaInfo , shouldLoop , new MediaPlayer .LaunchListener () {
440
+
441
+ @ Override
442
+ public void onError (ServiceCommandError error ) {
443
+ Log .e ("Error" , "Error playing video" , error );
444
+ stopMediaSession ();
445
+ }
423
446
424
447
public void onSuccess (MediaLaunchObject object ) {
425
448
launchSession = object .launchSession ;
426
- testResponse = new TestResponseObject (true , TestResponseObject .SuccessCode , TestResponseObject .Play_Video );
449
+ testResponse = new TestResponseObject (true , TestResponseObject .SuccessCode ,
450
+ TestResponseObject .Play_Video );
427
451
mMediaControl = object .mediaControl ;
428
452
mPlaylistControl = object .playlistControl ;
429
453
stopUpdating ();
430
454
enableMedia ();
431
455
isPlaying = true ;
432
- disconnectWebAppSession ();
433
- }
434
-
435
- @ Override
436
- public void onError (ServiceCommandError error ) {
437
- if (launchSession != null ) {
438
- launchSession .close (null );
439
- launchSession = null ;
440
- testResponse = new TestResponseObject (false , error .getCode (), error .getMessage ());
441
- stopUpdating ();
442
- disableMedia ();
443
- isPlaying = isPlayingImage = false ;
444
- }
445
456
}
446
457
});
447
458
}
448
459
460
+
461
+ private void stopMediaSession () {
462
+ // don't call launchSession.close() here, currently it can close
463
+ // a different web app in WebOS
464
+ if (launchSession != null ) {
465
+ launchSession = null ;
466
+ stopUpdating ();
467
+ disableMedia ();
468
+ isPlaying = isPlayingImage = false ;
469
+ }
470
+ }
471
+
449
472
@ Override
450
473
public void disableButtons () {
451
474
mSeekBar .setEnabled (false );
@@ -459,6 +482,7 @@ public void disableButtons() {
459
482
positionTrackView .setEnabled (false );
460
483
461
484
loopingButton .setChecked (false );
485
+ subtitlesButton .setEnabled (false );
462
486
super .disableButtons ();
463
487
}
464
488
@@ -834,14 +858,6 @@ private String formatTime(long millisec) {
834
858
return time ;
835
859
}
836
860
837
- private void disconnectWebAppSession () {
838
- if (WebAppFragment .mWebAppSession != null ) {
839
- WebAppFragment .mWebAppSession .setWebAppSessionListener (null );
840
- WebAppFragment .mWebAppSession .close (null );
841
- WebAppFragment .isLaunched = false ;
842
- }
843
- }
844
-
845
861
private class DownloadImageTask extends AsyncTask <String , Void , Bitmap > {
846
862
ImageView bmImage ;
847
863
0 commit comments