2012-03-15 4 views
0

私のアプリケーションはオンラインストリームを再生します。ビューコントローラ間を移動しながらサウンドを再生する

しかし、ユーザーが次のビューコントローラーに移動すると、音楽が停止します。私が試した

- (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]; 
     } 
    } 
} 

:ここでは音果たしているコードである

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

は、しかし、それは動作しません。誰にもこの経験はありますか?

答えて

1

ASオブジェクトへの参照を保持する必要があります。

私の場合、私はシングルトンでASをラップしました。

@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; 
    } 
} 
関連する問題