London
London

Reputation: 15284

Playing sound while navigating between view controllers

My application plays online streams.

However when user navigates to the next view controller the music stops. Here is the code that plays sound:

- (void)start
{
    @synchronized (self)
    {
        if (state == AS_PAUSED)
        {
            [self pause];
        }
        else if (state == AS_INITIALIZED)
        {
            NSAssert([[NSThread currentThread] isEqual:[NSThread mainThread]],
                @"Playback can only be started from the main thread.");
            notificationCenter =
                [[NSNotificationCenter defaultCenter] retain];
            self.state = AS_STARTING_FILE_THREAD;
            internalThread =
                [[NSThread alloc]
                    initWithTarget:self
                    selector:@selector(startInternal)
                    object:nil];
            [internalThread setName:@"InternalThread"];
            [internalThread start];
        }
    }
}

I've tried :

dispatch_async(dispatch_get_main_queue(), 
                           ^{
                               [internalThread start];
                           });

But it doesn't work. Does anyone have experience with this?

Upvotes: 0

Views: 389

Answers (1)

djleop
djleop

Reputation: 697

You have to keep a reference to your AS object.

In my case, I've wrapped AS in a singleton.

@implementation MyClass

static AudioStreamer *sharedStreamer = nil;

+(AudioStreamer *)sharedStreamer{
    return sharedStreamer;
}
-(void)play{
    playing=YES;
}

-(void)stop{
    playing=NO;
}    

-(IBAction)playStop{
    if (!self.playing)
    {       
        [self createStreamer];
        [[MyClass sharedStreamer] start];
    }
    else
    {
        [[MyClass sharedStreamer] stop];
    }

}    

- (void)viewDidLoad {
    [super viewDidLoad];

    if([MyClass sharedStreamer]){
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(playbackStateChanged:)
         name:ASStatusChangedNotification
         object:[MyClass sharedStreamer]];
        [self playbackStateChanged:nil];
    }
}

//
// createStreamer
//
// Creates or recreates the AudioStreamer object.
//
- (void)createStreamer{
    if ([MyClass sharedStreamer]){
        return;
    }

    [self destroyStreamer];

    NSString *escapedValue =
    [(NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                             nil,
                                                         (CFStringRef)@"http://url.com"],
                                                         NULL,
                                                         NULL,
                                                         kCFStringEncodingUTF8)
     autorelease];

    NSURL *url = [NSURL URLWithString:escapedValue];
    sharedStreamer = [[AudioStreamer alloc] initWithURL:url];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:ASStatusChangedNotification
     object:[MyClass sharedStreamer]];
}

//
// playbackStateChanged:
//
// Invoked when the AudioStreamer
// reports that its playback status has changed.
//
- (void)playbackStateChanged:(NSNotification *)aNotification {
    if ([[MyClass sharedStreamer] isWaiting]){
        playing = YES ;
    } else if ([[MyClass sharedStreamer] isPlaying]) {
        playing = YES;
    } else if ([[MyClass sharedStreamer] isIdle]) {
        playing = NO;
            [self destroyStreamer];
    }
}

//
// destroyStreamer
//
// Removes the streamer, the UI update timer and the change notification
//
- (void)destroyStreamer{
    if ([MyClass sharedStreamer]){
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:ASStatusChangedNotification
         object:[MyClass sharedStreamer]];

        [[MyClass sharedStreamer] stop];
        [sharedStreamer release];
        sharedStreamer = nil;
    }
}

Upvotes: 1

Related Questions