2013-06-02 5 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 "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; 
} 

를 추가하려면 v1AppDelegate.m 파일 지금

#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 

를 추가

사용이이 개 수입 : 스위프트 3.1

#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

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