This topic describes how to create and use a player for iOS.

Prerequisites

Before you create and use a player for iOS, you must integrate the latest version of Player SDK for iOS. For more information, see Integration.

Workflow

  1. Create a player.
    Create a player of the AliPlayer type.
    AliPlayer *player = [[AliPlayer alloc] init];
  2. Configure the proxy.
    ApsaraVideo Player SDK for iOS provides multiple delegation callback events, such as onPlayerEvent and onError. Example:
    @interface SimplePlayerViewController ()<AVPDelegate>
    @end
    - (void)viewDidLoad {
        self.player = [[AliPlayer alloc] init];
        self.player.playerView = self.avpPlayerView.playerView;
        self.player.delegate = self;
        //...
    }
    /**
     @brief The callback for an invalid delegate.
     @param player The pointer for the player.
     @param errorModel The description of player errors. This parameter is similar to the AliVcPlayerErrorModel parameter.
     */
    - (void)onError:(AliPlayer*)player errorModel:(AVPErrorModel *)errorModel {
        // Indicates that an error occurred and the playback stopped.
    }
    /**
     @brief The callback for player events.
     @param player The pointer for the player.
     @param eventType The event type of the player. This parameter is similar to the AVPEventType parameter.
     */
    -(void)onPlayerEvent:(AliPlayer*)player eventType:(AVPEventType)eventType {
        switch (eventType) {
            case AVPEventPrepareDone: {
                // The completion of preparation.
            }
                break;
            case AVPEventAutoPlayStart:
                // The start of autoplay.
                break;
            case AVPEventFirstRenderedStart:
                // The appearance of the first frame.
                break;
            case AVPEventCompletion:
                // The completion of playback.
                break;
            case AVPEventLoadingStart:
                // The start of buffering.
                break;
            case AVPEventLoadingEnd:
                // The end of buffering.
                break;
            case AVPEventSeekEnd:
                // The end of seeking.
                break;
            case AVPEventLoopingStart:
                // The start of loop playback.
                break;
            default:
                break;
        }
    }
    /**
     @brief The callback for the current playback position.
     @param player The pointer for the player.
     @param position The current playback position.
     */
    - (void)onCurrentPositionUpdate:(AliPlayer*)player position:(int64_t)position {
        // Updates the progress bar.
    }
    /**
     @brief The callback for the buffer position.
     @param player The pointer for the player.
     @ param position The current buffer position.
     */
    - (void)onBufferedPositionUpdate:(AliPlayer*)player position:(int64_t)position {
        // Updates the buffer progress.
    }
    /**
     @ brief The callback to obtain the track information.
     @param player The pointer for the player.
     @param info track The array of track information. This parameter is similar to the AVPTrackInfo parameter.
     */
    - (void)onTrackReady:(AliPlayer*)player info:(NSArray<AVPTrackInfo*>*)info {
        // Obtains the information about different bitrates.
    }
    /**
     @brief The callback to display the subtitle.
     @param player The pointer for the player.
     @param index The index number for the subtitle.
     @param subtitle The string of the subtitle.
     */
    - (void)onSubtitleShow:(AliPlayer*)player index:(int)index subtitle:(NSString *)subtitle {
        // Obtains the subtitle for display.
    }
    /**
     @brief The callback to hide the subtitle.
     @param player The pointer for the player.
     @param index The index number for the subtitle.
     */
    - (void)onSubtitleHide:(AliPlayer*)player index:(int)index {
        // Hides the subtitles.
    }
    /**
     @brief The callback for snapshot capture.
     @param player The pointer for the player.
     @param image The image.
     */
    - (void)onCaptureScreen:(AliPlayer *)player image:(UIImage *)image {
        // Previews and saves the snapshot.
    }
    /**
     @brief track The callback to indicate the completion of switching.
     @param player The pointer for the player.
     @param info The information about the switching after the switching is complete. This parameter is similar to the AVPTrackInfo parameter.
     */
    - (void)onTrackChanged:(AliPlayer*)player info:(AVPTrackInfo*)info {
        // Notifies the bitrate after switching.
    }
  3. Configure the playback source and prepare for playback.
    ApsaraVideo Player SDK for iOS supports the following playback sources:
    • AVPVidStsSource
    • AVPVidAuthSource
    • AVPVidMpsSource
    • AVPUrlSource
    Note You can use AVPUrlSource for playback based on URLs, or the other three playback sources for playback based on video IDs (VIDs). We recommend that you use AVPVidAuthSource for ApsaraVideo VOD. AVPVidMpsSource is only available in ApsaraVideo for Media Processing.
    In this example, AVPVidStsSource is used.
     // Creates the AVPVidStsSource playback source.
        AVPVidStsSource *source = [[AVPVidStsSource alloc] init];
        source.region = self. Access region;
        source.vid = self. VID;
        source.securityToken = self. Security token;
        source.accessKeySecret = self. Temporary AccessKey secret;
        source.accessKeyId = self. Temporary AccessKey ID;
         // Specifies the playback source.
        [self.player setStsSource:source]
  4. Configure the user interface (UI) view.
    If the playback source contains video images, you must configure the UI view to display the video images in the player. Example:
    self.player.playerView = self.avpPlayerView.playerView;// The UI view that is displayed.
  5. Prepare the playback.
    [self.player prepare];
  6. Complete the preparation.
    After the prepare method succeeds, the callback for -(void)onPlayerEvent:(AliPlayer*)player eventType:(AVPEventType)eventType is invoked. eventType is AVPEventPrepareDone.
    Note If the prepare method fails, the callback for - (void)onError:(AliPlayer*)player errorModel:(AVPErrorModel *)errorModel is invoked.
  7. Start the playback.
    Call the start method to start video playback.
    [self.player start];
  8. Pause the playback.
    Call the pause method to pause video playback. After you pause the playback of a video, you can call the start method to resume the playback.
    [self.player pause];
  9. Stop the playback.
    Call the stop method to stop video playback.
    [self.player stop];; // You can call this method at any time, especially when an onError event occurs.
  10. Release the player.
    If you no longer use a player, you need to call the destroy method to release the memory of the player. The player cannot be used after being released. If you want to use a player again, you need to recreate a player.
    [self.player destroy];
    self.player = nil;