Search…
Youtube API

Overview

Api from youtube allows you to watch a video from youtube on the devices (Android and IOS) using TotalCross.

Source Code

Youtube Example
1
public class YoutubeDemo extends MainWindow {
2
@Override
3
public void initUI() {
4
Edit.useNativeNumericPad = true;
5
Edit startEdit = new Edit();
6
startEdit.caption = "start (s)";
7
startEdit.setKeyboard(Edit.KBD_NUMERIC);
8
startEdit.setText("0");
9
Edit endEdit = new Edit();
10
endEdit.caption = "end (s)";
11
endEdit.setKeyboard(Edit.KBD_NUMERIC);
12
startEdit.setText("0");
13
Check auto = new Check("autoPlay");
14
add(startEdit, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
15
PARENTSIZE + 80, PREFERRED);
16
add(endEdit, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
17
PARENTSIZE + 80, PREFERRED);
18
add(auto, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
19
PARENTSIZE + 80, PREFERRED);
20
21
Button b = new Button("Open Video");
22
add(b, CENTER, AFTER + UnitsConverter.toPixels(DP + 16));
23
24
b.addPressListener((c) -> {
25
try {
26
int start = startEdit.getText() == null? 0 : (int) Double.parseDouble(startEdit.getText());
27
int end = endEdit.getText() == null? -1 : (int) Double.parseDouble(endEdit.getText());
28
boolean autoPlay = auto.isChecked();
29
new YoutubePlayer()
30
.start(start)
31
.end(end)
32
.autoPlay(autoPlay)
33
.play("o07Ju5snaCw",
34
(state) -> System.out.println("State: " + state));
35
} catch (Exception e) {
36
new MessageBox("Erro", e.getMessage()).popup();
37
}
38
});
39
}
Copied!

Attributes

Type
Name
Description
static final int
STATE_UNSTARTED
is a state of when the video has not started yet
static final int
STATE_ENDED
is a state of when the video ended
static final int
STATE_PLAYING
is a state of when the video is still playing
static final int
STATE_PAUSED
is a state of when the video is still paused
static final int
STATE_BUFFERING
is a state of when the video is still loading
static final int
STATE_CUED
is a state of when the video is still cued
static final int
STATE_UNKNOWN
when the player does not know what current state
static final int
ERROR_VIDEO_NOT_FOUND
when the video was not found
static final int
ERROR_UNKNOWN
Unknown error happened

Methods

Type
Name
Description
YoutubePlayer
autoPlay
Sets the video to play automatically when it's loaded.
YoutubePlayer
end
Sets the end point in seconds of the video.
YoutubePlayer
start
Sets the start point in seconds of the video.
void
play(String id)
plays the video that was passed in id
void
play(String id, Callback callback)
plays the video that was passed in the id and has a callback to inform which state of the player

References

  • You can view the code shown above in github
Last modified 2yr ago