1

私はこの質問で要求されたものと同様のことをしようとしています: iOSのコントロールでオーディオを再生する

XCode4.2でストーリーボードを使用しています。最初のビューページには、他のビューを呼び出すボタンがあり、サウンドは再生されません。これらの他のビューには、戻ることができるBarButtonItemを持つUINavigationControllerが含まれています。他のビューページには、サウンドを再生できるボタンがあります。各サウンドには独自の再生ボタンがあり、再開、一時停止、停止のボタンがあります。

すべてのサウンドは正常に再生されますが、メインページの[戻る]ボタンを押すと、サウンドは再生され続けます。望ましい動作は、戻るボタンを使用してメインページに戻るときにサウンドが停止することです。

ストーリーボードの前は、オーディオを停止するための戻るボタンを簡単にコーディングできましたが、ストーリーボードはそれほど単純ではありません。メソッドprepareForSegueを実装する必要があるようです。さて、属性インスペクターでSegue識別子を設定できます。以前の投稿を参照して、showPlayerを使用します。

しかし、次のクールな例に見られるように、サウンドビューの1つで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];

これを機能させることができなかったので、変更を元に戻しました。簡略化したコードを以下に示します。正しく行う方法について提案があれば、お知らせください。

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;
}

ボタンの再生、停止、一時停止のコードを追加します。

これをフォーマットしてみましたが、読みづらい場合は事前に申し訳ありません。

提案をありがとう!ロブ

4

2 に答える 2

3

viewWillDisappear で AVAudioPlayer を停止する必要があります。

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    if(theAudio && theAudio.isPlaying) {
        [theAudio stop]; 
    }
}
于 2012-04-28T04:46:00.810 に答える
2

ViewDidUnload は、ビューを変更したときではなく、デバイスがメモリ容量の終わりに達したときにのみプログラムによって呼び出されます。今週、同じ問題に苦しんでいるときに、これを自分で見つけたばかりです。

問題が解決することを願っています。

于 2012-09-26T20:55:24.683 に答える