【发布时间】:2014-12-14 08:43:37
【问题描述】:
我有一个AVQueuePlayer 属性,设置如下。 NSLog 语句报告音频应该可以正常播放(在应用程序运行时),但是在模拟器和 iOS 设备本身(iPhone 5s iOS 8.1.1)上进行测试时,没有音频播放。
我已确认该设备未静音,并且处于全音量状态。我正在使用 Xcode 6 和 iOS 8 SDK。我在“链接二进制文件”部分导入了 AVFoundation 框架并将其导入到我的类中。
-(void)viewDidLoad
{
[super viewDidLoad];
[self music];
// other setup code
}
- (void)music
{
music = [[NSMutableArray alloc]initWithArray:@[
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM1"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM2"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM3"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM4"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM5"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM6"
ofType:@"mp3" ]]],
[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BM7"
ofType:@"mp3" ]]]
]];
for(AVPlayerItem *a in music)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:a];
}
[music shuffle];
self.audio = [[AVQueuePlayer alloc]initWithItems:music];
[self.audio play];
NSLog(@"Now playing%@",[self.audio currentItem]);
NSLog(@"%@",self.audio);
}
-(void)playerItemFinished:(NSNotification *)notification
{
if([[self.audio currentItem] isEqual:[music lastObject]])
{
self.audio = nil;
[music shuffle];
self.audio = [[AVQueuePlayer alloc]initWithItems:music];
[self.audio play];
}
}
【问题讨论】:
-
能否显示您的
shuffle方法,在调用initWithItems:之前调用它,可能是此方法是您的问题的原因。此外,音乐创作可以从initWithArray:更改为initWithObjects:,因此您不需要创建新的不必要的数组。另外,您可以简单地调用[[NSBundle mainBundle] URLForResource:withExtension:,而不是对该数组中的每个对象调用[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:ofType:]]] -
哦...当您说
NSLog报告正在播放正确的文件时,我错过了部分。有点远射,但请尝试实施我给here 的答案。另外,尝试执行我上面的建议,看看它是否有任何改变。
标签: ios objective-c audio avplayer avqueueplayer