IOS 开发人员,正在开发一个基本的 MoviePlayer 应用程序。无法使用导航栏停止播放视频
Posted
技术标签:
【中文标题】IOS 开发人员,正在开发一个基本的 MoviePlayer 应用程序。无法使用导航栏停止播放视频【英文标题】:IOS Developer, working on a basic MPMoviePlayer app. Cant get the video to stop playing using navigationbar 【发布时间】:2011-08-19 15:05:10 【问题描述】:您好,我正在设计一个具有 UINavigationController 的基本 iPhone/iPad 应用程序。用户点击表格行,它会全屏打开一个视频。这很好用,可以播放视频。但是,当您单击导航栏以返回到 rootViewController 上的表格时,视频会继续在后台播放音频。有什么想法吗?
// 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
【问题讨论】:
请不要发布您的整个应用程序并期望 SO 成员为您调试它。缩小问题范围并发布代码 sn-ps。 公平评论,但是,我之前发布了 sn-ps,并且有 SO 成员要求我发布更多代码。我想有时候你赢不了 哈哈。是的。有时你不能。没有什么对你不利的——我通常会回答这类问题,但可以让自己浏览所有代码。 【参考方案1】:Apple 解释说,视频播放可以设置为“默认情况下使用应用程序的音频会话,但可以配置为使用系统提供的音频会话。”
如果您查看音频文档,您会发现您可以设置行为以在应用程序进入后台时停止或继续播放音乐(通常是获取 iPod 播放器行为)。那是你应该设置proper application variable.
【讨论】:
太棒了,谢谢凯达尔。我认为这可能与后台模式有关。感谢您的反馈。 我现在已经阅读了“应用程序音频会话”,这并不是我想要的。当我按下主页按钮时,应用程序关闭并且音频停止,这就是我想要的。问题是我从表格条目中选择一个视频,然后打开一个新窗口,视频开始播放。当我使用导航栏导航回根视图中的表格时,视频音频仍在播放。我不相信设置音频会话会帮助我在这一点上停止音频。也许我错了。我可以通过按视频上的完成来停止音频,但大多数用户不会这样做。有什么想法吗? 那你为什么不在viewDidUnload方法中重置视频呢?以上是关于IOS 开发人员,正在开发一个基本的 MoviePlayer 应用程序。无法使用导航栏停止播放视频的主要内容,如果未能解决你的问题,请参考以下文章
iOS“开发人员”角色可以创建/上传 Testflight 版本吗?