gpt4 book ai didi

ios - 未调用 audioPlayerDidFinishPlaying

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:07 26 4
gpt4 key购买 nike

我在使用 AVAudioPlayer 时遇到问题。我想在我的声音文件完成后做点什么。正在播放声音,但未调用 audioPlayerDidFinishPlaying。

我的.h

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

@interface Tag1Uebung1ViewController : ViewController {
AVAudioPlayer *audioPlayer;
}

@end

我的.m(实现文件)

#import "Tag1Uebung1ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "Tag1U2ViewController.h"

@interface Tag1Uebung1ViewController ()
@end
@implementation Tag1Uebung1ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/crunch_wdh8.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer.delegate = self;

NSLog(@"Entered the callback function");

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
//audioPlayer.numberOfLoops = 0;

if (audioPlayer== nil) {
NSLog([error description]);
} else {
[audioPlayer play]; }
}



- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"audioPlayerDidFinishPlaying");
}

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


@end

我的调试控制台只给我“输入回调函数”,所以我的意思是正确调用了 viewDidLoad 函数。我做错了什么?

最佳答案

您必须在 audioPlayer 初始化后设置 delegete。

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;

关于ios - 未调用 audioPlayerDidFinishPlaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com