audioPlayerDidFinishPlaying未调用

 2012牛人 发布于 2023-01-01 10:39

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

我的.h

#import "ViewController.h"
#import 

@interface Tag1Uebung1ViewController : ViewController {
    AVAudioPlayer *audioPlayer;
}

@end

我的.m(impl.文件)

#import "Tag1Uebung1ViewController.h"
#import 
#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函数.我做错了什么?

1 个回答
  • 您必须在audioPlayer初始化后设置delegete.

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

    2023-01-01 10:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有