Use o ApsaraVideo Player SDK for iOS para iniciar rapidamente a reprodução de vídeo.
Pré-requisitos
Integre o ApsaraVideo Player SDK for iOS. Para mais informações, consulte Integrate the SDK.
Projeto de demonstração open source
Para exemplos de código, consulte o módulo BasicPlayback em API-Example. Este projeto de demonstração em Objective-C facilita a integração rápida dos principais recursos de reprodução do SDK.
Procedimento
Etapa 1: Crie um player
Crie uma instância do AliPlayer.
// Create a player instance.
AliPlayer mAliPlayer = [[AliPlayer alloc] init];
// Create a view container to display the video and set the rendering view for the player.
UIView playerView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:playerView];
mAliPlayer.playerView = playerView;
[mAliPlayer setTraceID:traceId];
Os recursos Playback quality monitoring, single-point tracking e video playback statistics dependem do relatório de logs de instrumentação. O monitoramento da qualidade de reprodução exibe dados gerais sobre a qualidade da reprodução. O rastreamento ponto a ponto permite localizar um usuário ou dispositivo específico, analisar o comportamento de reprodução e identificar problemas com agilidade.
Os recursos disponíveis variam conforme o parâmetro setTraceID:
Sem o parâmetro
setTraceID(padrão): o recurso de relatório de logs de instrumentação fica ativado. É possível usar o monitoramento da qualidade de reprodução e as estatísticas de reprodução de vídeo, mas o rastreamento ponto a ponto não estará disponível.Com um traceid no parâmetro
setTraceID: o traceid é um valor personalizado que deve ser um identificador exclusivo do usuário ou do dispositivo, como o ID de usuário da sua empresa, o Identificador Internacional de Equipamento Móvel (IMEI) ou o Identificador para Anunciantes (IDFA). Ao fornecer o traceid, você ativa o relatório de logs de instrumentação e habilita o uso do monitoramento da qualidade de reprodução, do rastreamento ponto a ponto e das estatísticas de reprodução de vídeo.Com o parâmetro
setTraceIDdefinido comoDisableAnalytics: o relatório de logs de instrumentação é desativado. Nesse caso, não é possível usar o monitoramento da qualidade de reprodução, o rastreamento ponto a ponto nem as estatísticas de reprodução de vídeo.
Etapa 2: Defina a fonte de reprodução
Reprodução com VidAuth (Recomendado)
AVPVidAuthSource *authSource = [[AVPVidAuthSource alloc] init];
authSource.vid = @"Video ID"; // Required. The video ID.
authSource.playAuth = @"<yourPlayAuth>"; // Required. The playback credential. You must call the GetVideoPlayAuth operation of ApsaraVideo VOD to generate the credential.
authSource.region = @"Region"; // For ApsaraVideo Player SDK V5.5.5.0 and later, this parameter is deprecated. You do not need to set the region because the player automatically parses it. For versions earlier than V5.5.5.0, this parameter is required. It specifies the region where ApsaraVideo VOD is activated. Default value: cn-shanghai.
// authSource.authTimeout = 3600; // The validity period of the playback URL, in seconds. This value overwrites the validity period of URL signing that is set in the ApsaraVideo VOD console. If you do not set this parameter, the default value 3600 is used. If you set this parameter, make sure that its value is greater than the actual video duration to prevent the playback URL from expiring before playback is complete.
// Set the playback source.
[mAliPlayer setAuthSource:authSource];
Reprodução com VidSts
AVPVidStsSource *source = [[AVPVidStsSource alloc] init];
source.vid = @"Video ID"; // Required. The video ID.
source.region = @"Region"; // Required. The region where ApsaraVideo VOD is activated. Default value: cn-shanghai.
source.securityToken = @"<yourSecurityToken>"; // Required. The Security Token Service (STS) token. You must call the AssumeRole operation of STS to generate the token.
source.accessKeySecret = @"<yourAccessKeySecret>"; // Required. The AccessKey secret of the temporary AccessKey pair. You must call the AssumeRole operation of STS to generate the secret.
source.accessKeyId = @"<yourAccessKeyId>"; // Required. The AccessKey ID of the temporary AccessKey pair. You must call the AssumeRole operation of STS to generate the ID.
// source.authTimeout = 3600; // The validity period of the playback URL, in seconds. This value overwrites the validity period of URL signing that is set in the ApsaraVideo VOD console. If you do not set this parameter, the default value 3600 is used. If you set this parameter, make sure that its value is greater than the actual video duration to prevent the playback URL from expiring before playback is complete.
// If you enable HLS encryption parameter pass-through in the ApsaraVideo VOD console and the default parameter name is MtsHlsUriToken, you must set the config and pass it to the vid.
// Set the playback source.
[mAliPlayer setStsSource:source]
Reprodução com UrlSource
// Create a playback source object and set the playback URL.
// The playback URL can be a third-party video-on-demand (VOD) URL, a playback URL from ApsaraVideo VOD, or a local video URL.
AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:@"Playback URL"];
[mAliPlayer setUrlSource:urlSource];
Para obter mais detalhes sobre as fontes de reprodução, consulte Basic features.
Etapa 3: Iniciar a reprodução
// Prepare for playback.
[mAliPlayer prepare];
// After prepare is called, you can synchronously call the start operation. Playback starts automatically after the onPrepared callback is complete.
[mAliPlayer start];
Etapa 4: Parar a reprodução
// Unbind the player view.
mAliPlayer.playerView = nil;
// Stop playback.
[mAliPlayer stop];
// Release resources synchronously. The stop method is automatically called.
[mAliPlayer destroy];
// Clear the reference to prevent memory leaks.
mAliPlayer = nil;
Referências
Para saber mais sobre os recursos do player, como controle de reprodução e listeners de eventos, consulte Basic features.