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

A lot of fiture #51

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*.iws
.idea/

# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
Expand Down
26 changes: 19 additions & 7 deletions example/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: "d211f42860350d914a5ad8102f9ec32764dc6d06"
revision: "78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9"
channel: "stable"

project_type: app
Expand All @@ -13,14 +13,26 @@ project_type: app
migration:
platforms:
- platform: root
create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: android
create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: ios
create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: linux
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: macos
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: web
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
- platform: windows
create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9
base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9

# User provided section

Expand Down
123 changes: 123 additions & 0 deletions example/lib/cast_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:cast/cast.dart';
import 'package:flutter/material.dart';

class CastTile extends StatefulWidget {
const CastTile(this.device, {super.key});

final CastDevice device;

@override
State<CastTile> createState() => _CastTileState();
}

class _CastTileState extends State<CastTile> {
CastSession? session;
final double volumeChangeValue = 0.1;

Widget saperator() => const SizedBox(width: 10);

@override
Widget build(BuildContext context) {
return SizedBox(
height: 50,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: [
saperator(),
Text(widget.device.name),
saperator(),
TextButton(
onPressed: () async {
final double? volume = await widget.device.getVolume();
final snackBar = SnackBar(content: Text('Volume: $volume'));
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
},
child: const Text('Get Volume'),
),
saperator(),
TextButton(
onPressed: () => widget.device.volumeUp(volumeChangeValue),
child: const Text('Volume Up'),
),
saperator(),
TextButton(
onPressed: () => widget.device.volumeDown(volumeChangeValue),
child: const Text('Volume Down'),
),
saperator(),
TextButton(
onPressed: widget.device.playButton,
child: const Text('Play'),
),
saperator(),
TextButton(
onPressed: widget.device.pauseButton,
child: const Text('Pause'),
),
saperator(),
TextButton(
onPressed: () {
widget.device.openMedia(
url:
'http://commondatastorage.googleapis.com/gtv-videos-bucket/big_buck_bunny_1080p.mp4',
title: 'Big Buck Bunny',
coverImage:
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg',
);
},
child: const Text('Media'),
),
saperator(),
TextButton(
onPressed: () => widget.device.launchAppId('Netflix'),
child: const Text('Netflix'),
),
TextButton(
onPressed: () => widget.device.tts(
text: 'Hellow CyBear Jinni',
title: 'CBJ',
coverImage:
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg',
),
child: const Text('TTS'),
),
saperator(),
TextButton(
onPressed: () => widget.device.openUrl('https://cybearjinni.com'),
child: const Text('Browser'),
),
// saperator(),
// TextButton(
// onPressed: () => widget.device.openYouTube('o5owbiQahnY'),
// child: const Text('YouTube'),
// ),
saperator(),
TextButton(
onPressed: _openPLEX,
child: const Text('PLEX'),
),
saperator(),
TextButton(
onPressed: widget.device.close,
child: const Text('CLOSE'),
),
saperator(),
],
),
);
}

void _openPLEX() async {
widget.device.sendSingleRequestBefore(
CastSession.kNamespaceReceiver,
'LAUNCH',
payload: {
'appId': '9AC194DC', // PLEX app ID for Chromecast
},
close: false,
);
}
}
7 changes: 7 additions & 0 deletions example/lib/core/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:logger/logger.dart';

/// Instance of logger for all the app
final logger = Logger(
filter: ProductionFilter(),
printer: PrettyPrinter(methodCount: 0, printTime: true),
);
124 changes: 20 additions & 104 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:cast_example/cast_tile.dart';
import 'package:flutter/material.dart';
import 'package:cast/cast.dart';

void main() {
runApp(MyApp());
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -16,18 +19,20 @@ class MyApp extends StatelessWidget {
),
home: Scaffold(
appBar: AppBar(),
body: MyHomePage(),
body: const MyHomePage(),
),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});

@override
_MyHomePageState createState() => _MyHomePageState();
MyHomePageState createState() => MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
class MyHomePageState extends State<MyHomePage> {
Future<List<CastDevice>>? _future;

@override
Expand All @@ -48,13 +53,13 @@ class _MyHomePageState extends State<MyHomePage> {
),
);
} else if (!snapshot.hasData) {
return Center(
return const Center(
child: CircularProgressIndicator(),
);
}

if (snapshot.data!.isEmpty) {
return Column(
return const Column(
children: [
Center(
child: Text(
Expand All @@ -65,16 +70,15 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

return Column(
children: snapshot.data!.map((device) {
return ListTile(
title: Text(device.name),
onTap: () {
// _connectToYourApp(context, device);
_connectAndPlayMedia(context, device);
},
);
}).toList(),
return ListView.separated(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
final CastDevice device = snapshot.data![index];

return CastTile(device);
},
separatorBuilder: (BuildContext context, int index) =>
const SizedBox(height: 10),
);
},
);
Expand All @@ -83,92 +87,4 @@ class _MyHomePageState extends State<MyHomePage> {
void _startSearch() {
_future = CastDiscoveryService().search();
}

Future<void> _connectToYourApp(BuildContext context, CastDevice object) async {
final session = await CastSessionManager().startSession(object);

session.stateStream.listen((state) {
if (state == CastSessionState.connected) {
final snackBar = SnackBar(content: Text('Connected'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);

_sendMessageToYourApp(session);
}
});

session.messageStream.listen((message) {
print('receive message: $message');
});

session.sendMessage(CastSession.kNamespaceReceiver, {
'type': 'LAUNCH',
'appId': 'Youtube', // set the appId of your app here
});
}

void _sendMessageToYourApp(CastSession session) {
print('_sendMessageToYourApp');

session.sendMessage('urn:x-cast:namespace-of-the-app', {
'type': 'sample',
});
}

Future<void> _connectAndPlayMedia(BuildContext context, CastDevice object) async {
final session = await CastSessionManager().startSession(object);

session.stateStream.listen((state) {
if (state == CastSessionState.connected) {
final snackBar = SnackBar(content: Text('Connected'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});

var index = 0;

session.messageStream.listen((message) {
index += 1;

print('receive message: $message');

if (index == 2) {
Future.delayed(Duration(seconds: 5)).then((x) {
_sendMessagePlayVideo(session);
});
}
});

session.sendMessage(CastSession.kNamespaceReceiver, {
'type': 'LAUNCH',
'appId': 'CC1AD845', // set the appId of your app here
});
}

void _sendMessagePlayVideo(CastSession session) {
print('_sendMessagePlayVideo');

var message = {
// Here you can plug an URL to any mp4, webm, mp3 or jpg file with the proper contentType.
'contentId': 'http://commondatastorage.googleapis.com/gtv-videos-bucket/big_buck_bunny_1080p.mp4',
'contentType': 'video/mp4',
'streamType': 'BUFFERED', // or LIVE

// Title and cover displayed while buffering
'metadata': {
'type': 0,
'metadataType': 0,
'title': "Big Buck Bunny",
'images': [
{'url': 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg'}
]
}
};

session.sendMessage(CastSession.kNamespaceMedia, {
'type': 'LOAD',
'autoPlay': true,
'currentTime': 0,
'media': message,
});
}
}
1 change: 1 addition & 0 deletions example/linux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flutter/ephemeral
Loading