MusicStore provides a complete set of music playback management APIs, including starting/pausing/resuming/stopping playback and volume control. Through this class, BGM playback functionality can be implemented in live rooms.MusicStore instance, passing a valid live room ID.Property | Type | Description |
playURL | String? | Currently playing music URL. nil/null means no music is playing. |
playStatus | Current playback status. | |
playProgress | Int | Current playback progress (unit: milliseconds). |
totalDuration | Int | Total duration of current music (unit: milliseconds). 0 means unknown. |
musicVolume | Int | BGM volume, range 0 - 100, default 60. |
Function | Description |
Create music playback management instance. | |
Music playback event publisher. | |
Start playing music. | |
Pause playback. | |
Resume playback. | |
Stop playback. | |
Seek to specified position. | |
Set BGM volume. | |
Set pitch adjustment. |
public static func create(liveID: String) -> MusicStore {let store: MusicStoreImpl = StoreFactory.shared.getStore(roomID: liveID)return store}
Parameter | Type | Description |
liveID | String | Live room ID. |
public func startPlay(param: AudioMusicParam, completion: CompletionClosure?) {fatalError("\\(#function) must be overridden by subclass")}
param. If a network URL is provided in AudioMusicParam/path, the SDK will handle downloading internally. The playback status will transition to LOADING first, then to PLAYING when playback actually starts. Error Codes: On failure, the completion callback will return TRTC SDK error codes. See the error code table in onPlayError for details.Parameter | Type | Description |
param | Music playback parameters. See AudioMusicParam. | |
completion | Completion callback (success/failure status). Returns TRTC error code on failure. |
public func pausePlay() {fatalError("\\(#function) must be overridden by subclass")}
public func resumePlay() {fatalError("\\(#function) must be overridden by subclass")}
public func stopPlay() {fatalError("\\(#function) must be overridden by subclass")}
public func seek(to ms: Int) {fatalError("\\(#function) must be overridden by subclass")}
Parameter | Type | Description |
ms | Int | Target playback position in milliseconds. |
public func setMusicVolume(_ volume: Int) {fatalError("\\(#function) must be overridden by subclass")}
Parameter | Type | Description |
volume | Int | BGM volume, range 0 - 100. |
public func setPitch(_ pitch: Double) {fatalError("\\(#function) must be overridden by subclass")}
Parameter | Type | Description |
pitch | Double | Pitch adjustment value, range -12 ~ 12. Positive values raise the pitch, negative values lower it. |
Enum Value | Value | Description |
idle | 0 | Idle, not playing. |
playing | 1 | Playing. |
paused | 2 | Paused. |
loading | 3 | Loading (e.g. buffering network resource). |
Property | Type | Description |
playURL | String? | Currently playing music URL. nil/null means no music is playing. |
playStatus | Current playback status. | |
playProgress | Int | Current playback progress (unit: milliseconds). |
totalDuration | Int | Total duration of current music (unit: milliseconds). 0 means unknown. |
musicVolume | Int | BGM volume, range 0 - 100, default 60. |
Property | Type | Description |
id | Int | Music ID. Since the SDK allows playing multiple tracks at the same time, this ID is used to
mark a track for start/stop/volume control, etc. When 0, an ID will be generated internally
based on path. |
path | String? | Full path or URL of the audio file. Supported formats: MP3, AAC, M4A, WAV. |
loopCount | Int | Number of times to loop the track. 0 means play once without looping. |
case playCompleted(playURL: String)
Parameter | Type | Description |
playURL | String | The music URL that finished playing. |
case playError(playURL: String, code: Int)
Parameter | Type | Description |
playURL | String | The music URL that encountered error. |
code | Int | Error code, passed through from TRTC SDK. Common error codes are as follows: 0 (Success): Operation succeeded -4001 (ERR_BGM_OPEN_FAILED): Failed to open file, invalid audio data or FFMPEG protocol not found -4002 (ERR_BGM_DECODE_FAILED): Audio file decoding failed, file may be corrupted or encoding format unrecognized -4003 (ERR_BGM_OVER_LIMIT): Number of preloaded music exceeded the limit -4004 (ERR_BGM_INVALID_OPERATION): Invalid operation, e.g., calling preload during playback -4005 (ERR_BGM_INVALID_PATH): Invalid path, please check if the file path points to a valid music file -4006 (ERR_BGM_INVALID_URL): Invalid URL, please ensure the URL is accessible (iOS/Mac requires HTTPS) -4007 (ERR_BGM_NO_AUDIO_STREAM): No audio stream, please confirm the file is a valid audio file and not corrupted -4008 (ERR_BGM_FORMAT_NOT_SUPPORTED): Format not supported. Mobile supports: mp3, aac, m4a, wav, ogg, mp4, mkv. Desktop supports: mp3, aac, m4a, wav, mp4, mkv. -4009 (ERR_CONCURRENT_BGM_OVER_LIMIT): Number of concurrent BGM playback exceeded the limit (max 10). |
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários