0

こんにちは、以下のコードを見てください。同じボタンがクリックされたとき、別のボタンがクリックされたとき、およびホーム画面に戻ったときにもサウンドを停止できるようにしたいです。ここで何をすべきかわからない、

FoxViewController.m

#import "FoxViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
AVAudioPlayer *newPlayer_Fox;

@interface FoxViewController ()

@end

@implementation FoxViewController

-(IBAction) Fox;
{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"new"ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
[newPlayer play];



}


- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

 - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


@end

FoxViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioPlayer *newPlayer;

@interface FoxViewController : UIViewController

-(IBAction)Fox;

@end

あなたの助けに感謝します、ジャスティン。

4

2 に答える 2

0

ファイル内のグローバル変数として1 つのflag発言を維持するBOOL soundPlayFoxViewController.h

現在FoxViewController.m's viewDidLoadメソッド中

 soundPlay = TRUE;

アクションボタンは次のようになります。

-(IBAction) Fox;
{
 if(soundPlay){
   soundPlay = FALSE; //made false for next time another condition will be used.
   NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"new"ofType: @"mp3"];
   NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
 newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
  [newPlayer play];
 }
 else
 {
    if([newPlayer isPlaying])
    {
       [newPlayer stop];
    }  
 }
}

追加されていない場合は、このメソッドを追加しstopping soundますbackground

 -(void)viewWillDisappear:(BOOL)animated
 {
   [super viewWillDisappear:animated];

   //stop background sound
   if([newPlayer isPlaying])
   {
    [newPlayer stop];
   }
 }
于 2012-12-15T10:59:40.303 に答える
0

要約すると、サウンド ファイルを停止する文は次のとおりです。

AudioServicesDisposeSystemSoundID (soundFileObject);

説明 (理解することが重要):

18 の異なるサウンド ファイルを含むテーブル ビューがあります。ユーザーが行を選択すると、対応するファイルを鳴らす必要があり、別の行を選択すると、前の音を停止して他の音を再生する必要があります。

このすべてが必要です:

1) プロジェクトに「AudioToolbox.framework」を「Build Phases」内の「Link Binary With Libraries」内に追加します。

2)ヘッダーファイル(私のプロジェクトでは「GenericTableView.h」と呼ばれます)に次の文を追加します:

#include <AudioToolbox/AudioToolbox.h>

CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;

@property (readwrite) CFURLRef soundFileURLRef;
@property (readonly) SystemSoundID soundFileObject;

3)実装ファイル(私のプロジェクトでは「GenericTableView.m」と呼ばれます)に次の文を追加します。

@synthesize soundFileURLRef;
@synthesize soundFileObject;

そして、「アクション」実装内、または私の場合は、次のとおりです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
//
// That is the question: a way to STOP sound file
//
// -----------------------------------------------------------------------
// Very important to STOP the sound file
AudioServicesDisposeSystemSoundID (soundFileObject);

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------


// In my project "rowText" is a field that save the name of the 
// corresponding sound (depending on row selected)
// It can be a string, @"Alarm Clock Bell" in example

// Create the URL for the source audio file.
// URLForResource:withExtension: method is new in iOS 4.0.
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:rowText withExtension:@"caf"];

// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [soundURL retain];

// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);

AudioServicesPlaySystemSound (soundFileObject);

[soundURL release];
}

4) 最後に、同じ実装ファイルで (私のプロジェクトでは "GenericTableView.m" を呼び出します):

- (void)dealloc {
[super dealloc];

AudioServicesDisposeSystemSoundID (soundFileObject);
//CFRelease (soundFileURLRef);
}

ユーザーがテーブルビューを離れるとアプリが予期せず終了するため、「CFRelease (soundFileURLRef)」にコメントする必要があります。

このコードはすべて Apple によって調整されます。このリンクでは、iPhone シミュレーターで直接テストするためのサンプル コードも見つけることができます。

https://developer.apple.com/library/ios/samplecode/SysSound/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008018-Intro-DontLinkElementID_2

于 2013-09-10T16:15:31.263 に答える