0

問題をグーグルで検索しましたが、同様のケースが見つかりませんでした。

このチュートリアルを使用して、iPad-Simulator のボタンを押したときに testSound.mp3 - ファイルを再生しました: 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
4

1 に答える 1

0

コードにはいくつかの矛盾があります:パラメータplaySound:がありますが、そのメソッド内では. 次に、の代わりに設定します(つまり、無限の繰り返しを意味します)。NSStringAVAudioPlayerNSURLnumberOfLoops = -1numberOfLoops = 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

もちろん、「無限の繰り返し」でこれを行うべきではありません!

于 2013-04-06T13:35:50.157 に答える