Reputation: 403
I am wondering if it's possible to retrive the artist and song title from a YouTube video using the YouToube API. I think I can just parse the title, then search from API by artist (for example: http://www.youtube.com/artist?a=cJmvMpoqHdQ
), but it's not an optimal solution. Is there a more efficient/appropriate way of solving this problem?
Upvotes: 13
Views: 9009
Reputation: 113
Firstly, you can use youtube data api and get the title of the song like,
// Learn the process of generating youtube data api for your project (required)
var res = await get(Uri.parse('https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$your_youtube_data_api'));
var data = jsonDecode(res.body);
var songName = data['items'][0]['snippet']['title'];
You can fetch the singers' name from this title. But if you want directly the singers' name programmatically, then you have to use spotify api as follow,
// Learn the process to get accessToken of spotify (required)
var res2 = get(Uri.parse('https://api.spotify.com/v1/search?q=$songName&type=track&access_token=$your_spotify_access_token'));
var data2 = jsonDecode(res2.body);
var artists = [];
// add as many artists as you want
artists.add(data2['tracks']['items'][0]['artists'][0]['name']);
print(artists);
But, using spotify api requires authorization first which you will have to learn for sure.
Suggestion: If you are working on a music app then you should use spotify api not youtube api.
Disclaimer: This question is too old but, I thought, it must be answered so that it can help someone someday.
Upvotes: 0
Reputation: 618
You should be able to get most of details by passing Video id which is available in the youtube url. On making request to the below url u can get title , artist and many more.. Just enter the video id in the below url you will get a JSON response with all details..
(http://gdata.youtube.com/feeds/api/videos/Db9FBvpx49M?v=2&alt=json-in-script&callback=youtubeFeedCallback )
Upvotes: -3