iOSでサウンドファイルを再生する方法を示す簡単なサンプルアプリを作成しています。
このために、1つのViewControllerを使用して非常に単純なXCodeプロジェクトを作成しました。ただし、AppDelegate.hファイルと.mファイルは編集されていないままですが、AppDelegate.mで奇妙な解析の問題が発生しています。
@Implimentation行で、コンパイラは欠落している'@end'を通知します。
行-(BOOL)アプリケーション:(UIApplication )アプリケーションdidFinishLaunchingWithOptions:(NSDictionary)起動オプションそれは私に期待される';'を教えてくれます メソッドのプロトタイプの後。
この問題は、AppDelegate.mファイルの#import"ViewController.h"参照に起因しているようです。これを削除すると、これら2つのエラーがなくなり、クラスメッセージのReceiver'ViewController'が前方宣言に置き換えられます。これは、インポートがない場合に予想されることです。
これは奇妙な問題です。以前にいくつかのIOSアプリを作成しましたが、この問題は発生していません。背景情報として、プロジェクトはXCode4でシングルビューアプリとして作成されました。ViewController.hのIBOutletsとプロパティをInterfaceBuilderのXIBに適切に並べました。また、AudioToolboxフレームワークに、ビルドフェーズ>ライブラリとライブラリのリンク機能を介して追加しました。
これがデリゲートとビューコントローラファイルファイルです
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
ViewContoller.m
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
SystemSoundID pig;
SystemSoundID cow;
SystemSoundID sheep;
SystemSoundID chicken;
@end
@implementation ViewController
@Synthesize but_cow, but_pig, but_sheep, but_chicken;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString * cowSoundURL= [[NSBundle mainBundle] pathForResource:@"cow" ofType: @"mp3"];
NSString * pigSoundURL= [[NSBundle mainBundle] pathForResource:@"pig" ofType: @"mp3"];
NSString * sheepSoundURL= [[NSBundle mainBundle] pathForResource:@"sheep" ofType: @"mp3"];
NSString * chiickenSoundURL= [[NSBundle mainBundle] pathForResource:@"chicken" ofType: @"mp3"];
AudioServicesCreateSystemSoundID(cowSoundURL, &cow);
AudioServicesCreateSystemSoundID(pigSoundURL, &pig);
AudioServicesCreateSystemSoundID( sheepSoundURL, &sheep);
AudioServicesCreateSystemSoundID(chickenSoundURL, &chicken);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//====================================================
/**
Called when a button is pressed
**/
//====================================================
-(IBAction)buttonPressed:(id)sender
{
if (sender == but_cow)
{
AudioServicesPlaySystem(cow);
}
else if (sender == but_sheep)
{
AudioServicesPlaySystem(sheep);
}
else if (sender == but_pig)
{
AudioServicesPlaySystem(pig);
}
else if (sender == but_chicken)
{
AudioServicesPlaySystem(chicken);
}
}//===================================================
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIButton * but_cow;
@property (nonatomic, retain) IBOutlet UIButton * but_pig;
@property (nonatomic, retain) IBOutlet UIButton * but_sheep;
@property (nonatomic, retain) IBOutlet UIButton * but_chicken;
-(IBAction)buttonPressed:(id)sender;
これをお読みいただき、誠にありがとうございます。