diff --git a/lib/searchpage.dart b/lib/searchpage.dart index 0229b512a90b499129ba5caeb100aef74cb9e974..5c834388233d2f288d9f86401d3496a31c3b4c53 100644 --- a/lib/searchpage.dart +++ b/lib/searchpage.dart @@ -3,6 +3,9 @@ import 'package:flutter/material.dart'; import 'dart:developer'; import 'package:ambient/services/spotify.dart'; +import 'package:ambient/homepage.dart'; +import 'package:flutter/services.dart'; +import 'package:spotify_sdk/spotify_sdk.dart'; class StateSearcgPage extends StatefulWidget { const StateSearcgPage({super.key}); @@ -14,6 +17,7 @@ class StateSearcgPage extends StatefulWidget { class SearchPage extends State<StateSearcgPage> { final controller = TextEditingController(); Spotify spotifyApi = Spotify(); + HomePage homePage = HomePage(); List<Song> songs = List.empty(growable: true); @override @@ -65,7 +69,7 @@ class SearchPage extends State<StateSearcgPage> { ), ), onTap: () { - // TODO: Abspielen + play(song.id); }, ); })), @@ -82,4 +86,14 @@ class SearchPage extends State<StateSearcgPage> { songs.add(element); } } + + Future<void> play(String songId) async { + try { + await SpotifySdk.play(spotifyUri: 'spotify:track:$songId'); + } on PlatformException catch (e) { + //setStatus(e.code, message: e.message); + } on MissingPluginException { + //setStatus('not implemented'); + } + } } diff --git a/lib/services/spotify.dart b/lib/services/spotify.dart index a2716f903ae5c24a214f04ca3fbcee43513860f5..2d4f5a8b231a9a01972ea707ae45a13dc5ecb1b4 100644 --- a/lib/services/spotify.dart +++ b/lib/services/spotify.dart @@ -2,7 +2,9 @@ import 'dart:convert'; import 'dart:developer'; import 'dart:io'; +import 'package:flutter/services.dart'; import 'package:http/http.dart' as http; +import 'package:spotify_sdk/spotify_sdk.dart'; class Spotify { final clientId = '60cf1944b15d4f9fb223ba32ce6336d1'; @@ -61,6 +63,30 @@ class Spotify { } } + playGenre(genre) async { + try { + final response = await http.get( + Uri.parse('https://api.spotify.com/v1/search?q=genre=$genre'), + headers: { + // 'Authorization': 'Bearer $authToken', + 'Authorization': + 'Bearer BQBx44KHYThkfXDRdxk9MNUaepkW_29jFARFKqOcO11ukDjdzuHY4dzNg2vl_8QPZ2wHX9NzTYaYFbwEjt5a83MD9oHoBTrbgjwvzGRQkL8L6LLz571amR-YhhxTsH9pG8ClYDZv2zgUgKgqXH6W64YCfyYSTiqYWnjKuUGSTyVUnz-BEQTTqbjJqemR4H-thfA9oWQDp07Ud2hOtI8Q' + }, + ); + final responseJson = json.decode(response.body); + final items = responseJson['tracks']['items'] as List; + + List songList = items.map((item) => Song.fromJson(item)).toList(); + + var playSong = songList.first(); + + play(playSong['id']); + // T + } catch (e) { + log(e.toString()); + } + } + getUserAlbums() async { try { var response = await http.get( @@ -193,6 +219,16 @@ class Spotify { log(e.toString()); } } + + Future<void> play(String songId) async { + try { + await SpotifySdk.play(spotifyUri: 'spotify:track:$songId'); + } on PlatformException catch (e) { + //setStatus(e.code, message: e.message); + } on MissingPluginException { + //setStatus('not implemented'); + } + } } class Song {