objective-c - 相同的函数,都被执行,一个声音都没有

标签 objective-c xcode audio

我用Google搜索了问题,但找不到类似的情况。

我已经使用了本教程来播放testSound.mp3-在iPad-Simulator上单击按钮时播放文件:
http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/

如果playSound-Method在我的ViewController.m中,而在我的Sound.m(具有相同的方法)中,则它以这种方式工作(播放声音)。
该代码已执行(NSLog说:“Sound.m playSound已执行”),但是根本没有声音。

我真的很感谢这里的帮助,猜想我完全被卡住了... :(

最好的祝福,
- 茶壶

// ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Sound.h"

@interface ViewController : UIViewController {

    AVAudioPlayer *audioPlayer;

}

- (IBAction)pressButton:(id)sender;
- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats;

@end

// ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading thea view, typically from a nib.

}

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

- (IBAction)pressButton:(id)sender {

    NSLog (@"Method: pressButton");

    [self playSound: @"testSound.mp3" volume: 2 repeats: 2 url : url]; //It works!

    Sound *tempSound = [[Sound alloc] init];
    [tempSound playSound: @"testSound.mp3" volume: 2 repeats: 2]; // Doesn't work. -> Says "Sound.m playSound executed", but there is no Sound.

}

- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats {

    NSLog(@"ViewControler playSound");

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil){
        NSLog([error description]);
        NSLog(@"ViewController.m playSound NOT executed");
    }
    else{
        [audioPlayer play];
        NSLog(@"ViewController.m playSound executed");
    } 

}

@end

// Sound.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface Sound : NSObject {
    AVAudioPlayer *audioPlayer;
}

- (void) playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats;

@end

// Sound.m
#import "Sound.h"

@implementation Sound

- (void)playSound: (NSString*) soundFile volume : (NSInteger) volume repeats : (NSInteger) repeats {

    NSLog(@"Sound playSound");

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil){
        NSLog([error description]);
        NSLog(@"Sound.m playSound NOT executed");
    }
    else{
        [audioPlayer play];
        NSLog(@"Sound.m playSound executed");
    } 

}

@end

最佳答案

您的代码中存在一些不一致之处:playSound:具有NSString参数,但是该方法中的AVAudioPlayer使用NSURL。然后设置numberOfLoops = -1(这意味着无限重复),而不是numberOfLoops = repeat

但是的主要问题是这里的(假设您使用“自动引用计数”进行编译)

Sound *tempSound = [[Sound alloc] init];
[tempSound playSound: @"testSound.mp3" volume: 2 repeats: 2];

保留tempSound时,将释放pressButton:对象,因为不再存在对该对象的强引用。

如果您将实例变量(或属性)sound添加到 View Controller 类,并分配给该类
sound = [[Sound alloc] init];
[sound playSound: @"testSound.mp3" volume: 2 repeats: 2];

那么它应该会按预期工作。

,或者,,您可以通过在对象内部维护“自我引用”来防止Sound对象过早地释放,只有在声音播放完毕后才将其删除:
@interface Sound () <AVAudioPlayerDelegate>
@property(strong, nonatomic) AVAudioPlayer *audioPlayer;
@property(strong, nonatomic) Sound *selfRef;
@end

@implementation Sound

- (void)playSound:(NSString *)soundFile volume:(NSInteger)volume repeats:(NSInteger)repeats
{
    NSLog(@"Sound playSound");
    NSURL *soundURL = [[NSBundle mainBundle] URLForResource:soundFile withExtension:nil];
    NSError *error;
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
    if (self.audioPlayer == nil) {
        NSLog(@"%@", [error description]);
        NSLog(@"Sound.m playSound NOT executed");
    } else{
        self.audioPlayer.numberOfLoops = repeats;
        self.audioPlayer.delegate = self;
        [self.audioPlayer play];
        self.selfRef = self; // self reference to avoid deallocation
        NSLog(@"Sound.m playSound executed");
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    self.selfRef = nil; // remove self reference
}
@end

当然,您不应该使用“无限重复”来做到这一点!

关于objective-c - 相同的函数,都被执行,一个声音都没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15850790/

相关文章:

Swift:task.arguments 忽略斜线

laravel - HowlerJS:在Safari和iOS设备上音频自动跳回

actionscript-3 - 在as3中加载声音

jquery - 在jQuery中暂停音频之前添加延迟

ios - indexpath 行未准备好 segue 以进行详细信息披露

objective-c - 确定 macOS 中窗口所在的显示器

ios - 在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 之间有什么区别?

ios - 隐藏图像并稍后绘制 objective-c

Xcode 9 和 Xcode 8?

iphone - 在 XCode 中创建了一个新的空项目并添加了一个 View Controller ,但它无法编译