【新手基础教程】多媒体audio(音频)的使用
本帖最后由 猪百岁 于 2021-4-21 17:03 编辑# audio(音频) 的使用
详细 API 参考:(https://cn.maixpy.sipeed.com/zh/api_reference/media/audio.html)
## 1. 使用方法[](https://cn.maixpy.sipeed.com/zh/course/media/audio.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95)
- 创建 audio 对象
```
import audio
player = audio.Audio(path = "/sd/6.wav")
```
- 创建 I2S 对象(用于处理音频对象)
```
from Maix import I2S
# init i2s(i2s0)
wav_dev = I2S(I2S.DEVICE_0)
# config i2s according to audio info
wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)
```
- 获取 audio 对象信息并关联 I2S 对象
```
# read audio info
wav_info = player.play_process(wav_dev)
print("wav file head information: ", wav_info)
```
- 根据 audio 信息配置 I2S 对象
```
sample_rate = wav_info
wav_dev.set_sample_rate(sample_rate)
```
- 使用已关联的 I2S 对象播放音频
```
# loop to play audio
while True:
ret = player.play()
if ret == None:
print("format error")
break
elif ret==0:
print("end")
break
```
- 结束播放
```
player.finish()
```
## 2. 例程[](https://cn.maixpy.sipeed.com/zh/course/media/audio.html#%E4%BE%8B%E7%A8%8B)
> 测试音频地址:(https://github.com/sipeed/MaixPy_scripts/blob/master/multimedia/audio/6.wav)
- 播放 wav 文件:(https://github.com/sipeed/MaixPy_scripts/blob/master/multimedia/audio/play_wav.py)
- 录制音频为 wav 文件并保存:(https://github.com/sipeed/MaixPy_scripts/blob/master/multimedia/audio/record_wav.py)
页:
[1]