File size: 4,328 Bytes
3639861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 🎤 Voice Studio - iFrame Integration Guide

## Vấn đề Microphone trong iFrame

Khi embed ứng dụng Voice Studio vào iframe, microphone có thể không hoạt động do chính sách bảo mật của trình duyệt.

## ✅ Giải pháp cho Website

### 1. Thêm Permissions Policy vào iframe tag:

```html

<iframe 

    src="http://localhost:7864" 

    width="100%" 

    height="800"

    allow="microphone *; camera *; display-capture *; autoplay *"

    permissions-policy="microphone=*, camera=*, display-capture=*, autoplay=*"

    sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-modals allow-presentation"

    frameborder="0">

</iframe>

```

### 2. Cấu hình HTTPS (Khuyến nghị):

Microphone chỉ hoạt động tốt trên HTTPS. Nếu có thể, hãy deploy ứng dụng qua HTTPS.

### 3. Alternative HTML cho iframe:

```html

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Voice Studio Demo</title>

</head>

<body style="margin: 0; padding: 0; overflow: hidden;">

    <iframe 

        id="voice-studio"

        src="http://localhost:7864" 

        width="100%" 

        height="100vh"

        allow="microphone *; camera *; display-capture *; autoplay *"

        permissions-policy="microphone=*, camera=*, display-capture=*, autoplay=*"

        sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-modals"

        frameborder="0"

        style="border: none;">

    </iframe>



    <script>

    // Handle microphone permission fallback

    window.addEventListener('message', function(event) {

        if (event.data.type === 'microphone_error') {

            const fallbackDiv = document.createElement('div');

            fallbackDiv.innerHTML = `

                <div style="

                    position: fixed; 

                    top: 20px; 

                    right: 20px; 

                    background: #ff6b6b; 

                    color: white; 

                    padding: 15px; 

                    border-radius: 8px;

                    z-index: 9999;

                    box-shadow: 0 4px 12px rgba(0,0,0,0.2);

                ">

                    <strong>🎤 Microphone Blocked</strong><br>

                    <button onclick="window.open('${event.data.url}', '_blank')" 

                            style="margin-top: 10px; padding: 8px 16px; background: white; color: #ff6b6b; border: none; border-radius: 4px; cursor: pointer;">

                        Open in New Window

                    </button>

                </div>

            `;

            document.body.appendChild(fallbackDiv);

        }

    });

    </script>

</body>

</html>

```

## 🔧 Server Configuration

Nếu bạn host ứng dụng trên server riêng, thêm headers sau:

```python

# Trong Flask/Django

response.headers['Permissions-Policy'] = 'microphone=*, camera=*, display-capture=*'

response.headers['Feature-Policy'] = 'microphone \'self\' *; camera \'self\' *'

response.headers['X-Frame-Options'] = 'ALLOWALL'



# Hoặc trong nginx.conf

add_header Permissions-Policy "microphone=*, camera=*, display-capture=*";

add_header Feature-Policy "microphone 'self' *; camera 'self' *";

add_header X-Frame-Options "ALLOWALL";

```

## 📱 Mobile Considerations

Trên mobile, microphone trong iframe có thể bị hạn chế nhiều hơn. Khuyến nghị:

1. Hiển thị nút "Open in New Window" rõ ràng
2. Detect mobile và hiển thị thông báo phù hợp
3. Fallback to file upload nếu microphone không hoạt động

## 🚀 Best Practices

1. **HTTPS Required**: Deploy qua HTTPS để microphone hoạt động tốt nhất
2. **User Feedback**: Luôn hiển thị thông báo rõ ràng khi microphone bị block
3. **Fallback Options**: Cung cấp tùy chọn upload file thay thế
4. **Test Cross-Browser**: Test trên Chrome, Firefox, Safari, Edge

## 🔍 Debugging

Mở Developer Console để xem logs:
- `Running in iframe - requesting microphone permissions`
- `Microphone access granted/denied`

Nếu vẫn có vấn đề, users có thể click "🔗 Open in New Window" để sử dụng đầy đủ tính năng.