gpt4 book ai didi

ios - 使用类作为静态 AVAudioPlayer 的 AVAudioPlayerDelegate

转载 作者:行者123 更新时间:2023-12-01 19:11:55 24 4
gpt4 key购买 nike

我无法在 TriggerIO 插件中维护实例,因此我需要一种静态方式来响应我的 AVAudioPlayer的事件。但是,我无法设置 player.delegateaudio_API ,所以我有点卡住了。

音频API.h:

@interface audio_API : NSObject<AVAudioPlayerDelegate>

audio_API.m:
static AVAudioPlayer* player = nil;


@implementation audio_API


+ (void)play:(ForgeTask*)task {

// parse the file url from the file object
ForgeFile* file = [[ForgeFile alloc] initWithFile:[task.params objectForKey:@"file"]];
NSString* fileURL = [file url];

NSLog(@"Playing file at %@", fileURL);

NSURL* url = [NSURL URLWithString:fileURL];


// TESTING
url = [[NSBundle mainBundle] URLForResource:@"seconds.m4a" withExtension:nil];
// END TESTING

NSAssert(url, @"URL is invalid.");

// create the player
NSError* error = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if(!player)
{
NSLog(@"Error creating player: %@", error);
};

// player.delegate = audio_API;

[player play];

[task success:nil];
}


+ (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag) {
[ForgeLog d:@"Audio finished playing successfully."];
} else {
[ForgeLog d:@"Audio did not finish playing successfully."];
}

// call the audio.finishedPlaying event
[[ForgeApp sharedApp] event:@"audio.finishedPlaying" withParam:nil];
}

+ (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
[ForgeLog d:@"Audio had error decoding."];
[ForgeLog e:error];

// call the audio.decodeErrorOccurred event
[[ForgeApp sharedApp] event:@"audio.decodeErrorOccurred" withParam:nil];
}

@end

知道如何进行这项工作吗?

谢谢。

最佳答案

委托(delegate)必须是一个对象,但是 audio_API是类型名称。但是,您可以简单地将委托(delegate)指定为 audio_API 的类对象。 :

player.delegate = (id<AVAudioPlayerDelegate>)[audio_API class];

注意:如果你不强制转换类对象,编译器会报错,因为 [audio_API class]返回 Class ,而不是 Class<AVAudioPlayerDelegate> ,即使您在 audio_API 的接口(interface)上指定委托(delegate)

关于ios - 使用类作为静态 AVAudioPlayer 的 AVAudioPlayerDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007776/

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