の再生を停止することはありません。私の最初のビューページには、他のビューを呼び出すボタンがあり、サウンドはそこで再生されません。これらの他のビューには、戻ることができるBarButtonItemを持つUINavigationControllerが含まれています。他のビューページには、サウンドを再生するためのボタンがあります。各サウンドには独自の再生ボタンがあり、レジューム、一時停止、停止ボタンがあります。、音が、私はこの質問で要求されたものと同様の何かをしようとしている新しいビューに
すべてのサウンドは正常に再生されますが、メインページの戻るボタンを押してもサウンドは再生され続けます。目的の動作は、戻るボタンを使用してメインページに戻るときにサウンドが停止することです。
ストーリーボードの前に、私は簡単にオーディオを停止するために戻るボタンをコーディングすることができましたが、ストーリーボードはそれほど単純ではありません。 prepareForSegueメソッドを実装する必要があるようです。さて、属性インスペクタでSegueの識別子を設定し、以前の投稿に戻って参照するように設定できます。私はshowPlayerを使用します。
しかし、私はこのクールな例で見られるように、私は、私のサウンドビューの1つでprepareForSegueをコーディングすることができ考えていた:メインページViewController.mで 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;
}
}
、私が追加しようとした:
@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];
私はこれを動作させることができませんでしたので、元の状態に戻しました。私は下に私の簡略化されたコードを入れて、あなたがそれを正しく行う方法がある場合は、私に教えてください!
ViewController.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;
}
ボタンの再生、停止、一時停止のコードを追加します。
これをフォーマットしようとしましたが、読みにくい場合は事前にごめんなさい。
ありがとうございました! Rob
うわー...これがいかに簡単かがわかりました。 viewWillDisappearを通してサウンドを再生するのを止めるソリューションは!!! - (void)viewWillDisappear:(BOOL)animated { [self.navigationController setNavigationBarHidden:NO animated:animated]; [super viewWillDisappear:animated]; [TheAudio stop]; } 素晴らしいです! 準備ができている人にいてくれてありがとう! Rob – user1362308