Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
7.1 kB
1
Indexable
Never
//music home.dart

import 'dart:convert';

import 'package:cinelyric/elements/appbar.dart';
import 'package:cinelyric/elements/bottombar.dart';
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'package:cinelyric/elements/scaffold_bg.dart';
import 'package:cinelyric/screens/result_display_page.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

class MusicHome extends StatefulWidget {
  const MusicHome({Key? key});

  @override
  _MusicHomeState createState() => _MusicHomeState();
}

class _MusicHomeState extends State<MusicHome> {
  final SpeechToText _speechToText = SpeechToText();

  bool _speechEnabled = false;
  String _wordsSpoken = "";
  String token = "";

  @override
  void initState() {
    super.initState();
    initSpeech();
  }

  void initSpeech() async {
    _speechEnabled = await _speechToText.initialize();
    setState(() {});
  }

  void _startListening() async {
    await _speechToText.listen(onResult: _onSpeechResult);
    setState(() {});
  }

  void _stopListening() async {
    await _speechToText.stop();
    setState(() {});
  }

  void _onSpeechResult(result) {
    setState(() {
      _wordsSpoken = "${result.recognizedWords}";
      print(_wordsSpoken);
    });
  }

  Future<void> getDataFromSharedPreferences() async {
    // Get an instance of SharedPreferences
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? stringValue = prefs.getString('token');
    token = stringValue!;
    print('String value: $token');
  }

  Future getMusic() async {
    getDataFromSharedPreferences();
    String apiUrl = 'http://10.0.2.2:8000/song/';
    Map<String, String> headers = {
      'Authorization': 'Token $token',
      'Content-Type': 'application/json', // Specify content type as JSON
    };
    Map<String, dynamic> requestBody = {
      'lyric': _wordsSpoken,
    };
    String jsonBody = jsonEncode(requestBody);
    try {
      // Send the POST request
      http.Response response = await http.post(
        Uri.parse(apiUrl),
        headers: headers,
        body: jsonBody,
      );

      // Handle the response
      if (response.statusCode == 200) {
        // Request was successful
        print('Response: ${response.body}');
        Map<String, dynamic> decodedData = jsonDecode(response.body);
        int id = decodedData['id'];
        String artist = decodedData['artist_name'];
        String track = decodedData['track_name'];
        String genre = decodedData['genre'];
        String lyric = decodedData['lyrics'];
        String year = decodedData['release_date'];

        print('ID: $id');
        print('artist: $artist');
        print('track: $track');
        print('type: $genre');
        print('Year: $year');

        // context.read<MusicProvider>().changeMusicDetail(
        //     newId: id,
        //     newArtist: artist,
        //     newTrack: track,
        //     newGenre: genre,
        //     newLyric: lyric,
        //     newYear: year);

        // Navigator.push(
        //   context,
        //   MaterialPageRoute(builder: (context) => ResultHome()),
        // );
      } else {
        // Request failed
        print('Failed with status code: ${response.statusCode}');
        print('Response: ${response.body}');
        Map<String, dynamic> jasonBody = jsonDecode(response.body);
        String message = jasonBody['message'];
        print(message);
      }
    } catch (error) {
      // Handle any exceptions that occurred during the request
      print('Error: $error');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const MyAppBar(),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const Text(
              'Music Finder',
              style: TextStyle(fontSize: 24),
            ),
            const SizedBox(height: 20),
            if (_speechToText.isListening || _wordsSpoken.isNotEmpty)
              Container(
                decoration: BoxDecoration(
                  border: Border.all(),
                  borderRadius: BorderRadius.circular(10),
                ),
                padding: EdgeInsets.all(10),
                child: Column(
                  children: [
                    Text(
                      _wordsSpoken,
                      style: const TextStyle(
                        fontSize: 25,
                        fontWeight: FontWeight.w300,
                      ),
                    ),
                    const SizedBox(height: 10),
                    if (!_speechToText.isListening && _wordsSpoken.isNotEmpty)
                      TextButton(
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => ResultHome()),
                          );
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: const [
                            Text(
                              'See Results',
                              style: TextStyle(
                                color: Colors.blue,
                                fontSize: 16,
                              ),
                            ),
                            Icon(Icons.arrow_forward, color: Colors.blue),
                          ],
                        ),
                      ),
                  ],
                ),
              ),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        onPressed: _speechToText.isListening ? _stopListening : _startListening,
        tooltip: 'Listen',
        child: Icon(
          _speechToText.isNotListening ? Icons.mic_off : Icons.mic,
          size: 30,
          color: Colors.redAccent,
        ),
      ),
      bottomNavigationBar: const MyAppBottomBar(),
    );
  }
}




//musicprovieder

import 'package:flutter/material.dart';

class MusicProvider extends ChangeNotifier {
  int id;
  String artist_name;
  String track_name;
  String genre;
  String lyric;
  String release_date;

  MusicProvider({
    this.id = 00,
    this.artist_name = "default artist",
    this.track_name = "mehabooba",
    this.genre = "classic",
    this.lyric = "House so empty, need a centerpiece",
    this.release_date = "2023",
  });

  void changeMusicDetail({
    required int newId,
    required String newArtist,
    required String newTrack,
    required String newGenre,
    required String newLyric,
    required String newYear,
  }) async {
    id = newId;
    artist_name = newArtist;
    track_name = newTrack;
    genre = newGenre;
    lyric = newLyric;
    release_date = newYear;
    notifyListeners();
  }
}
Leave a Comment