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:
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
-
- @interface TalkingDemoViewController : UIViewController {
- AVAudioPlayer *player;
-
- }
-
- -(IBAction)sayTalking:(id)sender;
- @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:
- # Import "TalkingDemoViewController. h"
-
- @ Implementation TalkingDemoViewController
- // Implement viewDidLoad to do additiona l setup after loading the view, typically from a nib.
- -(Void) viewDidLoad {
- If (player ){
- [Player release];
- }
- NSString * soundPath = [[NSBundle mainBundle] pathForResource: @ "intro" ofType: @ "caf"];
- NSURL * soundUrl = [[NSURL alloc] initFileURLWithPath: soundPath];
- Player = [[AVAudioPlayer alloc] initWithContentsOfURL: soundUrl error: nil];
- [Player prepareToPlay];
- [SoundUrl release];
- [Super viewDidLoad];
- }
-
- -(IBAction) sayTalking :( id) sender
- {
- NSLog (@ "Playing Sound ");
- [Player play];
-
- }
- // Override to allow orientations other than the default portrait orientation.
- -(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation {
- Return YES;
- }
-
- -(Void) didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [Super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
-
- -(Void) viewDidUnload {
- // Release any retained subviews of the main view.
- // E.g. self. myOutlet = nil;
- }
-
- -(Void) dealloc {
- [Player release];
- [Super dealloc];
- }
- @ 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