AVPlayer 不工作
AVPlayer not functioning
我正在尝试在 SpriteKit 应用中使用 objective C 中的 AVPlayer,但出于某种原因,尽管 URL 定义正确,但我听不到音乐。
(这是来自游戏视图控制器的 运行)
-(void)viewDidLoad{
[super viewDidLoad];
[self prefersStatusBarHidden]; //hopefully stop an annoying bug.
NSLog(@"Is it on? %d", _playingMusic);
if(_playingMusic == 1){ //set audio to play if requested
NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"new_twig_BK2_rehash" ofType:@"mp3"];
NSURL *soundURL1 = [NSURL fileURLWithPath: soundPath1];
AVAudioPlayer *BKMPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL1 error:nil];
BKMPlayer1.numberOfLoops = -1; //infinite loops
[BKMPlayer1 setVolume: 1.0];
[BKMPlayer1 stop];
[BKMPlayer1 setCurrentTime:0];
[BKMPlayer1 play];
}
}
不要在 viewDidLoad
方法中创建音频播放器,您应该创建一个 strong
属性 的 audioPlayer
@property (nonatomic, strong) AVAudioPlayer *BKMPlayer1;
这是因为,一旦控件脱离函数,audioPlayer 就会被 ARC 释放。
我正在尝试在 SpriteKit 应用中使用 objective C 中的 AVPlayer,但出于某种原因,尽管 URL 定义正确,但我听不到音乐。
(这是来自游戏视图控制器的 运行)
-(void)viewDidLoad{
[super viewDidLoad];
[self prefersStatusBarHidden]; //hopefully stop an annoying bug.
NSLog(@"Is it on? %d", _playingMusic);
if(_playingMusic == 1){ //set audio to play if requested
NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"new_twig_BK2_rehash" ofType:@"mp3"];
NSURL *soundURL1 = [NSURL fileURLWithPath: soundPath1];
AVAudioPlayer *BKMPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL1 error:nil];
BKMPlayer1.numberOfLoops = -1; //infinite loops
[BKMPlayer1 setVolume: 1.0];
[BKMPlayer1 stop];
[BKMPlayer1 setCurrentTime:0];
[BKMPlayer1 play];
}
}
不要在 viewDidLoad
方法中创建音频播放器,您应该创建一个 strong
属性 的 audioPlayer
@property (nonatomic, strong) AVAudioPlayer *BKMPlayer1;
这是因为,一旦控件脱离函数,audioPlayer 就会被 ARC 释放。