2012-03-26 19 views
5

私はxcode 4.2を使用していて、iphoneアプリを構築しており、開いたときにオーディオストリームを再生するビューを持っています。
私が達成しようとするのは、アプリがバックグラウンドに入っても再生を続けることです。
私はstackoverflowや他の場所で見つかった可能な解決方法を試しましたが(利用可能なものも多数ありますが)、それを動作させることはできません。アプリケーションがバックグラウンドに入ると、オーディオが停止します。アプリを再び開くと、オーディオが継続します。私は何をしないのです
Required Background Modes -> App plays audio &
Application does not run in background -> NOiphone - バックグラウンドでオーディオストリームを再生する

:私のInfo.plistのIAでは

は2行を設定していますか?バックグラウンドでオーディオを再生し続けるために必要なものは何ですか?
ありがとうございます。

編集: AVAudio Frameworkの使用を提案して、この問題に関する多くの回答があります。 MPMoviePlayerControllerがバックグラウンドでストリームを再生できない可能性はありますか? AVAudioに変更する必要がありますか?

EDIT 2:
[OK]を達成するためには複雑すぎるようです。私は正確なコードを与えている、これが助けてくれることを願っています。

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 - 起動時) –

+0

ありがとうParth。このQでは、AVAudioPlayerが使用されますが、MPMoviePlayerControllerでは使用されません。あなたは違いがないと思いますか? – CrisDeBlonde

+0

質問は同じです。唯一のアプローチは異なります。それがあなたのために働く場合、あなたのアプローチを変更することができます。 –

答えて

6

それはいくつかの余分な注意が必要になる場合がありますようにオーディオセッションの設定を確認してください。

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

こんにちは、私の質問に迷惑を掛けてくれてありがとう。 AVAudioSessionとMPMoviePlayerControllerの関係は何ですか?どうか説明できますか?とにかく、あなたのコードを追加しても何の違いもありません。 – CrisDeBlonde

+0

@CrisDeBlonde AudioSessionのドキュメントを確認してください。 – Till

+1

最後に、これは解決策のようですが、何らかの理由でSimulatorでバックグラウンドで再生されません。デバイス上でそれをテストすることは、魅力のように動作します。 – CrisDeBlonde

0

このチュートリアルでは、プログラミングチュートリアルをどのように遭遇しましたか? http://mobisoftinfotech.com/integrate-music-player-in-iphone/

これはアプリ内で音楽を再生するはずであり、アプリをバックグラウンドで使用する場合は引き続き再生する必要があります。

関連する問題