Objective c 发出随机的声音

Objective c 发出随机的声音,objective-c,ios,random,audio,Objective C,Ios,Random,Audio,如何在iOS5的xcode中播放随机声音? 我一直收到一个“抛出异常”错误 我试过这个: int randomNumber = arc4random() % 24 + 1; NSString *tmpFileNameRandom = [[NSString alloc] initWithFormat:@"Sound%d", randomNumber]; NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName

如何在iOS5的xcode中播放随机声音? 我一直收到一个“抛出异常”错误

我试过这个:

int randomNumber = arc4random() % 24 + 1;

NSString *tmpFileNameRandom = [[NSString alloc] initWithFormat:@"Sound%d", randomNumber];

NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileNameRandom ofType:@"mp3"];

AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fileName] error:nil];

[soundPlayer prepareToPlay];
[soundPlayer play];

谢谢大家!

您是否已将AVFoundation框架添加到您的项目中?AVAudioPlayer就是这个框架的一个类


要添加此框架(在Xcode 4中),请选择您的项目文件,选择“摘要”选项卡,然后通过单击“链接的框架和库”表视图下方的+来添加它。

您是否已将AVFoundation框架添加到您的项目中?AVAudioPlayer就是这个框架的一个类


要添加此框架(在Xcode 4中),请选择您的项目文件,选择“摘要”选项卡,然后通过单击“链接的框架和库”表视图下方的+来添加它。

对于您应该使用的文件名

NSString *tmpFileNameRandom = [[NSString alloc] initWithFormat:@"Sound%02d", randomNumber];
这将为您提供数字小于10的前导零

或者更好的做法是,尝试以下方法:

int randomNumber = arc4random() % 24 + 1;

NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];

AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

[soundPlayer prepareToPlay];
[soundPlayer play];

对于您的文件名,您应该使用

NSString *tmpFileNameRandom = [[NSString alloc] initWithFormat:@"Sound%02d", randomNumber];
这将为您提供数字小于10的前导零

或者更好的做法是,尝试以下方法:

int randomNumber = arc4random() % 24 + 1;

NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];

AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

[soundPlayer prepareToPlay];
[soundPlayer play];

首先,将ViewController.h更改为

#import <UIKit/UIKit.h>

@class AVAudioPlayer;

@interface ViewController : UIViewController

-(IBAction)PlayRandomSound;
@property (nonatomic, retain) AVAudioPlayer *soundPlayer;


@end
#导入
@类音频层;
@界面ViewController:UIViewController
-(i)播放声音;
@属性(非原子,保留)AVAudioPlayer*soundPlayer;
@结束
和ViewController.m到的第一行

#import "ViewController.h"

#import <AVFoundation/AVAudioPlayer.h>


@implementation ViewController

@synthesize soundPlayer = _soundPlayer;


-(IBAction)PlayRandomSound{

    int randomNumber = arc4random() % 8 + 1;

    NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];


    _soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

    [_soundPlayer prepareToPlay];
    [_soundPlayer play];


    NSLog(@"randomNumber is %d", randomNumber);
    NSLog(@"tmpFilename is %@", soundURL);
}
#导入“ViewController.h”
#进口
@实现视图控制器
@合成声音播放器=_声音播放器;
-(i动作)播放声音{
int randomNumber=arc4random()%8+1;
NSURL*soundURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@“Sound%02d”,randomNumber]类型:@“mp3”];
_soundPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:soundURL错误:nil];
[_soundplayerpreparetoplay];
[音乐播放器播放];
NSLog(@“随机数为%d”,随机数);
NSLog(@“tmpFilename是%@”,soundURL);
}

编辑:我只是后来才注意到你没有使用ARC,所以这个代码有一个小漏洞。但从一开始就可以了。也许你应该在创建ViewController时将_soundPlayer设置为nil,然后检查:如果不是nil:释放它并创建一个新的,否则只创建一个新的。如果你是一个新项目,你可以考虑切换到ARC。

首先,改变你的VIEW控制器。

#import <UIKit/UIKit.h>

@class AVAudioPlayer;

@interface ViewController : UIViewController

-(IBAction)PlayRandomSound;
@property (nonatomic, retain) AVAudioPlayer *soundPlayer;


@end
#导入
@类音频层;
@界面ViewController:UIViewController
-(i)播放声音;
@属性(非原子,保留)AVAudioPlayer*soundPlayer;
@结束
和ViewController.m到的第一行

#import "ViewController.h"

#import <AVFoundation/AVAudioPlayer.h>


@implementation ViewController

@synthesize soundPlayer = _soundPlayer;


-(IBAction)PlayRandomSound{

    int randomNumber = arc4random() % 8 + 1;

    NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];


    _soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

    [_soundPlayer prepareToPlay];
    [_soundPlayer play];


    NSLog(@"randomNumber is %d", randomNumber);
    NSLog(@"tmpFilename is %@", soundURL);
}
#导入“ViewController.h”
#进口
@实现视图控制器
@合成声音播放器=_声音播放器;
-(i动作)播放声音{
int randomNumber=arc4random()%8+1;
NSURL*soundURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@“Sound%02d”,randomNumber]类型:@“mp3”];
_soundPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:soundURL错误:nil];
[_soundplayerpreparetoplay];
[音乐播放器播放];
NSLog(@“随机数为%d”,随机数);
NSLog(@“tmpFilename是%@”,soundURL);
}


编辑:我只是后来才注意到你没有使用ARC,所以这个代码有一个小漏洞。但从一开始就可以了。也许你应该在创建ViewController时将_soundPlayer设置为nil,然后检查:如果不是nil:释放它并创建一个新的,否则只创建一个新的。或者,如果你是一个新项目,你可以考虑切换到ARC。

你有没有尝试记录文件路径/名称,这些是否正确等等?还有什么异常被抛出,以及什么。%D应该是好的。它们是等价的(%d不是“double”,如果你想知道的话)。你每次都有例外吗?您的声音文件名为Sound1.mp3到Sound24.mp3吗?请至少给我们一个线索。。。。。如何发布异常的堆栈跟踪。什么不起作用?寂静崩溃后台进程突然出现?您是否尝试过记录文件路径/名称,这些路径/名称是否正确,等等引发了什么异常以及由什么引发的异常。%d应该可以-它们是等效的(%d不是“double”,以防您感到奇怪)。您是每次还是偶尔都会遇到异常?您的声音文件名为Sound1.mp3到Sound24.mp3吗?请至少给我们一个线索。。。。。如何发布异常的堆栈跟踪。什么不起作用?寂静崩溃从你鼻子里冒出来的守护进程?这是正确的答案-如果你希望人们继续帮助你,你应该接受它。嗨,文件名现在已经确认正确,但它仍然不能播放实际的声音。soundURL的NSLog返回Sound02.mp3实例,它是xcode项目中的有效文件。我没有收到任何错误,iphone的声音打开了,mac的声音打开了,但什么都没有…嗯,你的代码看起来不错-应该可以播放了。确保三件事:你的文件名每个字符都匹配(大写字母S),并且格式正确(mp3?)。文件也应该包含在项目中(右键单击project viewer->将文件添加到项目中)。此外,不要忘记将AVFoundation.framework包含到项目中,并将“AVFoundation/AVAudioPlayer.h”导入到调用AVAudioPlayer的文件中。我在这里上传了测试项目,我不再有任何线索。开始认为可能是xcode。。。这是正确的答案-如果你希望人们继续帮助你,你应该接受它。嗨,文件名现在已经确认正确,但它仍然不能播放实际的声音。soundURL的NSLog返回Sound02.mp3实例,它是xcode项目中的有效文件。我没有收到任何错误,iphone的声音打开了,mac的声音打开了,但什么都没有…嗯,你的代码看起来不错-应该可以播放了。确保三件事:你的文件名每个字符都匹配(大写字母S),并且格式正确(mp3?)。文件也应该包含在项目中(右键单击project viewer->将文件添加到项目中)。此外,不要忘记将AVFoundation.framework包含到项目中,并将“AVFoundation/AVAudioPlayer.h”导入到调用AVAudioPlayer的文件中。我上传了te