Swift 如何将MP3背景循环添加到Interface Builder生成的页面中?

Swift 如何将MP3背景循环添加到Interface Builder生成的页面中?,swift,ios8,xcode6,interface-builder,Swift,Ios8,Xcode6,Interface Builder,我不熟悉iOS/Xcode/Swift,需要一种将音频文件插入页面背景的解决方案,人们阅读页面时,该文件将播放。我建议您查看: 对于如何获得一个工作的音频播放器。 在一分钟的搜索后,我找到了你问题的“背景”部分。我希望这两个问题能进一步帮助你我也遇到了同样的问题,我所做的是在我的AppDelegate中创建AVAudioPlayer,然后创建一个AppDelegate实例,这样AVAudioPlayer实例将是全局的,在我所需的ViewController中,在我调用的[player play]

我不熟悉iOS/Xcode/Swift,需要一种将音频文件插入页面背景的解决方案,人们阅读页面时,该文件将播放。

我建议您查看: 对于如何获得一个工作的音频播放器。
在一分钟的搜索后,我找到了你问题的“背景”部分。我希望这两个问题能进一步帮助你

我也遇到了同样的问题,我所做的是在我的AppDelegate中创建AVAudioPlayer,然后创建一个AppDelegate实例,这样AVAudioPlayer实例将是全局的,在我所需的ViewController中,在我调用的[player play]的ViewWillAspect方法中;方法如果您想在视图消失时暂停音乐,如移动到另一个ViewController时,只需调用[player pause];视图中的方法将消失

AppDelegate.h文件应该是什么样子的:

#import <UIKit/UIKit.h>
@import AVFoundation;

@interface AppDelegate : UIResponder <UIApplicationDelegate,     AVAudioPlayerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (retain,nonatomic) AVAudioPlayer *myPlayer;
@property (retain, nonatomic) AVAudioSession *session;


@end
 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSError *error;
session = [AVAudioSession sharedInstance];
[session setCategory: AVAudioSessionCategorySoloAmbient error:&error];
NSString *path = [[NSBundle mainBundle] pathForResource:kSongTitle ofType:kSongFormat];
NSURL *pathURL = [NSURL fileURLWithPath:path];
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:pathURL error:&error];
[myPlayer setNumberOfLoops:-1.0];//for infinite loops set the value to a negative number..
[myPlayer setVolume:0.7];//this value varies between 0 and 1..

return YES;
}
所需ViewController的.h文件中的AppDelegate实例:

@property (nonatomic, strong) AppDelegate *appDelegate;
您的视图将出现,视图将消失方法:

-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[appDelegate.myPlayer pause];

}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[appDelegate.myPlayer play];

}
另外,如果你想处理干扰,比如说接到一个电话,只需拨打[myPlayer暂停];在AppDelegate.m文件中的applicationWillResignActive方法中。要恢复音频,只需调用[myPlayer prepareToPlay];和[我的玩家玩];在AppDelegate.m文件中的ApplicationIDBecMeactive方法中

我用的是Objective-C