Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching Movie Trailers. #21

Open
GnyanVarun opened this issue Dec 30, 2024 · 3 comments
Open

Fetching Movie Trailers. #21

GnyanVarun opened this issue Dec 30, 2024 · 3 comments

Comments

@GnyanVarun
Copy link

Hi @lacymorrow , I am having the issue with fetching the trailers from the TMDB API. I have written this code in the program but it is showing as error message as Failed to load trailer.

I have also referenced in the main API service file that is created in the project.

Could you please help me with this?

class TrailerButtonWidget extends StatelessWidget {
final int movieId;

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

Future _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'),
),
);
}
}

/// Fetch trailer URL (example placeholder)
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;
}

}

@lacymorrow
Copy link
Owner

I might be able to help, could you format the code a little better? What is the search term?

@GnyanVarun
Copy link
Author

Hi @lacymorrow Thank you for your help. I just tried to edit the code for the trailers part from my end. The redirection to youTube is quite efficient, the trailer is being played but the video always shows as a blank screen. Here is the code that I have written for the trailer button widget that renders in the user interface.

image

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

image

Could you please help me with this?

Thank you.

@lacymorrow
Copy link
Owner

Can you create a stackblitz or codepen?

This isn't really a movie-trailer issue, but I may be able to help if you post a reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants