2012-01-29 106 views
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 

回答

1

首先,你真的不需要發佈你的整個視圖控制器......它讓你的問題很長,而且很難閱讀!

看起來好像您沒有正確設置您的音頻類別。您需要明確告訴iOS您希望在設備進入睡眠狀態時播放音頻。這裏這個問題涵蓋了你需要做什麼:

Keep playing sound with MPMoviePlayerController and locked screen?

+1

感謝遺憾新來這個網站 – user1176951 2012-01-30 11:39:46

相關問題