0

像这样调用 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.

4

3 回答 3

2

您创建了 的实例AVPlayer,而不是 的实例AVAudioPlayer。看起来您想要创建一个AVAudioPlayer替代(正如您为类的实际player属性选择该类所表明的那样。AVAudioPlayer实际上具有该numberOfLoops属性,而AVPlayer没有。有关更多信息,请参阅AVAudioPlayerAVPlayer的文档。

于 2013-05-18T23:48:48.373 回答
1

AVPlayer没有numberOfLoops财产。那是`AVAudioPlayer 的一个属性。构建应用程序时不要忽略编译器警告。

此外,您定义_player为 anAVAudioPlayer但您是 alloc/init 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);
}
于 2013-05-18T23:46:26.910 回答
0

我认为您调用的方法不存在(考虑到您的错误)。

尝试:_player.numberOfLoops=-1

于 2013-05-18T23:34:12.340 回答