使用 NSObject 中的 AVAudioPlayer 播放音频 class
Play audio with AVAudioPlayer inside NSObject class
为了组织事情,我决定创建一个名为 SoundPlayer 的 class,其中 运行 来自我的应用程序的所有音频文件。 (这样可以避免有很多重复代码)
SoundPlayer.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#include <AudioToolbox/AudioToolbox.h>
@interface SoundPlayer : NSObject <AVAudioPlayerDelegate>
@property (strong,nonatomic) AVAudioPlayer *backgroundMusicPlayer;
-(void)PlaySound:(NSString*)name extension:(NSString*)ext loops:(int)val;
@end
SoundPlayer.m
#import "SoundPlayer.h"
@implementation SoundPlayer
-(void)PlaySound:(NSString *)name extension:(NSString *)ext loops:(int)val{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:ext];
NSURL *soundPath = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSError *error;
self.backgroundMusicPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundPath error:&error];
self.backgroundMusicPlayer.numberOfLoops = val;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
}
@end
这段代码非常简单,而且效果很好。当用户第一次打开我的应用程序时,我想播放声音,为此我在 didFinishLaunchingWithOptions 中调用 class,像这样:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SoundPlayer *sound = [[SoundPlayer alloc] init];
[sound PlaySound:@"preview" extension:@"mp3" loops:0];
return YES;//Diz que o retorno esta ok!
}
问题是声音没有被执行(现在,如果我复制 SoundPlayer class 中的所有代码并放入 class 我会用,声音 运行 完美)有什么问题吗?
您的 SoundPlayer
class 超出范围并被释放,这使声音静音。
将其分配给应用委托中的成员变量:
self.sound = [[SoundPlayer alloc] init];
[sound PlaySound:@"preview" extension:@"mp3" loops:0];
试试这个:
AppDelegate.h
#import <UIKit/UIKit.h>
#import "SoundPlayer.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic) SoundPlayer * soundPlayer;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "SoundPlayer.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.soundPlayer = [[SoundPlayer alloc] init];
[self.soundPlayer PlaySound:@"preview" extension:@"mp3" loops:0];
return YES;
}
为了组织事情,我决定创建一个名为 SoundPlayer 的 class,其中 运行 来自我的应用程序的所有音频文件。 (这样可以避免有很多重复代码)
SoundPlayer.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#include <AudioToolbox/AudioToolbox.h>
@interface SoundPlayer : NSObject <AVAudioPlayerDelegate>
@property (strong,nonatomic) AVAudioPlayer *backgroundMusicPlayer;
-(void)PlaySound:(NSString*)name extension:(NSString*)ext loops:(int)val;
@end
SoundPlayer.m
#import "SoundPlayer.h"
@implementation SoundPlayer
-(void)PlaySound:(NSString *)name extension:(NSString *)ext loops:(int)val{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:ext];
NSURL *soundPath = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSError *error;
self.backgroundMusicPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundPath error:&error];
self.backgroundMusicPlayer.numberOfLoops = val;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
}
@end
这段代码非常简单,而且效果很好。当用户第一次打开我的应用程序时,我想播放声音,为此我在 didFinishLaunchingWithOptions 中调用 class,像这样:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SoundPlayer *sound = [[SoundPlayer alloc] init];
[sound PlaySound:@"preview" extension:@"mp3" loops:0];
return YES;//Diz que o retorno esta ok!
}
问题是声音没有被执行(现在,如果我复制 SoundPlayer class 中的所有代码并放入 class 我会用,声音 运行 完美)有什么问题吗?
您的 SoundPlayer
class 超出范围并被释放,这使声音静音。
将其分配给应用委托中的成员变量:
self.sound = [[SoundPlayer alloc] init];
[sound PlaySound:@"preview" extension:@"mp3" loops:0];
试试这个:
AppDelegate.h
#import <UIKit/UIKit.h>
#import "SoundPlayer.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic) SoundPlayer * soundPlayer;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "SoundPlayer.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.soundPlayer = [[SoundPlayer alloc] init];
[self.soundPlayer PlaySound:@"preview" extension:@"mp3" loops:0];
return YES;
}