Objective c AVAUDIOPLAYOS无声音
似乎有人问过类似的问题,但似乎没有一个解决方案适合我。我一辈子都不能理解为什么我的代码不起作用,就在这里。 我没有收到任何错误,但也没有收到任何声音Objective c AVAUDIOPLAYOS无声音,objective-c,Objective C,似乎有人问过类似的问题,但似乎没有一个解决方案适合我。我一辈子都不能理解为什么我的代码不起作用,就在这里。 我没有收到任何错误,但也没有收到任何声音 @interface MainApp () @property (strong, nonatomic) AVAudioPlayer *audioPlayer; -(void)playSoundFX:(NSString*) soundFile; @end @implementation MainApp - (void)viewDidLoad
@interface MainApp ()
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
@end
@implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:@“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer setVolume:100.0f];
if (audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[audioPlayer play];
}
@end
*********vvvvvvvvv Amended Code that works vvvvvvvvv**********
@interface MainApp ()
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
@end
@implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:@“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.audioPlayer setVolume:100.0f];
if (self.audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[self.audioPlayer play];
}
@end
我正在我的iPhone上执行你的代码。它工作正常。请选择一个设备并运行代码。我希望它能正确地执行声音。您必须将功能分开 在viewDidLoad中加载音频文件并在play action中播放,类似这样的事情
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
AVAudioPlayer *_audioPlayer;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playSoundTapped:(id)sender
{
// When button is tapped, play sound
[_audioPlayer play];
}
#导入“ViewController.h”
#进口
@界面视图控制器()
{
AVAudioPlayer*\u音频播放器;
}
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//构造声音文件的URL
NSString*path=[NSString stringWithFormat:@“%@/sound.mp3”,[[NSBundle mainBundle]resourcePath]];
NSURL*soundUrl=[NSURL fileURLWithPath:path];
//创建音频播放器对象,并使用URL初始化声音
_audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:soundUrl错误:nil];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)播放声音:(id)发送方
{
//当按下按钮时,播放声音
[音频播放器播放];
}
这太疯狂了,因为我正试图在我的iPhone 6上播放它,设备上的声音是打开的,而且音量也增加了,但我仍然听不到我的声音文件。我找到了答案,但我不能百分之百确定我有正确的解释来解释为什么会这样。我在接口中声明了audioPlayer的一个实例属性,打算使用它,但没有将self.audioPlayer分配给alloc init方法,它看起来像是我在该方法中创建了另一个audioPlayer,并使用了该audioPlayer。我认为在声音播放之前,实例将超出范围或取消分配。这是一个准确的评估吗?阅读您的评论真的很有帮助和有趣,但答案实际上在于我使用(或不使用)audioPlayer属性实例的方式。我注意到你在使用下划线,所以我又看了一眼我是如何声明我的,它们不一致,这就是它不起作用的原因。(阅读我上面的评论)再次感谢你的帮助。