2013-06-02 174 views
9

我無法弄清楚如何在iPhone遊戲中播放音樂文件。在iOS應用中播放mp3文件

我創建一個遊戲,我試圖找出如何在應用程序啓動時播放音樂。

我嘗試這樣做:

- (void)awakeFromNib { 

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

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

    [theAudio play]; 


} 
+0

你可能想看看這個前面的問題。 http://stackoverflow.com/questions/1296786/how-do-i-programmatically-play-an-mp3-on-an-iphone – dotToString

回答

29

這就是你是如何做到的。在您的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]; 
    } 
+0

我很喜歡工作!謝謝大家! – user2444410

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]; 
} 
+1

「請注意,你應該持有一個參考,否則音樂將不會播放「。救了我 –

1

對於雨燕3.1:

使用該兩名外援:

0爲發揮

private var mAudioPlayer : AVAudioPlayer? 

使用此功能你是存儲在您的應用程序一個聲音:

import AVFoundation 
import AudioToolbox 

爲您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() 
}