0

起動時にイントロ クリップを再生するアプリがあります。次のコードは- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsappDelegate.m 内にあり、見事に機能します。

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];

イントロ サウンドが終了する前にユーザーがビューを変更しても、引き続き再生されます。私はこのコード- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsを役立つとは考えていませんでしたが、そうではありませんでした。

-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Stop Sound
[self.startupPlayer stop];
}

ロード リクエストの直後に if ステートメントを配置すればうまくいくかもしれないと思ったのですが、うまくいきませんでした。例を参照してください:

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];

if ([viewDidDisappear == YES]) {
[self.startupPlayer stop];
}

クリップの再生が完了する前にユーザーがビューを変更した場合にイントロ サウンドを停止する提案があれば、すばらしいでしょう。ああ、アプリの他の部分で「viewWillDisappear」を使用して成功しましたが、この場合は「viewDidDisappear」を選択しました。後者も機能しなかったからです。だから私は困惑しています。前もって感謝します。

編集:だから、viewWillDisappear を MainViewController に移動し、デリゲートを呼び出しましたが、まだ運がありません。繰り返しますが、助けていただければ幸いです。

4

1 に答える 1

0

この問題は、* startupPlayerの@property宣言を取得し、以下のようにではなくAppDelegate.hファイルに配置することで解決しました。

AppDelegate.mで

@interface AppDelegate () 

@property (nonatomic, strong) AVAudioPlayer *startupPlayer;
@end

次に、以下に示すように、それを.mファイルで@合成し、didFinishLaunchingWithOptionsを同じに保ちました。

@implementation AppDelegate 
@synthesize startupPlayer = _startupPlayer;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

//------ PLAY SOUND CLIP WHILE LOADING APP -----

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play]; }

次に、私のMainViewController.mで

#import "AppDelegate.h"

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//stop intro sound
AppDelegate *introClip = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[[introClip startupPlayer]stop];}

これで、イントロ音楽が1サイクルにわたって再生されている場合でも、ユーザーは別のビューに切り替えてその音楽を停止できます。

于 2012-10-18T17:43:03.140 に答える