我尝试在iOS中播放youtube视频。
背景音频代码。 `
- (BOOL) ensureAudio
{
// Registers this class as the delegate of the audio session (to get background sound)
[[AVAudioSession sharedInstance] setDelegate: self];
// Set category
NSError *categoryError = nil;
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&categoryError]) {
NSLog(@"Audio session category could not be set");
return NO;
}
// Activate session
NSError *activationError = nil;
if (![[AVAudioSession sharedInstance] setActive: YES error: &activationError]) {
NSLog(@"Audio session could not be activated");
return NO;
}
// Allow the audio to mix with other apps (necessary for background sound)
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"applicationDidEnterBackground");
state = ENTER_BACKGROUND;
// audio playing code
[self checkPlay];
}
`
Youtube播放器api代码
function onPlayerStateChange(e){
switch(e.data){
case YT.PlayerState.PAUSED:
play();
window.location = "native://state_change";
}
}
成功模式正在onPlayerState.PAUSED。 错误的模式无法正常运行.PlayseState.PAUSED。