【问题标题】:Audio pausing when device switches off NOT PLAYING IN BACKGROUND Xcode 4.2设备关闭时音频暂停不在后台播放 Xcode 4.2
【发布时间】:2012-01-29 21:56:04
【问题描述】:

请在这个问题上帮助我。我是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

这是我的 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

【问题讨论】:

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


    【解决方案1】:

    首先,您确实不需要发布整个视图控制器...这会使您的问题很长,而且难以阅读!

    您似乎没有正确设置音频类别。您需要明确告诉 iOS 您希望在设备进入睡眠状态时播放您的音频。这里的这个问题涵盖了您需要做的事情:

    Keep playing sound with MPMoviePlayerController and locked screen?

    【讨论】:

    • 感谢您对本网站的新用户感到抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多