# Youtube API

### Overview

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

### Source Code

{% code title="Youtube Example" %}

```java
public class YoutubeDemo extends MainWindow {
    @Override
    public void initUI() {
        Edit.useNativeNumericPad = true;
        Edit startEdit = new Edit();
        startEdit.caption = "start (s)";
        startEdit.setKeyboard(Edit.KBD_NUMERIC);
        startEdit.setText("0");
        Edit endEdit = new Edit();
        endEdit.caption = "end (s)";
        endEdit.setKeyboard(Edit.KBD_NUMERIC);
        startEdit.setText("0");
        Check auto = new Check("autoPlay");
        add(startEdit, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
                PARENTSIZE + 80, PREFERRED);
        add(endEdit, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
                PARENTSIZE + 80, PREFERRED);
        add(auto, CENTER, AFTER + UnitsConverter.toPixels(DP + 16),
                PARENTSIZE + 80, PREFERRED);

        Button b = new Button("Open Video");
        add(b, CENTER, AFTER + UnitsConverter.toPixels(DP + 16));

        b.addPressListener((c) -> {
            try {
                int start = startEdit.getText() == null? 0 : (int) Double.parseDouble(startEdit.getText());
                int end = endEdit.getText() == null? -1 : (int) Double.parseDouble(endEdit.getText());
                boolean autoPlay = auto.isChecked();
                new YoutubePlayer()
                        .start(start)
                        .end(end)
                        .autoPlay(autoPlay)
                        .play("o07Ju5snaCw",
                                (state) -> System.out.println("State: " + state));
            } catch (Exception e) {
                new MessageBox("Erro", e.getMessage()).popup();
            }
        });
    }
```

{% endcode %}

### 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](https://github.com/TotalCross/YoutubeSample)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.totalcross.com/documentation/apis/youtube-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
