2012-12-15 170 views
0

大家好, 請看下面的代碼,我希望能夠停止聲音時,單擊同一個按鈕,另一個按鈕被單擊,甚至當返回到主屏幕。不知道在這裏做什麼,在Xcode中停止聲音

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 

你的幫助深表感謝,賈斯汀。

回答

0

保持一個flagFoxViewController.h文件說BOOL soundPlay爲全局變量現在

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 soundbackground添加這個方法,如果沒有添加

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

    //stop background sound 
    if([newPlayer isPlaying]) 
    { 
    [newPlayer stop]; 
    } 
} 
+0

thankyou :)感謝您的幫助 –

0

彙總,停止聲音文件的一句話是:

AudioServicesDisposeSystemSoundID (soundFileObject); 

解釋(重要的是理解):

我有18表視圖不同的聲音文件。當用戶選擇一行時,必須發出相應的文件,並在選擇另一行時,必須停止前一個聲音並播放其他聲音。

所有這些東西是必要的:

1)添加到您的項目「AudioToolbox.framework」裏面的「構建階段」,裏面的「二進制鏈接與圖書館」

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)」,因爲應用程序在用戶離開表視圖時意外結束。

所有這些代碼是由蘋果比例。在這個鏈接中,你甚至可以找到一個示例代碼,直接在你的iPhone模擬器中測試。

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