# Correct way of playing sound contained within a sound tag

**URL:** <https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118>\
**Category:** Development\
**Created:** [April 9, 2021, 10:25am UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118 "2021-04-09T10:25:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucw](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/lucw/32/29840_2.png) [@lucw](https://forums.ankiweb.net/u/lucw)\
**Post date:** [April 9, 2021, 10:25am UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118/1 "2021-04-09T10:25:06Z")

</div>

What’s the right way of playing sounds contained within a sound tag in a field ?  
The following code fields the warning “Accessing the backend directly will break in the future. Please use the public methods on Collection instead.”. I don’t see an equivalent of this method on the collection object.

```
def play_anki_sound_tag(self, text):
    out = aqt.mw.col.backend.extract_av_tags(text=text, question_side=True)
    file_list = [
        x.filename
        for x in anki.template.av_tags_to_native(out.av_tags)
        if isinstance(x, anki.sound.SoundOrVideoTag)
    ]   
    if len(file_list) >= 1:
        filename = file_list[0]
        aqt.sound.av_player.play_file(filename)
```

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [April 9, 2021, 11:28am UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118/2 "2021-04-09T11:28:17Z")

</div>

Can’t you use card.question/answer\_av\_tags()?

---

<div class="post-metadata">

**Author:** ![lucw](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/lucw/32/29840_2.png) [@lucw](https://forums.ankiweb.net/u/lucw)\
**Post date:** [April 9, 2021, 11:38am UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118/3 "2021-04-09T11:38:14Z")

</div>

I use this inside the editor, a field which is registered as type “sound” will have a play button. (my addon Language Tools associates a language with each field, and sound fields are also registered that way)

 ![](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/b/b38728b9b7308668827e6ae00f051fd446048969.png)  
don’t worry too much about this use case as this particular feature is not super important.

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [April 9, 2021, 1:31pm UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118/4 "2021-04-09T13:31:32Z")

</div>

I have created and am currently using a small add-on that allows me to play `[sound:...]` tag files in the editor from the right-click context menu, similar to that feature of your add-on. I’m not sure if it’s a correct way, but in my add-on I use regular expressions to extract the sound tags. Here is the source code.

```python
import re
from pathlib import Path
import anki
import aqt

# https://fonts.google.com/icons
PLAY_ICON = str(
    Path( __file__ ).parent.resolve() / "icon" / "play_circle_outline-24px.svg"
)

def get_inverted_icon(icon):
    """ for Night Mode """
    img = icon.pixmap(32).toImage()
    img.invertPixels()
    new_icon = aqt.qt.QIcon(aqt.qt.QPixmap.fromImage(img))
    return new_icon

def on_editor_will_show_context_menu(
    editor_webview: aqt.editor.EditorWebView, menu: aqt.qt.QMenu # type: ignore
):
    assert aqt.mw is not None
    note: anki.notes.Note = editor_webview.editor.note # type: ignore
    sound_regex = aqt.mw.col.media.sound_regexps[0]
    for match in re.finditer(sound_regex, note.joinedFields()):
        filename = match.group("fname")
        truncated_filename = f"...{filename[-24:]}" if len(filename) > 24 else filename
        icon = aqt.qt.QIcon(PLAY_ICON) # type: ignore
        if aqt.mw.pm.night_mode():
            icon = get_inverted_icon(icon)
        menu.addAction(
            icon,
            truncated_filename,
            lambda f=filename: aqt.sound.av_player.play_file(f),
        )

aqt.gui_hooks.editor_will_show_context_menu.append(on_editor_will_show_context_menu)

```

![play_sound_in_editor](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/2/203085e13f207c27f6fc0b4e62de191003008a89.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:18am UTC](https://forums.ankiweb.net/t/correct-way-of-playing-sound-contained-within-a-sound-tag/9118/5 "2023-05-01T03:18:56Z")

</div>


