2013-05-07 1 views
0

에서 응용 프로그램의 배경 음악을 재생하는 방법을 나는 사용자가 장치 갤러리에서 사운드 파일을 선택할 수있는 응용 프로그램을 개발하고 있으며 응용 프로그램을 실행하는 동안 응용 프로그램의 백그라운드에서 재생이 시작됩니다. 한번 선택하면 사용자가 다시 찾아보고 소리 파일을 변경할 때까지 앱이 실행될 때마다 실행됩니다.IOS : GALLARY

소리 파일을 앱에 복사해야합니까? 또는 경로 만 사용하여 수행 할 수 있습니다.

누군가가 나를 도울 수 있으면 알려 주시기 바랍니다.

답변

0

다음은 요청 된 동작에 대한 코드입니다. appSoundPlayer는 h 파일에 선언 된 AVAudioPlayer의 속성입니다. 또한이 예제는 리소스 폴더에서 노래를 재생합니다.

난 당신이 너무이이 방법을 고려

#pragma mark - 
    #pragma mark *play* 
    - (IBAction) playaction { 

     NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"]; 
     NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
     self.soundFileURL = newURL; 
     [newURL release]; 
     [[AVAudioSession sharedInstance] setDelegate: self]; 
     [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; 

    // Registers the audio route change listener callback function 
    AudioSessionAddPropertyListener (
            kAudioSessionProperty_AudioRouteChange, 
            audioRouteChangeListenerCallback, 
            self 
            ); 

    // Activates the audio session. 

    NSError *activationError = nil; 
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 
    self.appSoundPlayer = newPlayer; 
    [newPlayer release]; 
    [appSoundPlayer prepareToPlay]; 
    [appSoundPlayer setVolume: 1.0]; 
    [appSoundPlayer setDelegate: self]; 
    [appSoundPlayer play]; 


    [stopbutton setEnabled:YES]; 
    [playbutton setEnabled: NO]; 
    playbutton.hidden=YES; 
    pausebutton.hidden =NO; 
}//playbutton touch up inside 

#pragma mark - 
#pragma mark *pause* 
-(IBAction)pauseaction { 
    [appSoundPlayer pause]; 
    pausebutton.hidden = YES; 
    resumebutton.hidden = NO; 

}//pausebutton touch up inside 

#pragma mark - 
#pragma mark *resume* 
-(IBAction)resumeaction{ 
    [appSoundPlayer prepareToPlay]; 
    [appSoundPlayer setVolume:1.0]; 
    [appSoundPlayer setDelegate: self]; 
    [appSoundPlayer play]; 
    playbutton.hidden=YES; 
    resumebutton.hidden =YES; 
    pausebutton.hidden = NO; 

}//resumebutton touch up inside 

#pragma mark - 
#pragma mark *stop* 
-(IBAction)stopaction{ 

    [appSoundPlayer stop]; 
    [playbutton setEnabled:YES]; 
    [stopbutton setEnabled:NO]; 
    playbutton.hidden=NO; 
    resumebutton.hidden =YES; 
    pausebutton.hidden = YES; 

}//stopbutton touch up inside 
+1

applicationDidEnterBackground :와 applicationWillEnterForeground을 일부 변경하고 didFinishLaunchingWithOptions에서 재생할 및 applicationWillTerminate 그것을 중지해야하므로 백그라운드로 재생하려는 내 요구 사항에 따라 이런 짓을 @Prakash 데 사이 고맙습니다하지만이 예에서는, 사운드 파일이 고정되어 있지만 사용자가 갤러리 –

+0

검사에서 선택하려면이 http://developer.apple.com/library/ios/#samplecode/AddMusic/Listings/ReadMe_txt.html# // apple_ref/doc/uid/DTS40008845-ReadMe_txt-DontLinkElementID_9 –

+0

ok ... 곧 처리 할 것입니다 .thnx –