10

我不知道如何在 iPhone 游戏中播放音乐文件。

我正在创建一个游戏,我试图弄清楚每当应用程序启动时如何播放音乐。

我试过这个:

- (void)awakeFromNib {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"musicamenu" ofType:@"mp3"];

    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];


}
4

3 回答 3

30

这就是你的做法。在您的 v1AppDelegate.h 文件中添加

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

@interface v1AppDelegate : UIResponder <UIApplicationDelegate>
{
    AVAudioPlayer *myAudioPlayer;
}

@property (nonatomic, retain) AVAudioPlayer *myAudioPlayer;

@property (strong, nonatomic) UIWindow *window;

@end

现在在你的 v1AppDelegate.m 文件中添加这个

#import "v1AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@implementation v1AppDelegate

@synthesize window = _window;

@synthesize myAudioPlayer;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //start a background sound
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Soothing_Music2_Long" ofType: @"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];    
    myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    myAudioPlayer.numberOfLoops = -1; //infinite loop
    [myAudioPlayer play];


    // Override point for customization after application launch.
    return YES;
}

如果您希望在代码中的任何其他位置停止或开始播放此音乐,只需添加此

#import "v1AppDelegate.h"    
- (IBAction)stopMusic
    {
        v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        [appDelegate.myAudioPlayer stop];
    }


    - (IBAction)startMusic
    {
        v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        [appDelegate.myAudioPlayer play];
    }
于 2013-06-02T12:48:41.723 回答
2

我建议在方法中添加播放音乐applicationDidBecomeActive:方法。因为您希望每次启动应用程序时都播放音乐。请注意,您应该持有对播放器的引用。否则音乐将不会播放。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Play music on another queue so that the main queue is not blocked.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self playMusic];
    });
}

- (void)playMusic
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"done" ofType:@"mp3"];
    NSError *error = nil;
    NSURL *url = [NSURL fileURLWithPath:path];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    [self.player play];
}
于 2013-06-02T10:19:52.720 回答
2

对于 Swift 3.1:

使用这两个进口:

import AVFoundation
import AudioToolbox

为您的 AVAudioPlayer 创建参考:

private var mAudioPlayer : AVAudioPlayer?

使用此功能播放您存储在应用程序中的声音:

func onTapSound(){
        let soundFile = Bundle.main.path(forResource: "slap_placeholder.wav", ofType: nil)
        let url = URL(fileURLWithPath: soundFile!)
        self.mAudioPlayer = try! AVAudioPlayer(contentsOf: url as URL)
        self.mAudioPlayer?.play()
}
于 2017-05-15T12:55:06.900 回答