AVplayer setNumberOfLoops方法中无法识别的选择器

时间:2013-05-18 23:27:15

标签: objective-c loops avplayer

当调用numberOfLoops方法时,如下所示: [_player setNumberOfLoops:-1];

我收到以下错误: -[AVPlayer setNumberOfLoops:]: unrecognized selector sent to instance 0x7d52d30

如何解决这个问题?

代码:

部首:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController {

}


@property (strong, nonatomic) AVAudioPlayer *player;

- (IBAction)playMusic:(id)sender;


@end

实现:

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playMusic:(id)sender {
    _player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://urlpath.wav"]];
    [_player setNumberOfLoops:-1];
    [_player prepareToPlay];
    [_player play];
}

@end

感谢您的时间,

Yoni201。

3 个答案:

答案 0 :(得分:2)

您已创建AVPlayer的实例,而不是AVAudioPlayer的实例。您似乎想要创建AVAudioPlayer(正如您在课程中为实际player属性选择的那个类所示。AVAudioPlayer实际上有numberOfLoops虽然AVPlayer没有。但有关详细信息,请参阅AVAudioPlayerAVPlayer的文档。

答案 1 :(得分:1)

AVPlayer没有numberOfLoops属性。这是`AVAudioPlayer的属性。在构建应用程序时,请不要忽略编译器警告。

此外,您将_player定义为AVAudioPlayer,但您分配/初始化AVPlayer

将您的代码更改为:

NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://urlpath.wav"] error:&error];
if (player) {
    [player setNumberOfLoops:-1];
    [player prepareToPlay];
    [player play];
    self.player = player;
} else {
    NSLog(@"Error create audio player: %@", error);
}

答案 2 :(得分:0)

我认为您调用的方法不存在(重音您的错误)。

尝试:_player.numberOfLoops = -1