I'd like to include an MP3 with my app and play it on demand. Unfortunately I am not sure how to get started. I keep reading about .caf files, but I am not sure what those are.
我想在我的应用程序中加入一个MP3并按需播放。不幸的是,我不知道如何开始。我一直在阅读.caf文件,但我不确定它们是什么。
I am looking for a step by step, if possible.
如果可能的话,我正在寻找一步一步。
4 个解决方案
#2
15
Here is what I am doing to play mp3 in xCode 4.2:
这是我在xCode 4.2中播放mp3的过程:
1) Import AVFoundation.framework
framework into your project
1)将AVFoundation.framework框架导入您的项目
2) Adding: #import "AVFoundation/AVAudioPlayer.h"
into your project ViewController.h
file:
2)在项目ViewController.h文件中添加:#import“AVFoundation / AVAudioPlayer.h”:
#import <UIKit/UIKit.h>
#import "AVFoundation/AVAudioPlayer.h"
@interface ViewController : UIViewController
@end
3) Drag and drop your mp3 file yoursoundfile.mp3
into your root project explorer
3)将mp3文件yoursoundfile.mp3拖放到根项目资源管理器中
4) Modify -(void)viewDidLoad
in ViewController.m
:
4)修改 - (void)viewDidLoadin ViewController.m:
- (void)viewDidLoad{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"yoursoundfile"
ofType:@"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
}
As soon as you start your program, it will play the sound. You can customize to fit your app by changing volume, or when to play, stop...
一旦你启动你的程序,它将播放声音。您可以通过更改音量或何时播放,停止来自定义以适合您的应用...
#3
2
Use afconvert in Terminal to convert your MP3 to a .caf. man afconvert will tell you more.
在终端中使用afconvert将MP3转换为.caf。男人afconvert会告诉你更多。
Then
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *player = [super initWithContentsOfURL: fileURL error: nil];
[fileURL release];
[player play];
#4
1
Maybe look at: http://iphoneincubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files and http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html?
也许请看:http://iphoneincubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files和http://cocoawithlove.com/2008/09/streaming-and-playing-现场的MP3 stream.html?
#1
#2
15
Here is what I am doing to play mp3 in xCode 4.2:
这是我在xCode 4.2中播放mp3的过程:
1) Import AVFoundation.framework
framework into your project
1)将AVFoundation.framework框架导入您的项目
2) Adding: #import "AVFoundation/AVAudioPlayer.h"
into your project ViewController.h
file:
2)在项目ViewController.h文件中添加:#import“AVFoundation / AVAudioPlayer.h”:
#import <UIKit/UIKit.h>
#import "AVFoundation/AVAudioPlayer.h"
@interface ViewController : UIViewController
@end
3) Drag and drop your mp3 file yoursoundfile.mp3
into your root project explorer
3)将mp3文件yoursoundfile.mp3拖放到根项目资源管理器中
4) Modify -(void)viewDidLoad
in ViewController.m
:
4)修改 - (void)viewDidLoadin ViewController.m:
- (void)viewDidLoad{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"yoursoundfile"
ofType:@"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
}
As soon as you start your program, it will play the sound. You can customize to fit your app by changing volume, or when to play, stop...
一旦你启动你的程序,它将播放声音。您可以通过更改音量或何时播放,停止来自定义以适合您的应用...
#3
2
Use afconvert in Terminal to convert your MP3 to a .caf. man afconvert will tell you more.
在终端中使用afconvert将MP3转换为.caf。男人afconvert会告诉你更多。
Then
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *player = [super initWithContentsOfURL: fileURL error: nil];
[fileURL release];
[player play];
#4
1
Maybe look at: http://iphoneincubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files and http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html?
也许请看:http://iphoneincubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files和http://cocoawithlove.com/2008/09/streaming-and-playing-现场的MP3 stream.html?