我正在尝试做类似于这个问题中要求的事情: Playing audio with controls in iOS
我正在使用带有 XCode 4.2 的情节提要。在我的初始视图页面上,我只有调用其他视图的按钮,并且没有播放任何声音。这些其他视图包含一个带有 BarButtonItem 的 UINavigationController 以允许您返回。其他视图页面将具有允许播放声音的按钮。每个声音都有自己的播放按钮,并且有恢复、暂停和停止按钮。
所有声音都可以正常播放,但如果我按返回按钮返回主页,声音就会继续播放。所需的行为是当通过后退按钮导航回主页时声音停止。
在故事板之前,我可以轻松地编写一个后退按钮来停止音频,但故事板看起来并不那么简单。看来我必须实现方法 prepareForSegue。好的,我可以设置 Segue 标识符,可以在 Attributes Inspector 中设置,参考之前的帖子,我将使用 showPlayer。
但我想我可以在我的声音视图中对 prepareForSegue 进行编码,如这个很酷的示例所示: http ://www.scott-sherwood.com/?p=219
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"showPlayer"]){
SoundPageController *theAudio = (SoundPageController *)[segue ViewController];
theAudio.delegate = self;
}
}
在主页面 ViewController.m 中,我尝试添加:
@property (strong, nonatomic) AVAudioPlayer *theAudio;
我尝试添加如下内容:
- (void)viewDidLoad {
[super viewDidLoad];
// this is the important part: if we already have something playing, stop it!
if (audioPlayer != nil)
{
[audioPlayer stop];
}
// everything else should happen as normal.
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
我无法使其正常工作,因此我将更改恢复为原始状态。我将把我的简化代码放在下面,如果您有正确的建议,请告诉我!
视图控制器.h:
@interface ViewController : UIViewController {
}
ViewController.m(几乎是默认的):
#import "ViewController.h"
#import "AppDelegate.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
SoundPage1.h:#import
#import <AVFoundation/AVAudioPlayer.h>
@interface occupy : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer* theAudio;
}
//-(IBAction)PressBack:(id)sender; now the back button is linked up through storyboard
-(IBAction)pushButton1;
-(IBAction)pushButton2;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end
SoundPage1.m
#import "SoundPage1.h"
#import "AppDelegate.h"
#import "ViewController.h"
@implementation SoundPage1
-(IBAction)pushButton {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}
-(IBAction)pushButton2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}
// Old code from XCode 3.x when creating a back button that could stop the audio was easy
//-(IBAction)PressBack:(id)sender{
// [theAudio stop];
// ViewController* myappname = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// [self.navigationController pushViewController:myappname animated:NO];
// }
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
为按钮添加播放、停止和暂停的代码。
我尝试对此进行格式化,如果难以阅读,请提前抱歉。
感谢您的任何建议!抢