IPhone tutorial playing audio files

Source: Internet
Author: User

IPhoneTutorial playbackSound fileThis document describes how to use Objective-C to develop and play mp3 files.IPhoneProgram, of course, the purpose of this article is not to let you doIPhoneBecause you are not needed at all, the iPod program is ready. This article aims to enable you to use music in your own games.

As follows:

1. Open xcode and create a View-based Application iPhone program named TalkingDemo.

2. If you want to use the playback sound function, you must introduce the AVFoundation library, right-click the Frameworkds directory in the project, and select Add-> Existing Frameworkd from the menu, as shown in:

This operation will open the browser library dialog box. We select the library named AVFoundation. framework and add it in.

3. modify the content of the TalkingDemoViewController. h file as follows:

 
 
  1. #import <UIKit/UIKit.h>   
  2. #import <AVFoundation/AVFoundation.h>   
  3.  
  4. @interface TalkingDemoViewController : UIViewController {   
  5.     AVAudioPlayer *player;   
  6.  
  7. }   
  8.  
  9. -(IBAction)sayTalking:(id)sender;   
  10. @end 

4. double-click TalkingDemoViewController. in the xib file, open InterfaceBuilder, drag a Round Rect Button component, and bind the component to btn. If you do not bind the InterfaceBuilder component to the Objective-C code, see the iPhone Button ), then, change the button label to "play music"

5. modify the content of the TalkingDemoViewController. m file as follows:

 
 
  1. # Import "TalkingDemoViewController. h"
  2.  
  3. @ Implementation TalkingDemoViewController
  4. // Implement viewDidLoad to do additiona l setup after loading the view, typically from a nib.
  5. -(Void) viewDidLoad {
  6. If (player ){
  7. [Player release];
  8. }
  9. NSString * soundPath = [[NSBundle mainBundle] pathForResource: @ "intro" ofType: @ "caf"];
  10. NSURL * soundUrl = [[NSURL alloc] initFileURLWithPath: soundPath];
  11. Player = [[AVAudioPlayer alloc] initWithContentsOfURL: soundUrl error: nil];
  12. [Player prepareToPlay];
  13. [SoundUrl release];
  14. [Super viewDidLoad];
  15. }
  16.  
  17. -(IBAction) sayTalking :( id) sender
  18. {
  19. NSLog (@ "Playing Sound ");
  20. [Player play];
  21.  
  22. }
  23. // Override to allow orientations other than the default portrait orientation.
  24. -(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation {
  25. Return YES;
  26. }
  27.  
  28. -(Void) didReceiveMemoryWarning {
  29. // Releases the view if it doesn't have a superview.
  30. [Super didReceiveMemoryWarning];
  31. // Release any cached data, images, etc that aren't in use.
  32. }
  33.  
  34. -(Void) viewDidUnload {
  35. // Release any retained subviews of the main view.
  36. // E.g. self. myOutlet = nil;
  37. }
  38.  
  39. -(Void) dealloc {
  40. [Player release];
  41. [Super dealloc];
  42. }
  43. @ End

6. This code will play back a file named "intro. caf". Please add this file to the Resources folder.

7. Press Command + R to run the program. Click "Play Music" to hear the playing sound.

Source code: http://easymorse-android.googlecode.com/svn/trunk/TalkingDemo/

Summary:IPhoneTutorial playbackSound fileI hope this article will help you.

This article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.