Hi i am designing a basic iPhone/iPad app that has a UINavigationController. The user clicks on the table row and it opens up a video in full screen. This works well and plays the video. however when you click on the navigation bar to go back to the table on the rootViewController the video keeps playing the audio in the background. Any ideas?
// RevoLix_HDAppDelegate.h
// RevoLix HD
//
// Created by Rob Hartley on 8/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@class UrologyViewController;
@interface RevoLix_HDAppDelegate : NSObject <UIApplicationDelegate> {
UrologyViewController *urologyViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
RevoLix_HDAppDelegate.m
// RevoLix HD
//
// Created by Rob Hartley on 8/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RevoLix_HDAppDelegate.h"
#import "PlayerViewController.h"
#import "UrologyViewController.h"
@implementation RevoLix_HDAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//PlayerViewController *vc = [[PlayerViewController alloc] init];
//[[self window]setRootViewController:vc];
urologyViewController = [[UrologyViewController alloc] init];
//create an instance of a UINavigationController, its stack contains only itemsViewController
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:urologyViewController];
[urologyViewController release];
[[self window]setRootViewController:navController];
[navController release];
[_window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
UrologyViewController.h
// RevoLix HD
//
// Created by Rob Hartley on 8/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PlayerViewController.h"
@ class PlayerViewController;
@interface UrologyViewController : UITableViewController {
NSMutableArray *allVideos;
}
@end
UrologyViewController.m
// RevoLix HD
//
// Created by Rob Hartley on 8/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "UrologyViewController.h"
#import "UrologyVideos.h"
#import "PlayerViewController.h"
@implementation UrologyViewController
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
[[self navigationItem] setTitle:@"Urology"];
}
allVideos = [[NSMutableArray alloc] init];
[allVideos addObject:[UrologyVideos fieldVideo]];
[allVideos addObject:[UrologyVideos strictureVideo]];
return self;
}
/*- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int numberOfRows = [allVideos count];
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create an instance of UITableViewCell, with default appearance
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
//set the text on the cell with the title of the video that is at the nth idex of possessions, where n = row this cell will appear in on the tableview
[[cell textLabel] setText:[[allVideos objectAtIndex:[indexPath row]] videoTitle]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerViewController *playerViewController = [[[PlayerViewController alloc] init] autorelease];
// Give detail view controller a pointer to the possession object in this row
[playerViewController setSelectVideo:[allVideos objectAtIndex:[indexPath row]]];
// Push it onto the top of the navigation controller's stack
[[self navigationController] pushViewController:playerViewController
animated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(void)dealloc
{
[allVideos release];
[super dealloc];
}
@end
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@class UrologyVideos;
@interface PlayerViewController : UIViewController {
MPMoviePlayerViewController *moviePlayer;
UrologyVideos *selectVideo;
}
@property (nonatomic,retain) UrologyVideos *selectVideo;
@end
PlayerViewController.m
// RevoLix HD
//
// Created by Rob Hartley on 8/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PlayerViewController.h"
#import "UrologyVideos.h"
@implementation PlayerViewController
@synthesize selectVideo;
- (id)init
{
return [super initWithNibName:@"PlayerViewController" bundle:nil];
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
return [self init];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self navigationItem] setTitle:[selectVideo videoTitle]];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:[selectVideo videoFileName] ofType:@"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[[self view] addSubview:[moviePlayer view]];
float halfHeight = [[self view]bounds].size.height;
float width = [[self view] bounds].size.width;
[[moviePlayer view] setFrame:CGRectMake(0, 0, width, halfHeight)];
}
- (void)dealloc
{
[selectVideo release];
[moviePlayer release];
[MPMoviePlayerViewController release];
[super dealloc];
}
- (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)viewDidUnload
{
[super viewDidUnload];
[selectVideo release];
selectVideo = nil;
[moviePlayer release];
moviePlayer = nil;
[MPMoviePlayerViewController release];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
Apple explains that video playback may be set to use “your application’s audio session by default, but can be configured to use a system-supplied audio session.”
If you check the audio documentation, you will find you can setup behavior to stop or keep playing music if application goes to background (typically, to obtain the iPod player behavior). That’s where you should set the proper application variable.