我在AVPlayer
上遇到了一些非常奇怪的問題。我有一個非常簡單的音樂播放器(可以從iTunes商店流式傳輸音樂樣本)在模擬器上正確使用它(包括iOS 5.1的iPhone和iPad),但它在真實設備上表現異常。iOS AVPlayer流媒體音樂問題
在具有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
我知道這是一個長鏡頭,但嘗試在調用setCategory之後調用setActive。 – SteveB
好吧,我並不完全確定它是這樣做的,因爲我在同一時間做了其他更改,但它現在正常工作(到目前爲止),除了斷開連接時聲音未被路由到揚聲器耳機。非常感謝你! – dvieira
使用AVAudioSessionCategoryPlayback,聲音應自動發送到揚聲器。在其他地方一定有一些代碼阻止了這種情況的發生。如果你可以張貼更多的代碼或上傳你的項目,我可以看看。 – SteveB