3.8 Player Module API Manual#

1. Overview#

This document provides a detailed introduction to the K230_CanMV Player module API, which is designed to support the playback of MP4 format files. This module can simultaneously play audio and video, with audio formats supporting G.711A/U and video formats supporting H.264/H.265 encoding.

2. API Introduction#

The module provides the Player class, which includes the following methods:

2.1 Constructor#

Description

Constructs a Player object based on the specified display_type. Users need to create a Player object first to perform subsequent operations.

Syntax

player = Player(Display.VIRT, [display_to_ide])

Parameters

Parameter Name

Description

Input/Output

Notes

display_type

Type of display device

Input

display_to_ide

Whether to output to IDE virtual screen simultaneously

Input

Return Value

Return Value

Description

Player object

Created Player instance

Example

player = Player(Display.VIRT)
player = Player(Display.ST7701)
player = Player(Display.LT9611)

2.2 Player.load#

Description

Loads the specified file. The current version only supports MP4 format files.

Syntax

player = Player()
player.load("test.mp4")

Parameters

Parameter Name

Description

Input/Output

filename

File name

Input

Return Value

Return Value

Description

0

Success

Non-zero

Failure

Note
The current version only supports playing MP4 format files.

2.3 Player.start#

Description

Starts playing audio and video content.

Syntax

player = Player()
player.start()

Parameters

None

Return Value

Return Value

Description

0

Success

Non-zero

Failure

2.4 Player.pause#

Description

Pauses the current playback.

Syntax

player = Player()
player.pause()

Parameters

None

Return Value

Return Value

Description

0

Success

Non-zero

Failure

2.5 Player.resume#

Description

Resumes playback.

Syntax

player = Player()
player.resume()

Parameters

None

Return Value

Return Value

Description

0

Success

Non-zero

Failure

2.6 Player.stop#

Description

Stops playback.

Syntax

player = Player()
player.stop()

Parameters

None

Return Value

Return Value

Description

0

Success

Non-zero

Failure

2.7 Player.set_event_callback#

Description

Sets the callback function for playback events.

Parameters

Parameter Name

Description

Input/Output

callback

Callback function name

Input

Syntax

def player_event(event, data):
    pass

player = Player()
player.set_event_callback(callback=player_event)

Return Value

Return Value

Description

0

Success

Non-zero

Failure

3. Data Structure Description#

3.1 play_event_type#

Description

Defines the types of playback events.

Members

Member Name

Description

K_PLAYER_EVENT_EOF

Playback End

K_PLAYER_EVENT_PROGRESS

Playback Progress

4. Example Program#

4.1 Example 1#

from media.player import *
import os
import time

start_play = False

def player_event(event, data):
    global start_play
    if event == K_PLAYER_EVENT_EOF:
        start_play = False

def play_mp4_test(filename):
    global start_play
    # Use IDE as output display, supports any resolution; suitable for BPI development board
    player = Player(Display.VIRT)
    # Use ST7701 LCD screen as output display, maximum resolution is 800x480
    # player = Player(Display.ST7701)
    # Use HDMI as output display
    # player = Player(Display.LT9611)
    
    player.load(filename)
    player.set_event_callback(player_event)
    player.start()
    start_play = True

    while start_play:
        time.sleep(0.1)

    player.stop()
    print("Playback finished")

play_mp4_test("/sdcard/examples/test.mp4")
Comments list

Comments list

Comments
Log in