AVAudioPlayer 类中的错误?

发布于 2024-09-07 08:34:50 字数 1435 浏览 2 评论 0原文

我会保持简短和甜蜜 - 在我为自己购买 iPhone 开发者计划之前,我正在构建一个应用程序只是为了练习。

我正在尝试 AVFoundation.framework,但一直遇到一个错误,该错误只会让我播放在代码中初始化的第一个声音。你认为你们中有人可以帮助我吗?提前致谢!! :)

视图控制器

-(IBAction)play {
    if (segments.selectedSegmentIndex == 0) {
        NSLog(@"Segment = 0");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"meow" ofType:@"wav"];

        if (path != nil) {
            NSURL *url = [NSURL fileURLWithPath:path];
            AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
            [player prepareToPlay];
            [player play];
        }
    }

    else if (segments.selectedSegmentIndex == 1) {
        NSLog(@"Segment = 1");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path2 = [bundle pathForResource:@"meowloud" ofType:@"wav"];

        if (path2 != nil) {
            NSURL *url2 = [NSURL fileURLWithPath:path2];
            AVAudioPlayer *player2 = [[AVAudioPlayer alloc]initWithContentsOfURL:url2 error:NULL];
            [player2 prepareToPlay];
            [player2 play];
//          [player2 release];
        }
    }

    text1.textColor = [UIColor clearColor];
    text2.textColor = [UIColor clearColor];
    text3.textColor = [UIColor clearColor];
    text4.textColor = [UIColor clearColor];

}

当执行此代码时,无论选择哪个段,都只会执行 meow.wav

I'll keep it short and sweet - I'm building an application just for practice before I buy myself the iPhone Developer Program.

I'm experimenting with the AVFoundation.framework and I keep running into a bug which will only let me play the first sound I initialize in my code. Do you think any of you could help me out? Thanks in advance!! :)

view controller

-(IBAction)play {
    if (segments.selectedSegmentIndex == 0) {
        NSLog(@"Segment = 0");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"meow" ofType:@"wav"];

        if (path != nil) {
            NSURL *url = [NSURL fileURLWithPath:path];
            AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
            [player prepareToPlay];
            [player play];
        }
    }

    else if (segments.selectedSegmentIndex == 1) {
        NSLog(@"Segment = 1");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path2 = [bundle pathForResource:@"meowloud" ofType:@"wav"];

        if (path2 != nil) {
            NSURL *url2 = [NSURL fileURLWithPath:path2];
            AVAudioPlayer *player2 = [[AVAudioPlayer alloc]initWithContentsOfURL:url2 error:NULL];
            [player2 prepareToPlay];
            [player2 play];
//          [player2 release];
        }
    }

    text1.textColor = [UIColor clearColor];
    text2.textColor = [UIColor clearColor];
    text3.textColor = [UIColor clearColor];
    text4.textColor = [UIColor clearColor];

}

When this code gets executed, only the meow.wav is executed, no matter which segment is selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-09-14 08:34:50

您的代码有一些内存泄漏。您在 play 方法中分配 AVAudioPlayer 的实例,但永远不会释放这些实例。有关详细信息,请参阅 Cocoa 的内存管理指南。由于 AVAudioPlayer 实例需要保留在内存中才能播放,最简单的解决方案是向 viewController 类添加一个成员变量并将其设置为 retain 属性。

为了不要重复自己(DRY),我还建议添加封装播放单个 wav 文件所需的所有代码的方法。

这是我编写此视图控制器的方式:

MyViewController.h

@interface MyViewController : UIViewController
{
    AVAudioPlayer *player;
}
@property(nonatomic, retain) AVAudioPlayer *player;
- (void)playWavFile:(NSString *)fileName;
- (IBAction)play;
@end

MyViewController.m

@synthesize player;

- (void)dealloc {
    [player release];
}

- (void)playWavFile:(NSString *)fileName {
    NSBundle *bundle = [NSBundle mainBundle];   
    NSString *path = [bundle pathForResource:fileName ofType:@"wav"];

    if (path != nil) {
        AVAudioPlayer *newPlayer;
        NSURL *url = [NSURL fileURLWithPath:path];
        newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                           error:NULL];
        [self setPlayer:newPlayer];
        [newPlayer release];

        [newPlayer prepareToPlay];
        [newPlayer play];
    }
}

- (IBAction)play {

    if (segments.selectedSegmentIndex == 0) {
        [self playWavFile:@"meow"];
    }
    else if (segments.selectedSegmentIndex == 1) {
        [self playWavFile:@"meowloud"];
    }
}

Your code has a couple of memory leaks. You allocate instances of AVAudioPlayer in your play method, but you never release those instances. See Cocoa's memory management qguidelines for details. Since the AVAudioPlayer instances need to remain in memory to play, the easiest solution is to add a member variable to your viewController class and set it up as a retain property.

For the sake of Don't Repeat Yourself (DRY), I would also suggest adding a method which encapsulates all of the code needed to play a single wav file.

Here is how I would write this view controller:

MyViewController.h

@interface MyViewController : UIViewController
{
    AVAudioPlayer *player;
}
@property(nonatomic, retain) AVAudioPlayer *player;
- (void)playWavFile:(NSString *)fileName;
- (IBAction)play;
@end

MyViewController.m

@synthesize player;

- (void)dealloc {
    [player release];
}

- (void)playWavFile:(NSString *)fileName {
    NSBundle *bundle = [NSBundle mainBundle];   
    NSString *path = [bundle pathForResource:fileName ofType:@"wav"];

    if (path != nil) {
        AVAudioPlayer *newPlayer;
        NSURL *url = [NSURL fileURLWithPath:path];
        newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                           error:NULL];
        [self setPlayer:newPlayer];
        [newPlayer release];

        [newPlayer prepareToPlay];
        [newPlayer play];
    }
}

- (IBAction)play {

    if (segments.selectedSegmentIndex == 0) {
        [self playWavFile:@"meow"];
    }
    else if (segments.selectedSegmentIndex == 1) {
        [self playWavFile:@"meowloud"];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文