2
請幫我解決這個問題。我是Xcode的新手,所以請詳細向我解釋一下。設備關閉時音頻暫停不播放背景Xcode 4.2
在我的應用程序中包含音頻和視頻,但我只包括音頻編碼。
音頻文件是30分鐘。長。設備關閉時音頻暫停,基本上不在背景中播放。
我已經在我的iPhone 4S上測試過了。它開始播放約1分鐘,然後電話按照正常方式進入睡眠狀態。突然音頻停止播放。當您打開手機時,它會在停止播放的位置再次開始播放。
即使手機進入睡眠,我也希望音頻連續播放。謝謝。
這是.h文件
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface Mynewpage4 : UIViewController
{
NSString *urlname;
MPMoviePlayerController *player;
}
@property(nonatomic,retain)NSString *urlname;
@end
這是M檔
#import "Mynewpage4.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation Mynewpage4
@synthesize urlname;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle]
pathForResource:urlname
ofType:@"mp3"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
player.view.frame = CGRectMake(0, 0, 320, 370);
[self.view addSubview:player.view];
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)viewDidDisappear:(BOOL)animated {
NSLog(@"hi");
[player stop];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
@end
這是我的.h文件
#import <UIKit/UIKit.h>
#import "Custom_cell.h"
@interface Newpage4 : UIViewController{
NSMutableArray *mydata;
NSMutableArray *myaudio;
// IBOutlet Custom_cell *owncell
}
@end
這是M檔
#import "Newpage4.h"
#import "Custom_cell.h"
#import "Mynewpage4.h"
@implementation Newpage4
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
mydata = [[NSMutableArray alloc]initWithObjects:@"great",nil];
myaudio = [[NSMutableArray alloc]initWithObjects:@"1",nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mydata count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Custom_cell";
Custom_cell *cell = (Custom_cell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {}
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"Custom_cell" owner:self options:nil];
//cell = [[[Custom_cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (Custom_cell *) currentObject;
//cell.backgroundView.backgroundColor = [UIColor clearColor];
break;
}
}
}
cell.lbl.text = [mydata objectAtIndex:indexPath.row];
return cell;
// [myString release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Mynewpage4 *page = [[Mynewpage4 alloc]init];
page.urlname = [myaudio objectAtIndex:indexPath.row];
[self.navigationController pushViewController:page animated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
感謝遺憾新來這個網站 – user1176951 2012-01-30 11:39:46