【问题标题】:Identical Functions, both get executed, no Sound in one相同的功能,都执行,没有声音
【发布时间】:2013-04-06 12:11:48
【问题描述】:

我用谷歌搜索了这个问题,但找不到类似的案例。

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

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

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

最好的问候, - 茶壶

// 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

// 声音.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

// 声音.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

【问题讨论】:

  • 网址是什么,可以给我看看吗?
  • 你到底在干什么?你真的检查过这段代码吗?
  • 你好,murali,我已经检查过了,后来重命名了一些东西。 url 输出(通过 absoluteString)有点奇怪,有时它会显示数千个中文字母。不知道该怎么做,但在 Martin R 的回答之后,这似乎不是问题所在。

标签: objective-c xcode audio


【解决方案1】:

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

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

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

pressButton: 离开时,tempSound 对象被释放,因为不再存在对该对象的强引用。

如果您将实例变量(或属性)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

当然,你不应该用“无限重复”来做这个!

【讨论】:

  • 啊,我没有考虑对象的生命周期。非常感谢你,马丁 R! :) 再次感谢您的替代解决方案!这让我放心了。是的,还没有使用重复等。首先,我希望修复该错误。
  • @Teapot:不客气。 ARC 是一项很棒的功能(我现在一直在使用它),但必须学会“在 ARC 中思考”:-)
猜你喜欢
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多