【问题标题】:audioPlayerDidFinishPlaying not called未调用 audioPlayerDidFinishPlaying
【发布时间】:2014-09-17 11:49:54
【问题描述】:

我在使用 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 函数被正确调用。我做错了什么?

【问题讨论】:

    标签: ios callback avaudioplayer


    【解决方案1】:

    在您的 .h 文件中添加委托协议:

    @interface Tag1Uebung1ViewController : ViewController <AVAudioPlayerDelegate> {
         AVAudioPlayer *audioPlayer;
    }
    

    【讨论】:

    • 这没有帮助,我的调试控制台只给我“进入回调函数”
    【解决方案2】:

    你必须在audioPlayer初始化之后设置delegate。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多