-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathcontrol_media_player.py
216 lines (182 loc) · 7.65 KB
/
control_media_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# This script allows you to use your Linux computer as a Bluetooth speaker.
# The MediaPlayer interface lets you interact with the media player on the
# other end of the Bluetooth connection (e.g., the music player on your phone).
# It displays information about the current track.
# Before running this script, ensure you pair and connect your audio source.
from bluezero.adapter import list_adapters, Adapter
from bluezero import dbus_tools
from bluezero.device import Device
from bluezero import constants
from bluezero.media_player import MediaPlayer
import time
def filter_by_interface(objects, interface_name):
""" filters the objects based on their support for
the specified interface """
object_paths = []
for path in objects.keys():
interfaces = objects[path]
for interface in interfaces.keys():
if interface == interface_name:
object_paths.append(path)
return object_paths
def filter_audio_devices(objects):
""" Filters devices that support the A2DP audio profile (AudioSink) """
audio_interface = 'org.freedesktop.DBus.Properties'
audio_devices = filter_by_interface(objects, audio_interface)
audio_sink_devices = []
for path in audio_devices:
interfaces = objects[path]
if 'org.freedesktop.DBus.Properties' in interfaces:
if 'AudioSink' in interfaces['org.freedesktop.DBus.Properties']:
audio_sink_devices.append(path)
return audio_sink_devices
def ctrl_media_player(media_player):
print("Make sure you are using a media player on your phone and "
"have selected a track...")
print("To interact with the Media Player, "
"choose the corresponding number:")
print("1. Play")
print("2. Pause")
print("3. Previous")
print("4. Next")
print("5. Track Info")
print("6. End")
while True:
try:
command = int(input("Enter a number (1-6): "))
if command == 1:
media_player.play()
elif command == 2:
media_player.pause()
elif command == 3:
media_player.previous()
elif command == 4:
media_player.next()
elif command == 5:
track_details = media_player.track
time.sleep(2)
for detail in track_details:
print(f'{detail} : {track_details[detail]}')
elif command == 6:
print("Ending media control.")
break
else:
print("Invalid choice. Please select a number "
"between 1 and 6.")
except ValueError:
print("Invalid input. Please enter a number between 1 and 6.")
def discover_devices(adapter_address):
""" Function to discover nearby devices and list them """
bluetooth_adapter = Adapter(adapter_address)
bluetooth_adapter.nearby_discovery()
print("Scanning for nearby devices...")
# Find managed devices and their addresses
dev_obj_path_list = filter_by_interface(dbus_tools.get_managed_objects(),
constants.DEVICE_INTERFACE)
dev_addr_list = list(
map(dbus_tools.get_mac_addr_from_dbus_path, dev_obj_path_list))
devices = []
for addr in dev_addr_list:
# Get the name of the device
device = Device(adapter_address, addr)
try:
name = device.name
devices.append((name, addr))
except Exception as e:
# If there is an error retrieving the device name, skip it
print(f"Error retrieving name for device {addr}: {e}")
return devices
def connect_to_device(adapter_address, device_address, retries=3, delay=5):
""" Function to connect to the selected device with retries """
attempt = 0
while attempt < retries:
try:
remote_device = Device(adapter_address, device_address)
# Verify if the device is paired, if not, pair it
if remote_device.paired != 1:
print(f"Pairing with {device_address}...")
try:
remote_device.pair()
print(f"Successfully paired with {device_address}")
except Exception as e:
print(
f"Pairing failed: {e}. Retrying... "
f"({attempt + 1}/{retries})")
print("Make sure you confirm pairing "
"on computer and phone")
attempt += 1
time.sleep(delay)
continue
# Verify if the device is connected, if not, connect it
if remote_device.connected != 1:
print(f"Connecting to {device_address}...")
remote_device.connect()
# Wait for the media player to be ready
time.sleep(10)
# Initialize the media player and control it
mp = MediaPlayer(device_address)
ctrl_media_player(mp)
return # Successful connection
except ValueError as e:
print(f"Error: {e}. Retrying... ({attempt + 1}/{retries})")
attempt += 1
time.sleep(delay)
print(f"Failed to connect to {device_address} after {retries} attempts.")
def select_adapter():
""" Prompt user to select a Bluetooth adapter """
adapters = list_adapters()
if not adapters:
print("No Bluetooth adapters found.")
return None
print("Available Bluetooth adapters:")
for i, adapter in enumerate(adapters, 1):
print(f"{i}. {adapter}")
try:
choice = int(input("Select the adapter by number: "))
if 1 <= choice <= len(adapters):
return adapters[choice - 1]
else:
print("Invalid choice. Please select a valid adapter.")
return None
except ValueError:
print("Invalid input. Please enter a number.")
return None
if __name__ == '__main__':
""" This script assumes that your remote
device has its Bluetooth function on.
It will list the nearby devices and let the user
choose which one to connect to. """
adapter_address = select_adapter() # Select a Bluetooth adapter
if not adapter_address:
print("Exiting due to invalid adapter selection.")
exit(1)
while True:
# Discover nearby devices
devices = discover_devices(adapter_address)
if devices:
print("Nearby devices:")
for i, (name, addr) in enumerate(devices, 1):
print(f"{i}. {name} ({addr})")
# Ask the user to select a device
try:
choice = int(
input(
"Enter the number of the device you "
"want to connect to (or 0 to refresh): "))
if choice == 0:
continue # Refresh discovery if the user selects 0
# Subtract 1 to match list index
selected_device = devices[choice - 1]
print(f"Attempting to connect to {selected_device[0]} "
f"({selected_device[1]})...")
connect_to_device(adapter_address, selected_device[1])
break # Exit the loop after successful connection
except (ValueError, IndexError):
print("Invalid choice. Please enter a valid device number.")
else:
print("No nearby devices found.")
# Ask if the user wants to refresh discovery or exit
refresh = input(
"Do you want to refresh device discovery? (y/n): ").strip().lower()
if refresh != 'y':
break