当调用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。
答案 0 :(得分:2)
您已创建AVPlayer
的实例,而不是AVAudioPlayer
的实例。您似乎想要创建AVAudioPlayer
(正如您在课程中为实际player
属性选择的那个类所示。AVAudioPlayer
实际上有numberOfLoops
虽然AVPlayer
没有。但有关详细信息,请参阅AVAudioPlayer和AVPlayer的文档。
答案 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