Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is answered before: <a href="https://stackoverflow.com/questions/13890127/stopping-sound-in-xcode/18724050#18724050">Stopping sound in Xcode</a></p> <p>But I can repeat my answer.</p> <p>Summarizing, the sentence to STOP the sound file is:</p> <pre><code>AudioServicesDisposeSystemSoundID (soundFileObject); </code></pre> <p>Explanation (Important to understand):</p> <p>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.</p> <p>All this stuff it is necessary:</p> <p>1) Add to your project "AudioToolbox.framework" inside "Build Phases", inside "Link Binary With Libraries"</p> <p>2) In header file (in my project it calls "GenericTableView.h") add these sentences:</p> <pre><code>#include &lt;AudioToolbox/AudioToolbox.h&gt; CFURLRef soundFileURLRef; SystemSoundID soundFileObject; @property (readwrite) CFURLRef soundFileURLRef; @property (readonly) SystemSoundID soundFileObject; </code></pre> <p>3) In implement file (in my project it calls "GenericTableView.m") add these sentences:</p> <pre><code>@synthesize soundFileURLRef; @synthesize soundFileObject; </code></pre> <p>And inside your "Action" implementation or in my case, in:</p> <pre><code>- (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,&amp;soundFileObject); AudioServicesPlaySystemSound (soundFileObject); [soundURL release]; } </code></pre> <p>4) Finally, in the same implement file (in my project it calls "GenericTableView.m"):</p> <pre><code>- (void)dealloc { [super dealloc]; AudioServicesDisposeSystemSoundID (soundFileObject); //CFRelease (soundFileURLRef); } </code></pre> <p>I must to comment "CFRelease (soundFileURLRef)" because the App ends unexpectedly when user leaves the table view.</p> <p>All this code is proportioned by Apple. In this link you can find even a sample code to test directly in your iPhone simulator.</p> <p><a href="https://developer.apple.com/library/ios/samplecode/SysSound/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008018-Intro-DontLinkElementID_2" rel="nofollow noreferrer">https://developer.apple.com/library/ios/samplecode/SysSound/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008018-Intro-DontLinkElementID_2</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload