使用暂停按钮播放音乐文件(如Mp3)最简单的方法是什么? 非常非常简单的按钮播放和另一个按钮暂停音乐.. 你可以写代码!使用iPhone SDK播放MP3文件
回答
这些是请求的操作的代码, appSoundPlayer是在h文件中声明的AVAudioPlayer的属性。另外这个例子在资源文件夹中播放歌曲。
#pragma mark -
#pragma mark *play*
- (IBAction) playaction {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self
);
// Activates the audio session.
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
[stopbutton setEnabled:YES];
[playbutton setEnabled: NO];
playbutton.hidden=YES;
pausebutton.hidden =NO;
}//playbutton touch up inside
#pragma mark -
#pragma mark *pause*
-(IBAction)pauseaction {
[appSoundPlayer pause];
pausebutton.hidden = YES;
resumebutton.hidden = NO;
}//pausebutton touch up inside
#pragma mark -
#pragma mark *resume*
-(IBAction)resumeaction{
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume:1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
playbutton.hidden=YES;
resumebutton.hidden =YES;
pausebutton.hidden = NO;
}//resumebutton touch up inside
#pragma mark -
#pragma mark *stop*
-(IBAction)stopaction{
[appSoundPlayer stop];
[playbutton setEnabled:YES];
[stopbutton setEnabled:NO];
playbutton.hidden=NO;
resumebutton.hidden =YES;
pausebutton.hidden = YES;
}//stopbutton touch up inside
The Apple documentation here应该有你需要知道的一切。
well here是一个很好的教程。
的主题是
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
,当要暂停;
[audioPlayer pause];
希望这有助于。
短的声音,或者当MP3上不建议代码打得好,你可以随时使用:
SystemSoundID soundID;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]];
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
AudioServicesPlaySystemSound (soundID);
不要忘了补充:对
#import <AudioToolbox/AudioToolbox.h>
非常感谢! – rwarner 2012-03-31 22:05:00
谢谢,为我工作。使用ARC:AudioServicesCreateSystemSoundID((__ bridge CFURLRef)url,&soundID); – 2013-06-11 15:27:42
的oalTouch示例代码Apple iOS开发人员网站可以完成您想要的任务,并且可以运行。
它还显示了如何测试另一个应用程序(例如ipod)是否已经在播放文件,如果您想允许用户听自己的音乐而不是您的音乐,这很方便。
DIRAC API是免费的并且非常易于使用。 我们已经用它在我说话的机器人应用http://itunes.apple.com/us/app/daidai/id484833168,它一直令人惊异的是我怎么设法改变速度和语音的音调
恐怕答案中规定不再适用iOS 7及以上版本。您将需要使用下面的代码:
在头文件(.h)中
为了处理委托方法时,音频的播放结束audioPlayerDidFinishPlaying像:,从AVAudioPlayerDelegate继承。
@property (nonatomic, strong) AVAudioPlayer *player;
在实现文件(.M)
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: resourceName
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
_player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[_player play];
我喜欢简单的代码,这里是我的解决方案:(记住添加开关按钮让音乐播放玩得开心)
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController()
{
NSURL* bgURL;
AVAudioPlayer* bgPlayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bgURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"]];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
}
#pragma mark AVAudioPlayer
- (IBAction)toggleMusic:(UISwitch*)sender {
NSLog(@"togging music %s", sender.on ? "on" : "off");
if (bgPlayer) {
if (sender.on) {
[bgPlayer play];
}
else {
[bgPlayer stop];
}
}
}
- 1. 在iphone上播放mp3 sdk?
- 2. Iphone sdk支持通过网络播放mp3文件
- 3. Iphone SDK:pathFoResource:Nsstring问题 - >播放MP3文件
- 4. 播放MP3文件
- 5. 用libsox播放mp3文件
- 6. iPhone - 使用MPMusicPlayerController播放资源mp3
- 7. 播放较慢播放的MP3文件
- 8. 使用jquery播放mp3文件
- 9. 使用javascript onClick播放mp3文件
- 10. 使用JavaFX播放Swing的MP3文件
- 11. 使用C#播放MP3文件#
- 12. 使用pjsip播放mp3文件
- 13. 使用纯Java播放MP3文件
- 14. 不使用库,播放MP3/WAV文件?
- 15. 使用MPMoviePlayerController播放mp3和mp4文件
- 16. FileNotFoundException:使用Java/Eclipse播放mp3文件
- 17. 使用Python播放MP3文件
- 18. iphone dev的mp3文件不使用AV音频播放器
- 19. Python - 播放MP3文件
- 20. iPhone的MP3播放问题
- 21. 从iPhone播放MP3的URL
- 22. 播放mp3文件直接发给mp3
- 23. AVAudioPlayer不播放MP3文件
- 24. jPlayer不播放MP3文件
- 25. J2ME MP3播放器 - 循环播放MP3文件
- 26. iPhone不能播放使用type = @的mp3音效“mp3”
- 27. 在网页上播放mp3文件
- 28. 播放MP3文件,即使分发
- 29. 播放包含多个按钮的多个MP3文件 - iPhone SDK - Xcode的
- 30. 直播iPhone视频播放iPhone SDK
你有本教程的示例代码吗? – Momi 2009-12-30 09:32:05
这些错误是由于您没有在h文件中声明它们而导致的。 – Nithin 2009-12-31 03:28:20
此代码是否适用于NSURL ...从服务器播放音频缓冲? – 2012-07-20 18:07:28