在Xcode中停止声音

时间:2012-12-15 06:53:49

标签: objective-c audio

大家好, 请看下面的代码,我希望能够在单击相同的按钮时停止声音,单击另一个按钮,甚至在返回主屏幕时也是如此。不知道该怎么做,

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

非常感谢你的帮助,贾斯汀。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

总结一下,停止声音文件的句子是:

AudioServicesDisposeSystemSoundID (soundFileObject);

解释(重要的是要理解):

我有一个包含18种不同声音文件的表视图。当用户选择一行必须发出相应的文件时,当选择另一行时,它必须停止前一个声音并播放其他声音。

所有这些都是必要的:

1)在“建立阶段”中添加到项目“AudioToolbox.framework”,在“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

相关问题