我搜索了问题,但找不到类似的案例。
我使用本教程播放testSound.mp3 - 在iPad模拟器上按下按钮时的文件: 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
答案 0 :(得分:0)
您的代码中存在一些不一致:playSound:
具有NSString
参数,但该方法中的AVAudioPlayer
使用NSURL
。然后设置numberOfLoops = -1
(表示无限重复)而不是numberOfLoops = repeat
。
但主要问题就是这里(假设您使用“自动引用计数”进行编译)
Sound *tempSound = [[Sound alloc] init];
[tempSound playSound: @"testSound.mp3" volume: 2 repeats: 2];
tempSound
对象在<{1}}离开时 deallocated ,因为不再存在对该对象的强引用。
如果将实例变量(或属性)pressButton:
添加到视图控制器类,并将其分配给
sound
然后它应该按预期工作。
或者, 您可以通过在对象内维护“自引用”来防止sound = [[Sound alloc] init];
[sound playSound: @"testSound.mp3" volume: 2 repeats: 2];
对象过早释放,只有在声音播放完毕:
Sound
当然,你不应该以“无限重复”来做到这一点!