2013-06-02 38 views
9

Tôi không thể tìm ra cách phát tệp nhạc trong trò chơi trên iPhone.phát tệp mp3 trong ứng dụng iOS

Im tạo trò chơi và tôi đang cố gắng tìm hiểu cách phát nhạc bất cứ khi nào ứng dụng được khởi chạy.

Tôi cố gắng này:

- (void)awakeFromNib { 

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

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

    [theAudio play]; 


} 
+0

bạn có thể muốn xem xét câu hỏi trước đây. http://stackoverflow.com/questions/1296786/how-do-i-programmatically-play-an-mp3-on-an-iphone – dotToString

Trả lời

29

Đây là cách bạn thực hiện. Trong tập tin v1AppDelegate.h bạn thê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 

Bây giờ trong file v1AppDelegate.m bạn thêm này

#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; 
} 

Nếu bạn muốn dừng hoặc khởi động âm nhạc này từ bất cứ nơi nào khác trong mã của bạn sau đó chỉ cần thêm

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


    - (IBAction)startMusic 
    { 
     v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     [appDelegate.myAudioPlayer play]; 
    } 
+0

TÔI ĐÃ LÀM VIỆC TIẾT KIỆM LÀM VIỆC !! CẢM ƠN MỌI NGƯỜI! – user2444410

2

Tôi khuyên bạn nên thêm các phương pháp chơi nhạc trong applicationDidBecomeActive: phương pháp. Vì bạn muốn nhạc được phát mỗi khi ứng dụng được khởi chạy. Lưu ý bạn nên giữ một tham chiếu đến trình phát. Khác âm nhạc sẽ không được phát.

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

"Lưu ý bạn nên giữ tham chiếu đến trình phát. Âm nhạc khác sẽ không được phát . " đã cứu tôi –

1

Đối Swift 3.1:

Sử dụng này hai nhập khẩu:

import AVFoundation 
import AudioToolbox 

Tạo một tài liệu tham khảo cho AVAudioPlayer của bạn:

private var mAudioPlayer : AVAudioPlayer? 

Sử dụng chức năng này để chơi một âm thanh bạn đang lưu trữ trong ứng dụng của bạn:

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() 
} 
Các vấn đề liên quan