Suport de The Movie Database

Hi Team, So I have been doing a flutter project and I tried to pull the trailer URL from the API. After the whole code is written without any errors, the key is being generated as null. Here is the code that I have written.

According to the code written and for any movie if the trailer button is clicked, a message with random key is being generated.

Failed to open trailer. Could not launch https:// www.youtube.com/watch?y=https:/ www.youtube.com/watch?v=TOwSzeBITAE

Could you please help me with this?

Thank you.

Future<String?> fetchTrailerUrl(int movieId) async {
    try {
      final response = await http.get(
        Uri.parse('$baseUrl/movie/$movieId/videos?api_key=$apiKey'),
      );

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);
        final results = data['results'] as List;

        // Look for a YouTube trailer
        final trailer = results.firstWhere(
          (video) => video['type'] == 'Trailer' && video['site'] == 'YouTube',
          orElse: () => null,
        );

        // Return the full YouTube URL if a trailer is found
        if (trailer != null) {
          return 'https://www.youtube.com/watch?v=${trailer['key']}';
        }

        // No trailer found
        return null;
      } else {
        throw Exception('Failed to load trailer: ${response.reasonPhrase}');
      }
    } catch (e) {
      print('Error in fetchTrailerUrl: $e');
      return null;
    }
  }
class TrailerButtonWidget extends StatelessWidget {
  final int movieId;

  const TrailerButtonWidget({super.key, required this.movieId});

  Future<void> _launchYouTube(String url) async {
    final Uri uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      // Automatically redirects to YouTube app or browser
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: ElevatedButton(
        onPressed: () async {
          try {
            final trailerKey = await ApiService().fetchTrailerUrl(movieId);
            if (trailerKey != null) {
              final trailerUrl = 'https://www.youtube.com/watch?v=$trailerKey';
              await _launchYouTube(trailerUrl);
            } else {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('Trailer not available.')),
              );
            }
          } catch (e) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Failed to open trailer: $e')),
            );
          }
        },
        child: const Text('Watch Trailer'),
      ),
    );
  }
}

4 resposta (a les p脿gines 1 de 1)

Jump to last post

Hello Team could you please provide any update on this?

@J_A_R_V_I_S said:
Fetching Movie Trailers from the API.
Hi Team,
So I have been doing a flutter project and I tried to pull the trailer URL from the API.
After the whole code is written without any errors, the key is being generated as null. Here is the code that I have written.

According to the code written and for any movie if the trailer button is clicked, a message with random key is being generated.

Failed to open trailer. Could not launch https://
www.youtube.com/watch?y=https:/
www.youtube.com/watch?v=TOwSzeBITAE
Could you please help me with this?
Thank you.

Future<String?> fetchTrailerUrl(int movieId) async {
      return null;
    }
  }
class TrailerButtonWidget extends StatelessWidget {
}

I am not a programmer.
So I can't help you by reading your code.
But looking at your code I notice something wrong in this part.

www.youtube.com/watch?y=https:/
www.youtube.com/watch?v=**TOwSzeBITAE**

That Youtube KEY, TOwSzeBITAE, is no longer available.

I can pass on the following information.

Trailer URL

When you make an API Request requesting Videos,
in the response you will receive something like this:

videos  
results 
0   
iso_639_1   "en"
iso_3166_1  "US"
name    "Fight Club - Theatrical Trailer Remastered in HD"
key "6JnN1DmbqoU"
site    "YouTube"
size    1080
type    "Trailer"
official    false
published_at    "2015-02-26T03:19:25.000Z"
id  "5e382d1b4ca676001453826d"

Just add the KEY in the respective URL:

YouTube: https://www.youtube.com/watch?v=KEY  
Vimeo: https://vimeo.com/KEY  
Vimeo: https://player.vimeo.com/video/KEY  

For example:
Youtube: https://www.youtube.com/watch?v=6JnN1DmbqoU
Youtube: https://www.youtube.com/watch?v=hzfvYSIIgyc
Vimeo: https://vimeo.com/282875052
Vimeo: https://player.vimeo.com/video/282875052

Travis Bell said on 2020-09-19
And it still holds true today. Nothing changed.

Hi @user, we only support YouTube and Vimeo right now.

And I don't see any plans on the To-Do list to add a new channel.
https://trello.com/b/k34aFUme/web?filter=video

Hi Team, I have made the update to the code in order to fetch the Trailer URL via redirection to YouTube. The trailer is being played but the video shows as the blank screen. Here is the code that I have written for the trailer button widget that renders in the user interface.

// Trailer Button Widget
class TrailerButtonWidget extends StatelessWidget {
  final int movieId;

  const TrailerButtonWidget({super.key, required this.movieId});

  Future<void> _launchYouTube(String url) async {
    final Uri uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: ElevatedButton(
        onPressed: () async {
          try {
            final trailerKey = await ApiService().fetchTrailerUrl(movieId);
            if (trailerKey != null) {
              // Construct the YouTube URL correctly
              final trailerUrl = 'https://www.youtube.com/watch?v=$trailerKey';
              print('Opening YouTube Trailer URL: $trailerUrl');
              await _launchYouTube(trailerUrl);
            } else {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('Trailer not available.')),
              );
            }
          } catch (e) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Failed to open trailer: $e')),
            );
          }
        },
        child: const Text('Watch Trailer'),
      ),
    );
  }
}

This is the code that I have written that tracks the data from the TMDB API.

/// Fetch trailer URL (example placeholder)
  Future<String?> fetchTrailerUrl(int movieId) async {
    final response = await http.get(
      Uri.parse('$baseUrl/movie/$movieId/videos?api_key=$apiKey'),
    );

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      final results = data['results'];

      if (results != null && results.isNotEmpty) {
        final trailer = results.firstWhere(
          (video) => video['type'] == 'Trailer',
          orElse: () => null,
        );

        if (trailer != null) {
          return trailer['key']; // Return only the video key
        }
      }
      return null;
    } else {
      throw Exception('Failed to load trailer');
    }
  }

Could you please help me with this?

Thank you.

@J_A_R_V_I_S said:

Hi Team,
I have made the update to the code in order to fetch the Trailer URL via redirection to YouTube.
The trailer is being played

Good

but the video shows as the blank screen.
Here is the code that I have written for the trailer button widget that renders in the user interface.

// Trailer Button Widget  
}

This is the code that I have written that tracks the data from the TMDB API.

/// Fetch trailer URL (example placeholder)
  Future<String?> fetchTrailerUrl(int movieId) async {
  }

Could you please help me with this?

I did a Google search.
https://www.google.com/search?client=firefox-b-d&q=trailer+is+being+played+but+the+video+shows+as+the+blank+screen
It seems like this is a problem for a lot of people.
I found this more recent question.
Maybe it will help.
https://community.firecore.com/t/black-screen-on-trailers/48616

No trobeu una pel路l铆cula o una s猫rie? Inicieu la sessi贸 per a crear-la.

Global

s centra la barra de cerca
p obre el men煤 del perfil
esc tanca una finestra oberta
? obre la finestra de dreceres de teclat

A les p脿gines de materials

b torna enrere (o la superior quan sigui aplicable)
e ves a la p脿gina d鈥檈dici贸

A les p脿gines de temporades

(fletxa dreta) ves a la temporada seg眉ent
(fletxa esquerra) ves a la temporada anterior

A les p脿gines d'episodis

(fletxa dreta) ves a l'episodi seg眉ent
(fletxa esquerra) ves a l'episodi anterior

A totes les p脿gines d'imatges

a obre la finestra d'afegir imatges

A totes les p脿gines d'edici贸

t obre el selector de traducci贸
ctrl+ s envia el formulari

A les p脿gines de debat

n crea un debat nou
w canvia l'estat de visualitzaci贸
p canvia p煤blic/privat
c tanca o obre
a obre activitat
r resposta al debat
l ves a la darrera resposta
ctrl+ enter envieu el vostre missatge
(fletxa dreta) p脿gina seg眉ent
(fletxa esquerra) p脿gina anterior

Configuracions

Desitgeu valorar o afegir aquest element a una llista?

Inicieu la sessi贸