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

标签 ios objective-c macos compiler-errors

我是一名业余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/

相关文章:

objective-c - 使用重叠添加 CMSampleBufferRef 执行缓冲/加窗

ios - 如何通过 podfile 共享两个目标中的一些 pod

c# - 如何在 OS X 和 Linux 上安装 MSBuild?

ios - 在提供激励之前如何查看 vungle 广告是否已加载并完成?

ios - 将数据(标签)从 TableViewCell 传递到另一个 ViewController

ios - 我想在开始输入时将文本字段颜色的底层更改为蓝色

ios - 无法在我的 iOS9 应用程序中打印 FBSDKAccessToken.currentAccessToken().tokenString

macos - 如何在 macOS 上的 Sublime Text 中在同一窗口中打开文件和文件夹?

git -/.git/hooks/: No such file or directory protocol error: expected control record on Mac osx

ios - 验证iOS APP时出现IDFA相关警告?