Repository navigation
Expand file tree
/
Copy pathoffscreen.js
More file actions
132 lines (111 loc) · 3.17 KB
/
Copy pathoffscreen.js
File metadata and controls
132 lines (111 loc) · 3.17 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
const audioElement = document.getElementById('audioElement');
// Process audio data received from background script
function processAudioData(audioDataArray, mimeType, isRecording) {
try {
// 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);
// Set up audio element
audioElement.src = audioUrl;
// 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 time and duration
function getTimeInfo() {
return {
currentTime: audioElement.currentTime,
duration: audioElement.duration
};
}
// Seek to a specific time
function seekTo(time) {
audioElement.currentTime = time;
}
// 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':
audioElement.play();
break;
case 'pause':
audioElement.pause();
break;
case 'stop':
audioElement.pause();
audioElement.currentTime = 0;
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'stopped' });
break;
case 'seek': {
seekTo(message.time);
return true;
}
case 'getTimeInfo':
sendResponse({ timeInfo: getTimeInfo() });
return true;
}
});
// Initialize audio event handlers
audioElement.onplay = () => {
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'playing' });
};
audioElement.onpause = () => {
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'paused' });
};
audioElement.onended = () => {
chrome.runtime.sendMessage({ type: 'stateUpdate', state: 'stopped' });
};
// Add timeupdate event for seeking
audioElement.ontimeupdate = () => {
chrome.runtime.sendMessage({
type: 'timeUpdate',
timeInfo: {
currentTime: audioElement?.currentTime ?? 0,
duration: audioElement?.duration ?? 0
}
});
};