tvOS应用程序无法播放音频文件

时间:2015-10-08 17:29:45

标签: avfoundation avaudioplayer tvos

我有一个tvOS应用程序尝试播放这样的音频文件:

#import "NameViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface NameViewController ()

@end

@implementation NameViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL fileTypeHint:AVFileTypeMPEGLayer3 error:nil];
    player.numberOfLoops = -1; //infinite

    [player play];


}

如果我在[玩家玩]之后放了一个断点;它开始播放几秒钟。如果我没有断点,就不会播放音频。

我做错了什么?

4 个答案:

答案 0 :(得分:0)

我不确定问题是什么,但是因为你说在[player play];之后放一个断点会导致音频播放,听起来好像系统也试图创建并播放音频播放器快......你是在模拟器上还是在电视开发套件上运行它?

也许尝试添加一个单独的方法来创建和播放播放器,就像这样,会给系统足够的时间来创建视图,然后创建和播放音频播放器?

- (void) createAndStartAudioPlayer {
  NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL fileTypeHint:AVFileTypeMPEGLayer3 error:nil];
    player.numberOfLoops = -1; //infinite

    [player play];
}

然后从viewDidLoad调用

[self createAndStartAudioPlayer];

答案 1 :(得分:0)

这可以通过在viewWillAppear中调用play来实现:

questions

答案 2 :(得分:0)

您的AVAudioPlayer需要在ViewController中定义为属性,然后您可以从您想要的地方调用它。

答案 3 :(得分:0)

AVAudioPlayer在声音播放完毕之前被解除分配。在调用播放后设置断点时,音频播放器不会被释放,直到您继续播放声音为止。如果您将音频播放器设置为@property (strong, nonatomic) AVAudioPlayer *player;之类的属性,则在取消视图控制器之前,它将无法取消分配。

相关问题