Reputation: 21
Help me on this issue please. I am new to Xcode, so please explain to me in detail.
In my app there is audio and video included, but I have only included the audio coding.
Audio file is 30 min. long. Audio pauses when device switches off, NOT PLAYING IN BACKGROUND basically.
I have tested this on my iPhone 4S. It starts playing for about 1 min., then phone goes to sleep as normally it does. Suddenly audio stopped playing. When you switch ON the phone, it starts playing again where it was stopped.
I want the audio to play continuously even the phone goes to sleep. Thanks.
This is the h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface Mynewpage4 : UIViewController
{
NSString *urlname;
MPMoviePlayerController *player;
}
@property(nonatomic,retain)NSString *urlname;
@end
This is m file
#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
This is my h file
#import <UIKit/UIKit.h>
#import "Custom_cell.h"
@interface Newpage4 : UIViewController{
NSMutableArray *mydata;
NSMutableArray *myaudio;
// IBOutlet Custom_cell *owncell
}
@end
This is m file
#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
Upvotes: 2
Views: 1855
Reputation: 31304
Firstly, you really didn't need to post your entire view controller...it makes your question very long, and hard to read!
It looks as if you are not setting your audio categories correctly. You need to explicitly tell iOS you want your audio to play when the device goes to sleep. This question here covers what you need to do:
Keep playing sound with MPMoviePlayerController and locked screen?
Upvotes: 1