[objective-c]相同的功能,两者都得到执行,在一个没有声音

标签: Xcode Objective-C
发布时间: 2017/3/27 3:28:57
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我用 google 搜索这个问题但是找不到类似的情况。

我使用本教程来播放 testSound.mp3-文件时打上 iPad 模拟器的按钮︰ http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/

它在起作用,(它播放的声音) 如果 playSound 方法是在我的 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

解决方法 1:

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

主要的问题是,在这里 (假设你使用"自动引用计数"编译)

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

tempSound对象是释放pressButton: 走了,因为没有对该对象的强引用不再存在。

如果您添加实例变量 (或财产) sound 视图控制器类,和分配给,

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

当然,你不应该这样做"无限重复"!

赞助商