2012-07-02 4 views
3

최근에 시작한 앱에 대한 도움이 필요합니다. 나는 코딩에 매우 큰 출발점 이니 제발 도와주세요, 많은 의미가 있습니다. 사용자가 응용 프로그램을 시작하자마자 연주 할 음악이 필요합니다. 다른 YouTube 튜토리얼을 따라 왔지만 이전 Xcode 버전에서만 작동합니다. 너무 감사드립니다.앱 시작과 동시에 자동으로 음악을 재생하는 방법은 무엇입니까?

+0

내가 어떻게 선수를 선언해야 – Bazinga

답변

4

를 가져올 기억 어떻게 application didFinishLaunching에 넣어하지만있는 .h와하는 .m에서 인스턴스화해야합니다 대한.

[player stop]; 

를하거나 일시 :

[player pause]; 

을도에 가져올 당신이 그것을 중지하려면 다음

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

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.wav"]; 
    NSLog(@"Path to play: %@", resourcePath); 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     player.delegate = self; 
     [player play]; 
     player.numberOfLoops = -1; 
     player.currentTime = 0; 
     player.volume = 1.0; 
    } 
} 

이 같은

뭔가 문제를 어떻게해야 헤더 파일 :

,

편집 :

당신은 당연히 헤더에 선언해야, 다음을 합성.

@ 인터페이스에의 ViewController : UIViewController에 < AVAudioPlayerDelegate> {

AVAudioPlayer *player; 
} 
@property (nonatomic, retain) AVAudioPlayer *player; 

//.m

@synthesize player; 
+0

을 appdidfinishlaunching에 당신의 MP3를 올리지는 어떻습니까? 왜냐하면 지금은 "선언되지 않은 식별자 '플레이어'사용 덕분에 오류가 발생하기 때문에 감사합니다. 그리고 내가 스토리 보드에있는 아무 것도 연결한다고 선언한다면? – user1483652

+0

내 편집 된 답변을 확인하고 문제가 계속 발생하는 경우 – Bazinga

+0

모든 도움을 주셔서 감사합니다. 귀하가 저에게 말했던 모든 오류가 사라졌지만 시뮬레이션을 수행 할 때 음악이 재생되지 않습니다.보고 싶습니까? 코드 또는 그와 비슷한 무엇입니까 ?? :) 감사 – user1483652

0

AVAudioPlayer을 사용할 수 있습니다. 그것은 꽤 똑바로 앞으로 사용하기 :

NSURL *path = [[NSURL alloc] initFileURLWithPath:[DataPersister getQualifiedPathForFileInDirectory:DataPersisterPathBundle fileName:@"music.mp3"]]; 
NSError *outError; 

AVAudioPlayer *musicSound = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:&outError]; 
[musicSound play]; 
[path release]; 

#import <AVFoundation/AVFoundation.h> 
0
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MUSIC.mp3", [[NSBundle mainBundle] resourcePath]]]; 
NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
audioPlayer.numberOfLoops = -1; 
[audioPlayer play]; 
audioPlayer.volume = 1.0; 
audioPlayer.currentTime = 2; 

//.h과는 굵은 부분을 추가

사용 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 방법에서이 있는지

해피 코딩 음악을 재생됩니다)

관련 문제