百度智能云

All Product Document

          Multimedia Cloud Processing

          Quick Advancement

          Release the Player Correctly

          To ensure the stable and efficient operation of App, it is of great importance to release the player correctly.

          Currently, the mainstream interaction is basically shown as follows:

          1.The user click one of the video in the video list and push Play UI to UINavigationController; 2.Play UIstarts loading the video and play; 3.The user clicks “back” button to exit playing, play UI from UINavigationController pop Drop.

          Step 3 is very important. It introduces the time and process to release players: 
          
          - When the user clicks back button, only the `stop` method of the player is called;
          
              ```objectivec
              
              - (IBAction)onBackButton:(id)sender {
                  [self.player stop];
              }
              ```
          
          - After the player sends the `BDCloudMediaPlayerPlaybackDidFinishNotification ` notification, modify the `Playback UI ` from`UINavigationController ` **pop** Drop.  
          
              ```objectivec
              // Register notifications
              - (void)setupNotifications {
                  [[NSNotificationCenter defaultCenter] addObserver:self
                  	                                             selector:@selector(onPlayerFinish:)
                  	                                                 name:BDCloudMediaPlayerPlaybackDidFinishNotification
                  	                                               object:nil];
                  	}
          
                  	// Pop after Finish notification
              - (void)onPlayerFinish:(NSNotification*)notification {
                  	    // If there are multiple player instances simultaneously, you’d better judge whether the object in the notification is the same as the carried one.
                  	    if (notification.object != self.player) {
                  	        return;
                  	    }
                      
                  	    // pop UI
                  	    [self.navigationController popViewControllerAnimated:YES];
                  	}
          
              ```
          
          This process can ensure that the relevant resources inside the player for this play have been released in case of exiting from ‘play UI’. It is helpful to reuse the player instance to initiate the next play.

          Video player process bar includes the following three information:

          • Total duration of the videoduration
          • Current play timecurrentPlaybackTime
          • Current buffer timeplayableDuration

          After receiving BDCloudMediaPlayerPlaybackIsPreparedToPlayNotification notification, you can get the duration to confirm the maximumValue (the value of the live video may be 0) of the process bar. Enable NSTimer to trigger once every second to access currentPlaybackTime and playableDuration in real time, and update the progress bar.

          Note: No process bar with buffering process is available in the IOS system, the users should realize by themselves.

          Play the Next Video

          When playing a video, if you want to give up the current play and immediately play another video in the current Play UI, operate according to the following ways.

          - (IBAction)onPlayNext:(id)sender {
              // Stop playing the current video
              [self.player stop];
              
              // Reset the player to the original status.
              [self.player reset];
              
              // Start the initialization and play procedure of the next video. 
              self.player.contentString = @"<next url>";
              self.player.shouldAutoplay = YES;
              [self.player prepareToPlay];
          }

          APM Access

          APM SDK Support data monitoring, sending, backstage report and display capacities. With powerful real-time capability, it can report the stutter, network speed and other information in a timely manner that the user encounters during VoD and live broadcast, so as to facilitate the operator to adjust the strategy and schedule in time.

          SDK does not directly rely on APM SDK, dynamically check whether the App accesses to APM SDK when operating.

          Multi-rate Switchover

          Rapid switch of multiple code rates

          When an HLS multi-code rate video is played, the player supports to switch the code rate in real time during the playback. Specific steps are as follows:

          1.After receiving BDCloudMediaPlayerPlaybackIsPreparedToPlayNotification notification, call the interface for getting multi-rate list; 2.Display the multiple code rate on play UI for users to select the code rate;

          Example of codes in the above two steps:  
           - (void)onPlayerPrepared:(NSNotification*)notification {
               // If there are multiple player instances simultaneously, you’d better judge whether the object in the notification is the same as the carried one.
               if (notification.object != self.player) {
                   return;
               }
               
               // Get multi-code rate lists.
               NSArray<BDCloudMediaPlayerBitrateItem*>* items = [self.player getSupportedBitrates];
               
               // Display the code rate list on UI.
               [self showBitrateList:items];
           }

          3.When the user select code rate, he calls setBitrateIndex: method to set code rate index:

           - (IBAction)onChangeBitrate:(id)sender {
               NSInteger index = <bitrateIndex>;
               [self.player setBitrateIndex:index];
           }

          Multi-rate seamless switchover

          The player supports not only HLS multi-rate rapid switchover but also the seamless switchover feature of mainstream media formats such as HLS and MAP4. Specific steps are as follows:

          HLS format multi-rate seamless switching of the Master playlist

          1.Set the input format

           - (void)setMediaInputType:(NSInteger)inputType {
               [self.player setMediaItemsInputType:inputType];
           }

          2.After receiving BDCloudMediaPlayerPlaybackIsPreparedToPlayNotification notification, call the interface for getting multi-rate list to display in UI.

           - (void)onPlayerPrepared:(NSNotification*)notification {
               // If there are multiple player instances simultaneously, you’d better judge whether the object in the notification is the same as the carried one.
               if (notification.object != self.player) {
                   return;
               }
               
               // Get multi-code rate lists.
               NSArray* bitrates = [self.player getMediaItems];
               
               // Get the current code rate index 
               int index = (int)[self.player mediaItemIndex];
               
               //  Display the code rate list on UI.
               [self.actions updateBitrateList:bitrates index:index];
           }

          3.The user switches to certain code rate according to the corresponding index.

           - (void)changeBitrate:(NSInteger)index {
               [self.player setMediaItemIndex:index];
           }

          4.When receiving BDCloudMediaPlayerMediaChangeStartNotification notification, it means the video starts switchover. 5.When receiving BDCloudMediaPlayerMediaChangeEndNotification notification, it means the switchover is over, and the value of BDCloudMediaPlayerMediaChangeEndResultUserInfoKey can be taken to judge whether the switchover is successful. After playing the player cache, the new code rate display is rapidly switched. 6.When receiving BDCloudMediaPlayerNaturalSizeChangedNotification notification, it means the video resolution ratio changes.

          Seamless switching of the non-nested code rate as MP4

          1.Set the input format

           - (void)setMediaInputType:(NSInteger)inputType {
               [self.player setMediaItemsInputType:inputType];
           }

          2.Set multi-code rate video link (note the difference between HLS and it)

           - (void)setMediaInputList:(NSArray *)inputList {
               [self.player setMediaItems:inputList];
           }

          3.After receiving BDCloudMediaPlayerPlaybackIsPreparedToPlayNotification notification, call the interface for getting multi-rate list to display in UI.

           - (void)onPlayerPrepared:(NSNotification*)notification {
               // If there are multiple player instances simultaneously, you’d better judge whether the object in the notification is the same as the carried one.
               if (notification.object != self.player) {
                   return;
               }
               
               // Get multi-code rate lists.
               NSArray* bitrates = [self.player getMediaItems];
               
               // Get the current code rate index
               int index = (int)[self.player mediaItemIndex];
               
               // Display the code rate list on UI.
               [self.actions updateBitrateList:bitrates index:index];
           }

          4.The user switches to certain code rate according to the corresponding index.

           - (void)changeBitrate:(NSInteger)index {
               [self.player setMediaItemIndex:index];
           }

          5.When receiving BDCloudMediaPlayerMediaChangeStartNotification notification, it means the video starts switchover. 6.When receiving BDCloudMediaPlayerMediaChangeEndNotification notification, it means the switchover is over, and the value of BDCloudMediaPlayerMediaChangeEndResultUserInfoKey can be taken to judge whether the switchover is successful. After playing the player cache, the new code rate display is rapidly switched. 7.When receiving BDCloudMediaPlayerNaturalSizeChangedNotification notification, it means the video resolution ratio changes.

          Play the HLS Encrypted Video

          Baidu Media Cloud MCT The service supports transcoding into HLS encrypted video.

          With different encryption methods, the method for playing is slightly different:

          • PlayerBinding encryption mode Play as common videos.
          • Custom Playerld and PlayerKey mode

            Set `playerId` and `playerKey` before playing
              (void)play {
                  // First set `playerId` and `playerKey`
                  [self.player setPlayerID:@"<playerId>" key:@"<playerKey>"];
                  
                  // Play the video again.
                  self.player.contentString = @"<url>";
                  self.player.shouldAutoplay = YES;
                  [self.player prepareToPlay];
              }
          - Token mode:
          
               Set `token` before playing
          
          ```objectivec
             (void)play {
                 // Set `token` first.
                 [self.player setToken:@"<token>"];
                 
                 // Play the video again.
                 self.player.contentString = @"<url>";
                 self.player.shouldAutoplay = YES;
                 [self.player prepareToPlay];
             }    

          Set the Header of HTTP Requests

          If you need to set common HTTP Header fields, such as User-Agent, you can use interface setOptionValue.

          NSString* value = @"User-Agent: <your user agent>";
          [player setOptionValue:value
                          forKey:@"headers"
                      ofCategory:BDCloudMediaPlayerOptionCategoryFormat];

          Note:

          If you need to set multiple HTTP Header fields, you need to connect multiple fields by using \r\n according to HTTP specification.

          Download HLS Video

          Besides providing video play feature, SDK also supports download of HLS videos. The class BDCloudMediaDownload provides download-related interface.

          Download of the common HLS videos

          The following introduces the basic procedures of the download:

          1.Realize BDCloudMediaDownloadDelegate to respond to the status change callback for the download task

              // Start the task
              - (void)taskStart:(BDCloudMediaDownloadTask*)task {
              }
          
              // Task progress report
              - (void)task:(BDCloudMediaDownloadTask*)task progress:(float)progress { 
                  NSLog(@"task%@, download process %.2f", task, progress);
              }
          
              // The task is complete, and when error is nil, it indicates a successful download.
              - (void)taskEnd:(BDCloudMediaDownloadTask*)task error:(NSError*)error {
                  if (!error) {
                       NSLog(@"Download succeeded!");
                      // Get the local path of the offline video, and offline play can be realized after getting it.
                      NSString* path = [task.item.path stringByAppendingPathComponent task.item.index];
                  }
              }

          2.Create a downloader

          When initializing the downloader, the username should be introduced for distinguishing the download and storage of different local login users (if there is no account system in App, a fixed value can be introduced).
              self.downloader = [[BDCloudMediaDownload alloc] initWithUser:@"<user>" delegate:self];

          3.Create to download the task and start downloading

          After creating the download task, the task enters the execution pipeline, and the maximum concurrency number of the pipelines is 4. When it exceeds the maximum number of concurrency, the newly created task will be in a state waiting for execution. 
              NSError* error;
              BDCloudMediaDownloadTask* task = [self.downloader downloadTaskWithURL:@"<url>" title:@"<title>" error:&error];

          Note:

          • The introduced url must start with http or https and end with .m3u8.
          • If the incoming url was downloaded successfully and the removeMediaIteminterface was not called to clean up the video data, the creation of the download task fails, and the code of error is BDCloudMediaDownloadErrorCodeAlreadyExists.

          Encrypt the download of HLS videos

          Compared with the download of encrypted HLS video and that of common HLSvideo, there is more process to set authentication information, with the proxy method task:needAuthentication:required only.

          - (void)task:(BDCloudMediaDownloadTask*)task needAuthentication:(NSMutableDictionary*)parameters {
              // When the downloaded video uses PlayerID and PlayerKey encryption mode, you need to set playerID in parameters 
              [parameters setObject:@"<playerId>" forKey:@"playerId"];
              
              // When the video to download uses token encryption method, you need to set token in parameters
              [parameters setObject:@"<token>" forKey:@"token"];
          }

          Pause and resuming of the task

          The download tasks support pauses and resumption:

          • When pausing, the data downloaded locally is not lost;
          • When resuming, it continuously downloads from the previous download position.
          // Pause the task
          [self.downloader suspendTask:task];
          
          //  resume tasks
          [self.downloader resumeTask:task];

          Breakpoint resumption

          When App reopens to continue the unfinished download last time, first call resumeUncompletedTasks method (internal SDK automatically creates the unfinished task and adds it in the download pipeline according to the task status) to return to the unfinished download task.

          self.downloader = [[BDCloudMediaDownload alloc] initWithUser:@"<user>" delegate:self];
          
          // Get the unfinished tasks last time.
          self.tasks = [self.downloader resumeUncompletedTasks];

          Note:

          • The rules of and access to the thumbnail need to configure MCT transcoding template, and then the transcoding can be used.

          Download and Display the Thumbnails

          • Besides providing video play feature, SDK also supports thumbnail download and display. BDCloudSpriteManagerProvide thumbnail-related interfaces.
          // 1. Initialize CSS sprites rendering configuration and set the essential attributes.
          BDCloudSpriteRenderConfiguration *renderConfig = [BDCloudSpriteRenderConfiguration defaultConfiguration];
          renderConfig.row = <Number of lines in the thumbnail ofSprite Image>;
          renderConfig.column = <The thumbnail Pipeline number in CSS sprites> ;
          renderConfig.thumbnailDuration = <Time intervals of every thumbnail>;
          
          // 2. Initialize the CSS sprites download configuration and set the essential attribute.
          BDCloudSpriteDownloadConfiguration *downloadConfig = [BDCloudSpriteDownloadConfiguration defaultConfiguration];
          downloadConfig.imageList = <CSS sprites URL list>;
          downloadConfig.maxThreadNum = <Maximum thread number after downloading>;
          downloadConfig.path = <Download link>;
          
          // 3. Initialize CSS sprites rendering control class and add display canvas.
          BDCloudSpriteManager *manager = [BDCloudSpriteManager initWithRenderConfig:renderConfig downloadConfig:downloadConfig];
          manager.renderView.frame = <Canvas frame>;
          [self.view addSubView:manager.renderView]
          [manager startDownload:nil];
          
          // 4. After you receive successful callback of downloaded items, display the thumbnail according to the timestamp.
          [manager renderTime:<Display the timestamp>]; 

          Note:

          • Current online video acceleration only supports mp4 format.

          Network Video Acceleration

          Besides providing video play feature, SDK also provides online video acceleration. BDCloudMediaSourceManagerProvide accelerated play of the online videos. Contain the following features:

          • Video preload.
          • Save while playing.
          // 1. Pre-download the video. 
          [[BDCloudMediaSourceManager sharedInstance] download:url size:downloadSize complete:^(BOOL succ, NSError *error) {
              NSLog(@"pre download succ = %d, error = %@",error);            
          }];
          
          // 2. Enable the feature of saving while playing 
          [BDCloudMediaSourceManager sharedInstance].autoWriteCache = YES;
          
          // 3. The player is set with network proxy. 
          self.player.proxy = [BDCloudMediaSourceManager sharedInstance];
          
          // 4. Start to play after pre-downloading.
          [self.player prepareToPlay]

          VR Video Play

          Besides providing basic video play feature, SDK also supports VR video play. BDCloudVRRenderProvide VR video playback.

          // 1. Configure basic information of VR playback
          // 1.1 Create an information instance by default.
          self.vrConfig = [BDCloudVRConfiguration defaultConfig];
          // 1.2 Configure VR resource type.
          [self.vrConfig setProjectionMode:<projectionMode>];
          // 1.3 Configure rendering mode.
          [self.vrConfig setDisplayMode:<displayMode>];
          // 1.4 Configure the interaction mode. 
          [self.vrConfig setInteractiveMode:<interactiveMode>];
          // 1.6 Configure pinch gestures. 
          [self.vrConfig setPinchEnabled:<pinchEnabled>];
          
          // 2. Receive the decoded information notification, and initialize the VR rendering control class and start VR rendering after configuring the decoding information.
          - (void)onPlayerVideoDecoderOpen:(NSNotification *)notification {
              if (notification.object != _player) {
                  return;
              }
              if (self.vrConfig) {
                  // 2.1 Configure decoding information
                  [self.vrConfig setProviderBDCloudMediaPlayerView:_player.view
                                         viaHardwareAccelerate:[_player viaHardwareAccelerate]];
                  // 2.2 Configuration rendering parent view
                  [self.vrConfig setRenderOn:self.player.view];
                  // 2.3 Initialize the VR rendering control class
                  self.vrManager = [BDCloudVRRenderControl renderWithConfig:self.vrConfig];
                  // 2.4 Start VR rendering. 
                  [self.vrManager resume];
              }
          }
          Previous
          SDK Integration
          Next
          Migration_iOS