gpt4 book ai didi

ios - iOS应用程式当机-[NSURL initFileURLWithPath :]

转载 作者:行者123 更新时间:2023-12-01 17:41:23 24 4
gpt4 key购买 nike

我是一名业余ios开发人员,其版本为4.6的xcode和OSX 10.8.4。我正在尝试制作一个播放声音的应用程序,但是当我尝试运行该应用程序时,出现以下消息:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSURL initFileURLWithPath:]:无字符串参数'

我的应用也不会启动。

这是我的view controller.h的代码:

      ViewController.h:

#import <UIKit/UIKit.h>
#import <RevMobAds/RevMobAds.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController



- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;

@end

这是我的 View Controller .m:
      Viewcontroller.m:


#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>


@interface ViewController ()
{
AVAudioPlayer *avPlayer;
}
@end

@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:stringPath];

NSError *error;

avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];

[avPlayer setNumberOfLoops:-1];


}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}


- (IBAction)playButton:(id)sender {
[avPlayer play];
}

- (IBAction)stopButton:(id)sender {
[avPlayer pause];

}
@end

最佳答案

问题是pathForResource返回nil。您是否已将确切的文件名添加到包中?

另外,在继续加载播放器之前,您应该先测试stringPatherror。例如:

NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];
if (stringPath) {
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if (!error) {
[avPlayer setNumberOfLoops:-1];
} else {
NSLog(@"Error occurred: %@", [error localizedDescription]);
}
} else {
NSLog(@"Resource not found");
}

关于ios - iOS应用程式当机-[NSURL initFileURLWithPath :],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com