首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVQueuePlayer音频不播放

AVQueuePlayer音频不播放
EN

Stack Overflow用户
提问于 2014-12-14 08:43:37
回答 2查看 574关注 0票数 0

我有一个AVQueuePlayer属性,设置如下。NSLog语句报告说,音频应该正确播放(在应用程序运行时),但是在模拟器和iOS设备本身(iPhone 5s iOS 8.1.1)上测试时,没有音频播放。

我已经证实,这个装置不是静音的,而且是全音量的。我正在使用Xcode 6和iOS 8 SDK。我在“链接二进制文件”部分导入了AVFoundation框架,并将其导入到我的类中。

代码语言:javascript
复制
-(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];

    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-14 09:39:22

好吧,我从上面的评论中做了一个代码片段,这样你就可以把它复制粘贴到你的项目中了。

我基本上做了两个改变。

数组创建已经从initWithArray:更改为initWithObjects:,因此您不会创建一个新的不必要的、未使用的数组,这可能是在此方法调用之后释放的。

另外,不是对数组中的每个对象调用[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:ofType:]]],而是简单地调用[[NSBundle mainBundle] URLForResource:withExtension:

代码语言:javascript
复制
music = [[NSMutableArray alloc] initWithObjects:
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"BM1" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"BM2" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"BM3" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"BM4" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"MP5" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"MP6" withExtension:@"mp3"]],
                          [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"MP7" withExtension:@"mp3"]],
                          nil];  

祝你好运,伙计。

编辑-

我不是以英语为母语的人,所以我会尽量说清楚,

如果有什么不清楚的话,告诉我,我会试着换个说法。

initWithArray所做的是使用给定数组的内容创建一个新数组,

例如:

代码语言:javascript
复制
NSArray *array1 = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil]];  
NSArray *array2 = [[NSArray alloc] initWithArray:array1];  

因此,现在array1array2都有相同的项。

如果我没有弄错的话,因为Xcode 4引入了一种创建数组的新方法,现在可以使用[[NSArray alloc] initWithObjects:]语法创建带有对象的数组,而不是调用@[]

所以我们上面的例子如下所示:

代码语言:javascript
复制
NSArray *array1 = @[@"One", @"Two", @"Three"]; // Notice that 'nil' is not necessary  
NSArray *array2 = [[NSArray alloc] initWithArray:array2];  

所以你在原始代码中所做的,基本上就是调用[[NSMutableArray alloc] initWithObjects:],它最终会创建一个新的数组,

然后,使用“新”@[]语法,创建了另一个包含所有AVPlayerItem对象的新数组,

并“告诉”music数组,在其中包含该新数组的内容。

因此,在这一行代码中,您实际上创建了两个不同的数组。

“新”未命名数组可能在该行之后被取消分配,因为您在任何地方都没有对该数组的任何引用(在新创建的数组中,只有对其对象的引用),

ARC自动释放引用计数为零的对象.

因此,您可能不会感觉到任何内存/性能问题,

但我认为这是值得指出的,因为它是不必要的,它不是实现您正在尝试的东西的‘正确’方式,现在代码看起来更干净、更易读。

顺便说一下-

这不是您的代码不起作用的原因。

您的问题在于,您的原始代码正在生成以路径为字符串的NSURL/PATH/TO/MY/MP3

但是AVPlayerItem期望路径是一个URL:file:///PATH/TO/MY/MP3

(您可以NSLog新的和旧的调用来验证不同的输出)

票数 0
EN

Stack Overflow用户

发布于 2014-12-14 09:26:20

尝试以这种方式构建数组:

代码语言:javascript
复制
music = [[NSMutableArray alloc] initWithArray:@[[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"BM1" withExtension:@"mp3"]],...]];

编辑:无论如何,IMHO最重要的方面是突出显示withExtension不工作的原因:第一个方面返回由指定名称标识的资源的文件URL,以及文件扩展名,其中-pathForResource:ofType:返回由指定名称和文件扩展名标识的资源的完整路径名。但是,您需要一个NSURL对象来初始化AVPlayer,而AVQueuePlayer是AVPlayer的一个子类,用于按顺序播放多个项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27467691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档