[英]handleWatchKitExtensionRequest play background audio on iphone
[英]iphone - play audio stream in background
我正在使用xcode 4.2並構建一個iphone應用程序,並且認為打開后會播放音頻流。
我試圖實現的是讓應用程序即使在進入后台后仍繼續播放。
我嘗試了在stackoverflow或其他地方(還有很多可用的地方)找到的任何可能的解決方案,但是無法正常工作。 當應用程序進入背景時,音頻停止,當我再次打開應用程序時,音頻繼續。
在我的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];
self.navigationItem.title=@"";
// 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
檢查您的音頻會話設置,因為這可能需要額外注意。
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
[[AVAudioSession sharedInstance] setActive:YES
error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
在這個地方如何偶然發現了編程教程的詳細信息。 http://mobisoftinfotech.com/integrate-music-player-in-iphone/
這應該會在應用程序中播放音樂,並且如果您將應用程序置於后台也可以繼續播放。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.