请帮我解决这个问题。我是 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
最佳答案
首先,您真的不需要发布整个 View Controller ...这会使您的问题变得很长且难以阅读!
您似乎没有正确设置音频类别。您需要明确告诉 iOS 您希望在设备进入休眠状态时播放音频。这里的这个问题涵盖了您需要做什么:
Keep playing sound with MPMoviePlayerController and locked screen?
关于iphone - 设备关闭时音频暂停不在后台播放 Xcode 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057145/