
在带有iOS 5.1.1的iPad 2上,即使我将耳机连接到设备,它也能正常播放.但是一旦我断开它们就不会通过扬声器发出声音(即使我再次连接它们,我也可以听这首歌).
在安装了iOS 5.1的iPhone 4上,它似乎无法通过扬声器播放,但我可以通过耳机收听音乐.虽然它似乎没有通过扬声器播放,我可以偶尔听到音乐播放的短暂时间(并且可以确认它实际上正在播放,因为我的UI响应相应)虽然它似乎是随机的.
我正在使用AVPlayer,因为它似乎符合要求.我应该使用其他图书馆吗?我需要手动路由声音吗?为什么我遇到这些类型的问题?我正确使用音频会话吗?
Media.h:
#import <Foundation/Foundation.h>#import <AVFoundation/AVFoundation.h>#import <AudioToolBox/AudioToolBox.h>#define NOT_PLAYING -1@protocol MediaDelegate <NSObject>@optional- (voID) dIDEndplayingAtIndex:(int) index;- (voID) startedtoplayAtIndex:(int) index;- (voID) stoppedtoplayAtIndex:(int) index;- (voID) startedtoplayAtIndex:(int) to fromIndex:(int) from;- (voID) pausedAtIndex:(int) index;@end@interface Media : NSObject <AVAudioSessionDelegate>@property (nonatomic,assign) int currentItem;@property (nonatomic,strong) NSURL *url;@property (nonatomic,strong) AVPlayer *player;@property (nonatomic,assign) ID<MediaDelegate> delegate;- (voID) toggle:(int) index;- (voID) stop;@end
Media.m:
#import "Media.h"@implementation Media@synthesize currentItem;@synthesize player;@synthesize delegate;@synthesize url;- (ID)init{ self = [super init]; if (self) { NSError *activationError = nil; NSError *setcategoryError = nil; AVAudioSession *session = [AVAudioSession sharedInstance]; session.delegate = self; [session setActive:YES error:&activationError]; [session setcategory:AVAudioSessioncategoryPlayback error:&setcategoryError]; if(activationError || setcategoryError) NSLog(@"ERROR: %@,%@",activationError,setcategoryError); self.currentItem = NOT_PLAYING; } return self;}- (voID)dealloc{ NSError *activationError = nil; [[AVAudioSession sharedInstance] setActive:NO error:&activationError]; if(activationError) NSLog(@"ERROR: %@",activationError); [self.player removeObserver:self forKeyPath:@"status"];}- (voID)observeValueForKeyPath:(Nsstring *)keyPath ofObject:(ID)object change:(NSDictionary *)change context:(voID *)context{ switch (player.status) { case AVPlayerItemStatusReadytoplay: [self.player play]; if([self.delegate respondsToSelector:@selector(startedtoplayAtIndex:)]) [self.delegate startedtoplayAtIndex:self.currentItem]; break; default: break; }}- (voID) toggle:(int) index{ if (self.currentItem == NOT_PLAYING) { self.player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithURL:self.url]]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(dIDEnd:) name:AVPlayerItemDidplayToEndTimeNotification object:self.player]; self.currentItem = index; [self.player addobserver:self forKeyPath:@"status" options:0 context:nil]; } else { if (self.currentItem == index) { [self.player pause]; if([self.delegate respondsToSelector:@selector(stoppedtoplayAtIndex:)]) [self.delegate stoppedtoplayAtIndex:index]; self.currentItem = NOT_PLAYING; [self.player removeObserver:self forKeyPath:@"status"]; self.player = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; } else{ [self.player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:self.url]]; if([self.delegate respondsToSelector:@selector(startedtoplayAtIndex:fromIndex:)]) [self.delegate startedtoplayAtIndex:index fromIndex:self.currentItem]; self.currentItem = index; } }}- (voID) stop{ [self.player pause]; if([self.delegate respondsToSelector:@selector(stoppedtoplayAtIndex:)]) [self.delegate stoppedtoplayAtIndex:self.currentItem];}-(voID) dIDEnd:(ID)sender{ if([self.delegate respondsToSelector:@selector(dIDEndplayingAtIndex:)]) [self.delegate dIDEndplayingAtIndex:self.currentItem]; self.currentItem = NOT_PLAYING; [self.player removeObserver:self forKeyPath:@"status"]; self.player = nil; [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - AVAudioSession delegate-(voID)beginInterruption{ NSLog(@"Interruption"); if(self.currentItem != NOT_PLAYING){ [self.player pause]; if([self.delegate respondsToSelector:@selector(pausedAtIndex:)]) [self.delegate pausedAtIndex:self.currentItem]; }}-(voID)endInterruption{ NSLog(@"Ended interruption"); if(self.currentItem != NOT_PLAYING){ [self.player play]; if([self.delegate respondsToSelector:@selector(startedtoplayAtIndex:)]) [self.delegate startedtoplayAtIndex:self.currentItem]; }}@end解决方法 看起来你有正确的音频路由问题.我建议添加一个音频路由更改侦听器回调.首先声明一个方法: voID audioRoutechangelistenerCallback(voID *inUserData,AudioSessionPropertyID inPropertyID,UInt32 inPropertyValueSize,const voID *inPropertyValue);
然后在你的init方法中添加一个回调:
// Prevent from audio issues when you pull out earphoneAudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,audioRoutechangelistenerCallback,(__brIDge voID *)(self));
最后,添加一个回调实现:
voID audioRoutechangelistenerCallback(voID *inUserData,const voID *inPropertyValue) { if (inPropertyID != kAudioSessionProperty_AudioRouteChange) { return; } CFDictionaryRef routeChangeDictionary = inPropertyValue; CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary,CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); SInt32 routeChangeReason; CFNumberGetValue(routeChangeReasonRef,kcfNumberSInt32Type,&routeChangeReason); if (routeChangeReason == kAudioSessionRouteChangeReason_oldDeviceUnavailable) { // headset is unplugged.. NSLog(@"headset is unplugged.."); // Delayed play: 0.5 s. dispatch_after(dispatch_time(disPATCH_TIME_Now,0.5f * NSEC_PER_SEC),dispatch_get_main_queue(),^{ [CORE.player play]; // NOTE: change this line according your current player implementation NSLog(@"resumed play"); }); } if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) { // headset is plugged in.. NSLog(@"headset is plugged in.."); }} 希望这有帮助!至少对其他人来说,这将是有用的提示.
总结以上是内存溢出为你收集整理的iphone – iOS AVPlayer流媒体音乐问题全部内容,希望文章能够帮你解决iphone – iOS AVPlayer流媒体音乐问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)