問題をグーグルで検索しましたが、同様のケースが見つかりませんでした。
このチュートリアルを使用して、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