在Xcode中停止声音

问题描述:

大家好, 请看下面的代码,我希望能够停止声音时,单击同一个按钮,另一个按钮被单击,甚至当返回到主屏幕。不知道在这里做什么,在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 

你的帮助深表感谢,贾斯汀。

保持一个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 :)感谢您的帮助 –

汇总,停止声音文件的一句话是:

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