Question

I am developing an app in which user can select a sound file from device gallery and it will start playing in app background while app runs. once selected it will run every time when app runs till user again browse and changes sound file.

do I need to copy sound file to app? or it can be done using its path only.

please let me know if anybody can help me.

Was it helpful?

Solution

These are the codes for the requested actions, appSoundPlayer is a property of AVAudioPlayer declared in h file. Also this example plays a song in the resource folder.

I did this as per my requirement you want to play it in background so you need to make some changes and play it in didFinishLaunchingWithOptions and stop it in applicationWillTerminate take into consideration this 2 methods too applicationDidEnterBackground applicationWillEnterForeground

#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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top