すべてのプロダクト
Search
ドキュメントセンター

:基本機能

最終更新日:Oct 28, 2024

このトピックでは、ApsaraVideo Player SDK for Windowsの基本機能を設定する方法について説明します。

再生機能

プレーヤーを作成する

このセクションでは、ApsaraVideo Player SDK for Windowsを使用してビデオを再生する方法について説明します。 手動再生と自動再生がサポートされています。

  1. プレイヤーを作成します。

    AliPlayerオブジェクトを作成します。

    using namespace alivc_player;
    AliPlayer* mPlayerPtr = AliPlayer::CreatePlayer();
  2. リスナーを設定します。

    ApsaraVideo Player SDK for Windowsは、onPlayerEventやonErrorなどの複数のイベントのコールバックをサポートしています。 イベントコールバック機能を有効にするには、IAVPListenerを継承するクラスを作成し、そのクラスの純粋な仮想関数を実装します。 サンプルコード:

    mPlayerPtr->setListener(new AVPListenerImpl);
  3. 再生ソースを設定します。

    • ApsaraVideo Player SDK for Windowsは、UrlSource、VidAuth、VidStsに基づくビデオオンデマンド (VOD) 再生をサポートしています。 ApsaraVideo VODでVidAuthに基づいてビデオを再生することを推奨します。

    • ApsaraVideo Player SDK for Windowsは、UrlSourceベースのライブストリーミングのみをサポートします。

    オンデマンドビデオの再生

    UrlSourceベースの再生

    UrlSourceに基づいてオンデマンドビデオを再生する場合は、setUrlパラメーターをビデオの再生URLに設定する必要があります。 再生URLは、ApsaraVideo VODまたはサードパーティサービスによって生成されます。

    GetPlayInfo操作を呼び出して、ApsaraVideo VODに保存されているメディアファイルの再生URLを取得できます。 サーバーSDKを使用して、ApsaraVideo VODでメディア再生URLを取得することを推奨します。 これにより、複雑な署名計算から解放されます。 GetPlayInfo操作の詳細については、 OpenAPI Explorerを使用します。

    AVPUrlSource urlSource;
    urlSource.setUrl("media url");  // The playback URL that is generated by ApsaraVideo VOD or a third-party service. 
    mPlayerPtr->setSource(urlSource);

    (推奨) VidAuthベースの再生

    VidAuthに基づいてオンデマンドビデオを再生する場合は、vidパラメーターをメディアファイルのIDに設定し、playAuthパラメーターを再生資格情報に設定する必要があります。

    • ApsaraVideo VODにメディアファイルをアップロードした後、ApsaraVideo VODコンソールにログインし、[メディアファイル] > [オーディオ /ビデオ] を選択してメディアIDを表示します。または、SearchMedia操作を呼び出してメディアIDを取得することもできます。

    • GetVideoPlayAuth操作を呼び出して、再生資格情報を取得できます。 サーバーSDKを使用して、ApsaraVideo VODでのメディア再生用の資格情報を取得することを推奨します。 これにより、複雑な署名計算から解放されます。 GetVideoPlayAuth操作の詳細については、 「OpenAPI Explorer」をご参照ください。

    ApsaraVideo VODでのビデオ再生にはVidAuthを使用することを推奨します。 STSベースの再生と比較して、VidAuthベースの再生は使いやすく、より安全です。 2つの再生方法の違いの詳細については、「資格情報とSTSの比較」をご参照ください。

    AVPVidAuthSource authSource;
    authSource.initWithVid("Video ID",
        "playAuth",  // The playback credential.
         "Access region ID");  // The ID of the region from which you want to access ApsaraVideo VOD. Default value: cn-shanghai.
    mPlayerPtr->setSource(authSource);

    VidStsベースの再生

    VidStsに基づいてオンデマンドビデオを再生する場合、再生資格情報の代わりに一時的なSTSトークンが使用されます。 この場合、ビデオをオンデマンドで再生する前にSTSトークンを取得する必要があります。 STSトークンの取得方法の詳細については、「STSを使用した動画のアップロード」をご参照ください。

    VidStsに基づいてオンデマンドビデオを再生する場合は、securityTokenを一時的なSTSトークンに設定し、一時的なAccessKeyペアを指定する必要があります。

    AVPVidStsSource vidSource;
    vidSource.initWithVid("Video ID",   // The video ID.
    "<yourAccessKeyId>",   // The AccessKey ID used for authentication.
    "<yourAccessKeySecret>",  // The AccessKey secret used for authentication.
     	"<yourSecurityToken>",   // The security token.
    "region",   // The ID of the region from which you want to access ApsaraVideo VOD.
    nullptr);
    mPlayerPtr->setSource(vidSource);

    UrlSourceベースのライブストリーミング

    UrlSourceに基づいてライブストリームを再生する場合は、setUrlをライブストリームのストリーミングURLに設定する必要があります。 ストリーミングURLは、ApsaraVideo Liveまたはサードパーティサービスによって生成されます。

    ApsaraVideo liveコンソールのURLジェネレーターを使用して、ライブストリームのストリーミングURLを生成できます。 詳細については、次をご参照ください: URLジェネレーター

    AVPUrlSource urlSource;
    urlSource.setUrl("media url");  // The streaming URL that is generated by ApsaraVideo Live or a third-party service. 
    mPlayerPtr->setSource(urlSource);
  4. ビューを設定します。

    再生ソースにビデオ画像が含まれている場合は、プレーヤーにビデオ画像を表示するようにビューを設定する必要があります。 サンプルコード:

    mPlayerPtr->setView((void *) The HWND of the view);
  5. プレイヤーを準備します。

    prepare() メソッドを呼び出して、プレイヤーを準備します。

    mPlayerPtr->prepare();
  6. (オプション) 自動再生機能を有効にします。 デフォルトでは、この機能は無効になっています。

    mPlayerPtr->setAutoPlay(true);
  7. 再生を開始します。

    • 自動再生機能が有効になっていない場合は、OnPrepardコールバックでmPlayerPtr->start(); を呼び出して再生を開始する必要があります。

    • 自動再生機能が有効になっている場合、mPlayerPtr->start(); を呼び出す必要はありません。 データが解析された後、ビデオは自動的に再生されます。

    mPlayerPtr->start();

メディア再生の管理

ApsaraVideo Player SDK for Windowsは、再生、一時停止、指定された時点からの再生などの基本的な再生制御機能をサポートしています。

再生を開始

startメソッドを呼び出して再生を開始できます。 サンプルコード:

mPlayerPtr->start();

特定の時点からビデオを再生する

seekToTimeを呼び出して、指定した時点からビデオを再生できます。 この機能は、ユーザーがプログレスバーのスライダーをドラッグするとき、または特定の時点からビデオを再開するときに使用されます。 サンプルコード:

// Seek to a position at a specific point in time. 
// Valid values of the mode parameter: AVP_SEEKMODE_ACCURATE and AVP_SEEKMODE_INACCURATE. AVP_SEEKMODE_ACCURATE indicates accurate seeking and AVP_SEEKMODE_INACCURATE indicates inaccurate seeking. Inaccurate seeking navigates you to the nearest next keyframe of the video. Accurate seeking navigates you to the video image at the specified point in time. Accurate seeking takes a longer time than imprecise seeking. 
mPlayerPtr->seekToTime(int64_t time_in_ms, mode);

再生の一時停止

pauseメソッドを呼び出して、再生を一時停止できます。 サンプルコード:

mPlayerPtr->pause();

再生の停止

stopメソッドを呼び出して、再生を停止できます。 サンプルコード:

mPlayerPtr->stop();

ビデオ表示モードを設定する

ApsaraVideo Player SDK for Windowsは、スケーリングモード、回転角度、ミラーリングモードなどの表示設定をサポートしています。

スケーリング

setScaleModeを呼び出して、元のアスペクト比を変更せずにビデオをスケールインまたはスケールアウトしたり、ビデオをストレッチしたりできます。 サンプルコード:

// Scale in the video to fit the view. The aspect ratio of the video is maintained.
mPlayerPtr->setScalingMode(AVP_SCALINGMODE_SCALEASPECTFIT);
// Scale out the video to fill the view. The aspect ratio of the video is maintained.
mPlayerPtr->setScalingMode(AVP_SCALINGMODE_SCALEASPECTFILL);
// Stretch the video image to fill the view. If the aspect ratio of the video image is different from that of the view, the image may be distorted.
mPlayerPtr->setScalingMode(AVP_SCALINGMODE_SCALETOFILL);

回転

setRotateModeを呼び出して、ビデオ画像の回転角度を指定できます。 回転角度を照会することもできます。 サンプルコード:

// Set the rotation angle to 0° in the clockwise direction.
mPlayerPtr->setRotateMode(AVP_ROTATE_0);
// Set the rotation angle to 90° in the clockwise direction.
mPlayerPtr->setRotateMode(AVP_ROTATE_90);
// Set the rotation angle to 180° in the clockwise direction.
mPlayerPtr->setRotateMode(AVP_ROTATE_180);
// Set the rotation angle to 270° in the clockwise direction.
mPlayerPtr->setRotateMode(AVP_ROTATE_270);
// Query the rotation angle.
mPlayerPtr->getRotateMode();

ミラーリング

setMirrorModeを呼び出して、ミラーリングモードを指定できます。 水平ミラーリング、垂直ミラーリング、およびミラーリングなしがサポートされています。 サンプルコード:

// Specify no mirroring for video images.
mPlayerPtr->setMirrorMode(AVP_MIRRORMODE_NONE);
// Specify horizontal mirroring for video images.
mPlayerPtr->setMirrorMode(AVP_MIRRORMODE_HORIZONTAL);
// Specify vertical mirroring for video images.
mPlayerPtr->setMirrorMode(AVP_MIRRORMODE_VERTICAL);       

再生情報の取得

ApsaraVideo Player SDK for Windowsでは、現在の再生進行状況や再生時間の合計などの再生情報を取得できます。

再生の進行状況の取得

onCurrentPositionUpdateコールバックが呼び出された後、現在の再生位置を取得できます。 単位:ミリ秒。 サンプルコード:

void AlivcLivePlayerMainDlg::onCurrentPositionUpdate(AliPlayer *player, int64_t position)
{
    // The position parameter indicates the current playback position. Unit: milliseconds.
printf("current position is %lld ms", position);
}

再生時間の取得

ビデオの合計時間を取得できます。 AVPEventPrepareDoneイベントがトリガーされた後、ビデオの合計期間がdurationパラメーターで返されます。 サンプルコード:

void AlivcLivePlayerMainDlg::onPlayerEvent(AliPlayer *player, AVPEventType eventType)
{
    if (eventType == AVPEventPrepareDone) {
        int64_t duration = mPlayerPtr->getDuration();
printf("total duration is %lld ms", duration);
    }
}

バッファリング位置を取得する

onBufferedPositionUpdateコールバックのpositionパラメーターから、ビデオの現在のバッファリング位置を取得できます。 サンプルコード:

void AlivcLivePlayerMainDlg::onBufferedPositionUpdate(AliPlayer *player, int64_t position)
{
    printf("buffered position is %lld ms", position);
}

プレイヤーステータスの取得

onPlayerStatusChangedコールバックからプレーヤーのステータスを取得できます。 サンプルコード:

void AlivcLivePlayerMainDlg::onPlayerStatusChanged(AliPlayer *player, AVPStatus oldStatus, AVPStatus newStatus)
{
    switch (newStatus) {
case AVPStatusIdle:{
//  The player is idle.
}
break;
case AVPStatusInitialzed:{
//  The player is initialized.
}
 break;
     case AVPStatusPrepared:{
//  The player is prepared.
}
break;
case AVPStatusStarted:{
//  The player is playing a video.
}
 break;
case AVPStatusPaused:{
// The playback is paused.
}
break;
case AVPStatusStopped:{
//  The playback is stopped.
 }
break;
case AVPStatusCompletion:{
//  The playback is complete.
 }
break;
case AVPStatusError:{
//  A playback error occurs.
 }
break;
default:
break;
}
}

ボリュームの設定

ApsaraVideo Player SDK for Windowsでは、ビデオの音量を変更したり、ビデオをミュートしたりできます。

ボリュームの変更

setVolumeを呼び出して音量を変更できます。 現在のボリュームを取得することもできます。 サンプルコード:

// Set the volume to a number from 0 to 2. 
mPlayerPtr->setVolume(1.0f);
// Obtain the volume information. 
mPlayerPtr->getVolume();

ミュートモードの設定

setMuteを呼び出して、再生中のビデオをミュートできます。 サンプルコード:

mPlayerPtr->setMute(true);

再生速度の設定

ApsaraVideo Player SDK for Windowsでは、再生速度を設定できます。 rateメソッドを呼び出して、再生速度を0.5 × から2 × に変更できます。 オーディオピッチは、異なる速度で変化しないままです。 サンプルコード:

// Playback speeds ranging from 0.5x to 2x are supported. Common playback speeds are multiples of 0.5x, such as 0.5x, 1x, and 1.5x.
mPlayerPtr->setRate(1.5);

マルチ定義設定の構成

VidAuthまたはVidStsに基づいてビデオを再生する場合、再生のためにマルチ定義設定を構成する必要はありません。 VidAuthに基づいてビデオを再生することを推奨します。 ApsaraVideo Player SDK for Windowsは、ApsaraVideo VODからビデオ定義を自動的に取得します。 ビジネス要件に基づいてビデオ定義を照会および切り替えることができます。 UrlSourceベースの再生では、マルチ定義設定を構成できません。

ビデオ定義の取得

ビデオがロードされたら、ビデオ定義を取得できます。

void AlivcLivePlayerMainDlg::onTrackReady(AliPlayer *player, AVPTrackInfo *info[], int count)
{
    if (count < 0) {
        return;
    }

    for (int i = 0; i < count; i++) {
        AVPTrackInfo *track = info[i];
        switch (track->trackType) {
            case AVPTRACK_TYPE_VIDEO: {
                int trackBitrate = track->trackBitrate;
            } break;
        }
    }
}

Switch between definitions

selectTrackメソッドを呼び出して、ビデオを再生する定義に対応するインデックスにTrackInfoを設定できます。

mPlayerPtr->selectTrack(trackIndex);

定義の変更に関する通知の送信

正常に定義を切り替えるためのコールバックを設定します。

void AlivcLivePlayerMainDlg::onTrackChanged(AliPlayer *player, AVPTrackInfo *info)
{
    // The definition is switched.
}

ループ再生の有効化

ApsaraVideo Player SDK for Windowsはループ再生機能をサポートしています。 setLoopメソッドを呼び出して、ループ再生を有効にします。 ループ再生機能を使用すると、ビデオの再生が終了した後、最初からビデオを再生できます。 AVPEventLoopingStartコールバックは、ループ再生の開始時に返されます。 サンプルコード:

mPlayerPtr->setLoop(true);