Hello Admin Saad,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you are having Issue with Media Playback in Azure Communication Services Using Python.
Kindly follow these eight steps to resolve the issues:
- Azure Communication Services supports certain audio formats you will need to double-check:
- That the format use WAV files with PCM encoding or MP3.
- Encoding audio file is encoded in a supported codec. For WAV, use Linear PCM with 16-bit depth, and the
- Sample Rate is the recommended sample rate is 16 kHz.
- Use tools like FFmpeg - https://ffmpeg.org/ to verify or convert the audio format using bash:
ffmpeg -i input.wav -ar 16000 -acodec pcm_s16le output.wav
- Ensure the hosted file is:
- Publicly accessible with no authentication required.
- Hosted on a server that serves audio files with correct MIME types (
audio/wav
oraudio/mp3
).
Test the URL:
- Open it in a browser.
- Check server headers using tools like
curl
:curl -I http://example.com/audio.wav
- The ACS Python SDK expects asynchronous operations for call media playback. Use the
play_to_all
parameter to ensure the audio plays for all participants:
from azure.communication.callautomation import FileSource
async def play_audio(call_connection_id, audio_file_path):
try:
audio_url = f"http://example.com/{audio_file_path}" # Public URL
call_connection = call_automation_client.get_call_connection(call_connection_id)
file_source = FileSource(url=audio_url)
# Asynchronously play the media
await call_connection.play_media(play_source=file_source, play_to_all=True)
print(f"Playing audio: {audio_url}")
except Exception as e:
print(f"Error playing audio: {e}")
- Create a debugging for playbacks, if audio does not play:
- Use detailed logs from both the bot and ACS.
- Make sure the
CallConnected
andPlayCompleted
events are correctly handled. - Check if the
PlayCompleted
event is triggered by ACS.
- Before uploading the file to a public server test it:
- Use tools like VLC or Audacity to confirm the file plays correctly.
- Make sure the file has no corruption or encoding issues.
- If using a local server:
- Use a reliable and scalable hosting service like Azure Blob Storage.
- Configure Blob Storage with a public access level using bash command:
az storage blob set-permission --container-name <container> --account-name <account> --public-access container
- Update the Python SDK and Dependencies using bash:
pip install --upgrade azure-communication-callautomation
- Use ACS diagnostic tools to record audio playback and identify issues - https://learn.microsoft.com/en-us/azure/communication-services/resources/troubleshooting/voice-video-calling/references/how-to-collect-diagnostic-audio-recordings
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.