Add custom header field in request of AVPlayer

前端 未结 6 1940
陌清茗
陌清茗 2020-11-29 00:21

Is it possible to send headers with an http request to an audio file when using AVPlayer? I need to be able to inspect the content of the header when received by the server

6条回答
  •  青春惊慌失措
    2020-11-29 00:43

    the full code could look like this

      #pragma Mark Sound Stuff
    
    - (void)playSound:(NSString *)filePath
    {
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filePath]];
        [playerItem addObserver:self forKeyPath:@"status" options:0 context:0];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
        self.audioPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
        [self.audioPlayer play];
    }
    
    - (void)initSoundPrelisten
    {
        //NSLog(@"begin: %s", __FUNCTION__);
        self.activityIndicator.hidden = NO;
        [self.activityIndicator startAnimating];
    
        // verification delegate : register
        dataProtocol = [[StoreConnection alloc] init];
        [dataProtocol setDelegate:self];
        [dataProtocol requestDataFromServer:[NSString stringWithFormat:@"sound/%@/audio/sample", [self.sound objectForKey:@"globalId"]]];
    }
    
    - (void)dataSuccessful:(BOOL)success successData:(NSMutableData *)data;
    {
        NSLog(@"%s", __FUNCTION__);
        if (success) {
            //NSLog(@"sound data: %@", data);
            NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filePath = [cacheDirectory stringByAppendingPathComponent:@"sample.mp3"];
            //NSLog(@"filePath: %@", filePath);
            [data writeToFile:filePath atomically:YES];
            [self playSound:filePath];
    
        } else
        {
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Store Error" message:[NSString stringWithFormat:@"An Error occured while trying to download sound. Please try again"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            alertView.tag = 1;
            [alertView show];
        }
    }
    

提交回复
热议问题