停止播放其他声音之前 [英] Stop a sound Before PLaying another

查看:80
本文介绍了停止播放其他声音之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在此之前已经有人问过这个问题,但是我不理解答复。我正在制作一个音板,当您单击其中一个按钮播放声音时,我希望已经播放的声音停止播放。我知道我需要添加 AudioServicesDisposeSystemSoundID(soundID); ,但是我应该在哪里添加呢?这是我的代码:

I know that this has been asked before on here, but i didn't understand the responses. I am making a soundboard, and when you click one of the buttons to play a sound, i would like the one that was already playing to stop. I know i need to add AudioServicesDisposeSystemSoundID(soundID);, but where do i add it? Here is my code:

- (IBAction)beyond:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef =  CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"beyond" ,CFSTR ("wav") , NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
   }

- (IBAction)Years:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef =  CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"years" ,CFSTR ("wav") , NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
   }


推荐答案

之前被回答:
在Xcode中停止声音

但是我可以重复我的答案。

But I can repeat my answer.

总而言之,停止声音文件的句子是:

Summarizing, the sentence to STOP the sound file is:

AudioServicesDisposeSystemSoundID (soundFileObject);

说明(重要理解):

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

I have a table view with 18 different sound files. When user select a row must sound the corresponding file and when select another row, it must STOP the previous sound and play other.

所有这些都是必要的:

1)将 AudioToolbox.framework添加到项目中的 Build Phases中,在 Link Binary With Libraries中

1) Add to your project "AudioToolbox.framework" inside "Build Phases", inside "Link Binary With Libraries"

2)在头文件中(在我的项目中,它称为 GenericTableView.h)添加以下语句:

2) In header file (in my project it calls "GenericTableView.h") add these sentences:

#include <AudioToolbox/AudioToolbox.h>

CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;

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

3)在工具文件中(在我的项目中,它称为 GenericTableView.m)添加以下语句:

3) In implement file (in my project it calls "GenericTableView.m") add these sentences:

@synthesize soundFileURLRef;
@synthesize soundFileObject;

在您的动作实现中或在我的情况下,位于:

And inside your "Action" implementation or in my case, in:

- (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):

4) Finally, in the same implement file (in my project it calls "GenericTableView.m"):

- (void)dealloc {
[super dealloc];

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

我必须评论 CFRelease(soundFileURLRef),因为该应用程序在以下情况下意外结束用户离开表格视图。

I must to comment "CFRelease (soundFileURLRef)" because the App ends unexpectedly when user leaves the table view.

所有这些代码均由Apple按比例分配。在此链接中,您甚至可以找到示例代码以直接在iPhone模拟器中进行测试。

All this code is proportioned by Apple. In this link you can find even a sample code to test directly in your iPhone simulator.

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

这篇关于停止播放其他声音之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆