设备关闭时音频暂停不播放背景Xcode 4.2

时间:2012-01-29 21:56:05

标签: iphone ios audio media-player xcode4.2

请帮我解决这个问题。我是Xcode的新手,所以请详细向我解释。

在我的应用程序中包含音频和视频,但我只包括音频编码。

音频文件是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

这是我的文件

#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 个答案:

答案 0 :(得分:1)

首先,你真的不需要发布你的整个视图控制器......这会让你的问题变得很长,很难阅读!

您好像没有正确设置音频类别。您需要明确告诉iOS您希望在设备进入休眠状态时播放音频。这个问题包含了您需要做的事情:

Keep playing sound with MPMoviePlayerController and locked screen?

相关问题