【发布时间】:2012-05-17 21:57:16
【问题描述】:
AVPlayer 遇到了一些非常奇怪的问题。我有一个非常简单的音乐播放器(它从 iTunes 商店流式传输样本音乐)在模拟器(iPhone 和 iPad 与 iOS 5.1)上正常工作,但它在真实设备上表现异常。
在装有 iOS 5.1.1 的 iPad 2 上,即使我将耳机连接到设备,它也能正常播放。但是一旦我断开它们,它就不会通过扬声器播放声音(即使我再次连接它们,我也可以听歌)。
在安装了 iOS 5.1 的 iPhone 4 上,它似乎根本无法通过扬声器播放,但我可以通过耳机听音乐。虽然它似乎没有通过扬声器播放,但我不时能听到很短的音乐播放(并且可以确认它实际上正在播放,因为我的 UI 相应地响应)虽然它似乎是随机的。
我正在使用AVPlayer,因为它似乎符合要求。我应该使用另一个库吗?我需要手动路由声音吗?为什么我会遇到这些类型的问题?我是否正确使用了音频会话?
媒体.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
媒体.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。
-
好吧,我不确定它是否做到了,因为我同时进行了一些其他更改,但它现在正常工作(到目前为止),除了声音没有被路由每当我断开耳机时,扬声器。非常感谢!
-
使用 AVAudioSessionCategoryPlayback 声音应该自动路由到扬声器。其他地方一定有一些代码阻止了这种情况的发生。如果您可以发布更多代码或将您的项目上传到某个地方,我可以看看。
-
真的很奇怪。不幸的是,上传更多代码并不取决于我,我很抱歉。但我很感激并感谢您的帮助。