Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从iOS中的资源文件夹中随机选择一个文件_Iphone_Arrays_Cocoa Touch_Ios - Fatal编程技术网

Iphone 从iOS中的资源文件夹中随机选择一个文件

Iphone 从iOS中的资源文件夹中随机选择一个文件,iphone,arrays,cocoa-touch,ios,Iphone,Arrays,Cocoa Touch,Ios,在iOS 4.2 XCode项目中,我的资源文件夹中有1000个.mp3文件 现在,我可以从文件夹中选择一个文件(并播放),在我的.m文件中使用以下代码:(给定文件名“AFile.mp3”) 我希望能够让代码随机选择一个.mp3,播放它,然后选择另一个文件并播放它们,直到它们至少播放一次。我想我需要使用NSArray,但我不确定如何处理 我将代码更改为以下内容,在iOS模拟器中运行时没有收到任何错误。当我运行它时,没有显示任何文件?当我启动应用程序时,它会退出,但不会在调试器中记录任何错误。 更

在iOS 4.2 XCode项目中,我的
资源
文件夹中有1000个
.mp3
文件

现在,我可以从文件夹中选择一个文件(并播放),在我的
.m
文件中使用以下代码:(给定文件名“AFile.mp3”)

我希望能够让代码随机选择一个
.mp3
,播放它,然后选择另一个文件并播放它们,直到它们至少播放一次。我想我需要使用NSArray,但我不确定如何处理

我将代码更改为以下内容,在iOS模拟器中运行时没有收到任何错误。当我运行它时,没有显示任何文件?当我启动应用程序时,它会退出,但不会在调试器中记录任何错误。

更新代码:

-(IBAction)playSound {

NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@".mp3" inDirectory:@"Resources"];
NSString *randomPath = [array objectAtIndex:arc4random() % [array count]];

AVAudioPlayer* theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: randomPath] error: NULL;
    theAudio.delegate = self;
    [theAudio play];

}
如何设置一个包含1000个
.mp3
文件的简单数组,并让该方法随机播放一个

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsofURL: [NSURL arrayURLWithPath:array] error:NULL];
应该是:

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsofURL: [NSURL URLWithString:[array objectAtIndex:11]] error:NULL];
因此,数字“11”是一个随机文件路径。要获得随机性,可以使用以下方法:

int randomNumber = random()%1000; // random number between 0 and 1000
然后使用
randomNumber
而不是
11

若要只播放一次每个文件,您可以播放一次第四首歌曲,然后从阵列中删除此项目。下次生成随机数时,请使用999而不是1000,以此类推,直到它变为1

//编辑:请尝试以下操作:

-(IBAction)playSound {

    NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@".mp3" inDirectory:@"Resources"];
    int rndm = arc4random() % [array count];
    if (rndm<0) rndm*=-1;
    NSString *randomPath = [array objectAtIndex:rndm];

    AVAudioPlayer* theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: randomPath] error: NULL;
    theAudio.delegate = self;
    [theAudio play];

}
-(iAction)播放声音{
NSArray*数组=[[NSBundle mainBundle]路径ForResourceSoftType:@.mp3“目录:@“资源”];
int rndm=arc4random()%[数组计数];

如果(rndm那么,如果您已经有了一个对要从中检索随机元素的数组的引用,那么它非常简单:

NSString *randomPath = [array objectAtIndex:arc4random_uniform([array count])];
另外,请使用此代码段


然后获取数组计数..并使用random获取随机整数和文件。

查看apple文档后,这里的错误是确定的。不知何故,即使我们指定自定义目录,它似乎也只是供参考。我发现的另一个错误是,我们只需要指定扩展名,而不需要“.”

NSArray*audioPathArray=[[NSBundle mainBundle]路径源软件类型:@“mp3”目录:nil]

NSLog(@"%d",[audioPathArray count]);

NSString *path = [audioPathArray objectAtIndex:arc4random() % [audioPathArray count]];

NSURL *randomPath = [[NSURL alloc]initFileURLWithPath:path]; 

AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:randomPath error:NULL];


[theAudio play];

这对我来说很有用。希望它能帮到你。

我的项目成功了

将Resources文件夹导入项目时,请选择“为任何添加的文件夹创建文件夹引用”。这将实际维护[NSBundle mainBundle]中的目录结构

然后可以按如下方式提取内容:

NSString * bundlePath = [NSBundle mainBundle] bundlePath];
NSString * resourceFolderPath = [NSString stringWithFormat:@"%@/Resources", bundlePath];
NSArray * resourceFiles = [[NSFileManager defaultManager] directoryContentsAtPath:resourceFolderPath error:nil];
resourceFiles应该包含资源目录的内容列表

NSInteger randomFileIndex = arc4random() % [resourceFiles count];
NSString * randomFile = [resourceFiles objectAtIndex:randomFileIndex];

arrayURLWithPath:
从何而来?@iPortable再次感谢。当我现在运行时,行int rndm=arc4random()%[array count];在GDB中发出此命令:程序接收信号:“EXC_算术”,然后是NSLog()您的*数组中似乎没有任何值。谢谢..我正在这样做,但确实发现数组是空的..我可能会提出一个单独的问题,说明为什么,除非您在方法的第一行中看到一些错误?在/Resources文件夹中肯定有mp3文件…不确定为什么没有添加到数组中…我更改了资源对于“.”,这似乎解决了问题。URLWithString:或fileURLWithPath:我不知道您在内部调用什么函数谢谢,我采纳了您的建议,但仍然没有成功…请参见上面的edit.yep,正如我所想(因此我使用了random()而不是arc4random())。您可以在arc4中获得负数。单独保存随机数并检查其是否为负数:int rm=arc4random();if(rm)当前解决方案似乎没有考虑将每个文件播放一次。为什么不按照递归函数的思路添加一些内容(这可能会占用一组未播放的文件)并通过下一次迭代,例如
[[NSMutableArray arrayWithArray:array]removeObject:randomPath];
?如果随机选择声音播放给用户,
arc4random()
将不会更有用。用户不会在意使用哪个RNG。:)@乔纳森·格林斯潘:不过,养成使用
arc4random()
vs
rand()
的习惯还是不错的。我想你是说
rand()
;p@Jonathan格林斯潘:哈哈,我现在涉猎的语言太多了。@Jacob谢谢,我加入了arc4random()…但还是挂断了。请看上面的编辑。我看到了一个在4.2.1中运行此代码时出现“directoryContentsAtPath已弃用”警告。
NSString * bundlePath = [NSBundle mainBundle] bundlePath];
NSString * resourceFolderPath = [NSString stringWithFormat:@"%@/Resources", bundlePath];
NSArray * resourceFiles = [[NSFileManager defaultManager] directoryContentsAtPath:resourceFolderPath error:nil];
NSInteger randomFileIndex = arc4random() % [resourceFiles count];
NSString * randomFile = [resourceFiles objectAtIndex:randomFileIndex];