forked from phildougherty/local_tts_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
214 lines (180 loc) · 5.44 KB
/
offscreen.js
File metadata and controls
214 lines (180 loc) · 5.44 KB
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
let audioContext = null;
let audioElement = null;
let isPlaying = false;
let audioSource = null;
let hasSourceConnected = false;
// Initialize the audio context
function initAudio() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
if (!audioElement) {
audioElement = document.getElementById('audioElement');
if (!audioElement) {
audioElement = document.createElement('audio');
audioElement.id = 'audioElement';
audioElement.controls = true; // For debugging
document.body.appendChild(audioElement);
}
}
}
// Process audio data received from background script
function processAudioData(audioDataArray, mimeType, isRecording) {
try {
initAudio();
// Convert array back to Uint8Array
const uint8Array = new Uint8Array(audioDataArray);
// Create blob from the array
const blob = new Blob([uint8Array], { type: mimeType });
// Create URL for the blob
const audioUrl = URL.createObjectURL(blob);
// If recording is enabled, send URL back for download
if (isRecording) {
chrome.runtime.sendMessage({
type: 'recordingComplete',
audioUrl: audioUrl
});
}
// Play the audio
playAudioUrl(audioUrl);
// Notify that audio is ready to play
chrome.runtime.sendMessage({ type: 'audioReady' });
} catch (error) {
console.error('Error processing audio data:', error);
chrome.runtime.sendMessage({
type: 'streamError',
error: error.message
});
}
}
// Play audio from URL
function playAudioUrl(audioUrl) {
try {
console.log('Playing audio URL:', audioUrl);
// Reset connection flag
hasSourceConnected = false;
// Set up audio element
audioElement.src = audioUrl;
// Set up event listeners
audioElement.onplay = () => {
isPlaying = true;
// Connect to audio context only once
if (!hasSourceConnected) {
audioSource = audioContext.createMediaElementSource(audioElement);
audioSource.connect(audioContext.destination);
hasSourceConnected = true;
}
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'playing' });
};
audioElement.onpause = () => {
isPlaying = false;
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'paused' });
};
audioElement.onended = () => {
isPlaying = false;
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'stopped' });
chrome.runtime.sendMessage({ type: 'streamComplete' });
};
// Add timeupdate event for seeking
audioElement.ontimeupdate = () => {
chrome.runtime.sendMessage({
type: 'timeUpdate',
timeInfo: {
currentTime: audioElement.currentTime,
duration: audioElement.duration
}
});
};
// Start playing
audioElement.play().catch(err => {
console.error('Play error:', err);
chrome.runtime.sendMessage({
type: 'streamError',
error: err.message
});
});
} catch (error) {
console.error('Error playing audio URL:', error);
chrome.runtime.sendMessage({
type: 'streamError',
error: error.message
});
}
}
// Get current player state
function getPlayerState() {
if (!audioElement) return 'stopped';
if (audioElement.paused) {
return audioElement.currentTime > 0 && audioElement.currentTime < audioElement.duration ? 'paused' : 'stopped';
}
return 'playing';
}
// Get current time and duration
function getTimeInfo() {
if (!audioElement) return null;
return {
currentTime: audioElement.currentTime,
duration: audioElement.duration
};
}
// Seek to a specific time
function seekTo(time) {
if (!audioElement) return false;
try {
audioElement.currentTime = time;
return true;
} catch (error) {
console.error('Error seeking:', error);
return false;
}
}
// Handle messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Offscreen received message:', message.type);
switch (message.type) {
case 'processAudioData':
if (message.audioData) {
processAudioData(message.audioData, message.mimeType, message.isRecording);
}
break;
case 'play':
if (audioElement) {
audioElement.play();
}
break;
case 'pause':
if (audioElement) {
audioElement.pause();
}
break;
case 'stop':
if (audioElement) {
audioElement.pause();
audioElement.currentTime = 0;
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'stopped' });
}
break;
case 'seek':
const success = seekTo(message.time);
sendResponse({ success });
return true;
case 'getState':
sendResponse({ state: getPlayerState() });
return true;
case 'getTimeInfo':
sendResponse({ timeInfo: getTimeInfo() });
return true;
}
});
// Initialize when the document loads
document.addEventListener('DOMContentLoaded', () => {
console.log('Offscreen document loaded');
// Create audio element
audioElement = document.createElement('audio');
audioElement.id = 'audioElement';
audioElement.controls = true; // For debugging
document.body.appendChild(audioElement);
// Initialize audio context
initAudio();
console.log('Offscreen document initialized');
});