2012-03-26 142 views
5

我使用xcode 4.2並構建一個iphone應用程序,並有一個視圖,當打開時播放音頻流。
我試圖實現的是應用程序繼續播放,即使它進入背景。
我試過任何可能的解決方案,我發現在stackoverflow或其他地方(並有很多可用),但不能得到它的工作。當應用程序進入後臺時,音頻停止,當我再次打開應用程序時,音頻繼續。iphone - 播放背景中的音頻流

在我的Info.plist IA已設置2行:
Required Background Modes -> App plays audio &
Application does not run in background -> NO

我失去了什麼?還有什麼需要繼續在後臺播放音頻?
預先感謝您。

編輯: 我看到很多關於這個問題的答案,建議使用AVAudio Framework。有沒有可能MPMoviePlayerController無法在後臺播放流?我應該改用AVAudio嗎?

編輯2:
好吧,似乎這對我來說太複雜了。我給出了確切的代碼,希望這會有所幫助。

RadioStreamer.h

#import <UIKit/UIKit.h> 
#import "MediaPlayer/MediaPlayer.h" 
@interface RadioStreamer : UIViewController { 
    MPMoviePlayerController *player; 
    IBOutlet UIButton *playButton; 
    IBOutlet UIButton *pauseButton; 
} 
@property (nonatomic, retain) MPMoviePlayerController *player; 
@property (nonatomic, retain) IBOutlet UIButton *playButton; 
@property (nonatomic, retain) IBOutlet UIButton *pauseButton; 

- (IBAction)playButtonPressed:(id)button; 
- (IBAction)pauseButtonPressed:(id)button; 
- (void) playAudio; 
- (void) pauseAudio; 
@end 

RadioStreamer.m

#import "RadioStreamer.h" 
#import "tabbartestAppDelegate.h" 

@implementation RadioStreamer 
@synthesize player; 
@synthesize playButton; 
@synthesize pauseButton; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    return self; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [email protected]""; 
    // Do any additional setup after loading the view from its nib. 

    if (!self.player) { 
     player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://s6.onweb.gr:8006/listen.pls"]]; 
     player.movieSourceType = MPMovieSourceTypeStreaming; 
     player.view.frame = CGRectMake(55, 180, 200, 30); 
     player.backgroundView.backgroundColor = [UIColor clearColor]; 
     player.view.backgroundColor = [UIColor clearColor]; 
     [self.view addSubview:player.view]; 
     [player play]; 
    } 
} 

- (void) playAudio { 
    if (player.playbackState != MPMoviePlaybackStatePlaying){ 
     [player play]; 
    } 
} 

- (void) pauseAudio { 
    if (player.playbackState == MPMoviePlaybackStatePlaying) { 
     [player pause]; 
    } 
} 

- (IBAction)playButtonPressed:(id)button { 
    [self playAudio]; 
} 

- (IBAction)pauseButtonPressed:(id)button { 
    [self pauseAudio]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 


- (void)dealloc { 
    [self.player release]; 
    [self.playButton release]; 
    [self.pauseButton release]; 
    self.player = nil; 
    self.playButton = nil; 
    self.pauseButton = nil; 
} 

@end 
+0

[iOS 5 - 在啓動應用程序時在後臺播放音頻文件]的可能副本(http://stackoverflow.com/questions/9199095/ios-5-play-audio-file-in-the-background當該應用程序啓動時) – 2012-03-26 17:30:55

+0

謝謝Parth。在這個Q AVAudioPlayer使用,而不是MPMoviePlayerController。你認爲沒有區別? – CrisDeBlonde 2012-03-26 17:33:30

+0

問題是一樣的。唯一的方法是不同的。你可以改變你的方法,如果這對你有用。 – 2012-03-26 17:34:26

回答

6

檢查您的音頻會話建立因爲這可能需要一些額外的關注。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
             error:nil]; 
[[AVAudioSession sharedInstance] setActive:YES 
            error:nil]; 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
+0

嗨,直到,感謝與我的問題打擾。 AVAudioSession與MPMoviePlayerController有什麼關係?你能解釋一下嗎?總之,添加你的代碼並沒有什麼區別。 – CrisDeBlonde 2012-03-26 22:11:58

+0

@CrisDeBlonde你應該真的檢查AudioSession文檔。 – Till 2012-03-27 06:31:13

+1

最後這似乎是解決方案,但由於某種原因,在模擬器中它不會在後臺播放。在設備上測試它雖然工作就像一個魅力。 – CrisDeBlonde 2012-04-04 21:04:36