2009-12-29 121 views
13

使用暂停按钮播放音乐文件(如Mp3)最简单的方法是什么? 非常非常简单的按钮播放和另一个按钮暂停音乐.. 你可以写代码!使用iPhone SDK播放MP3文件

回答

33

这些是请求的操作的代码, 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 
+0

你有本教程的示例代码吗? – Momi 2009-12-30 09:32:05

+1

这些错误是由于您没有在h文件中声明它们而导致的。 – Nithin 2009-12-31 03:28:20

+0

此代码是否适用于NSURL ...从服务器播放音频缓冲? – 2012-07-20 18:07:28

7

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]; 

希望这有助于。

+1

感谢很多我爱http://stackoverflow.com当然你:D – Momi 2009-12-29 11:19:48

+0

好的,我再次遇到一些问题。当我点击播放按钮几次音乐播放和... ...!如何播放音乐1次?和关于暂停按钮,谁工作? 对不起iam amateur: – Momi 2009-12-29 11:51:40

25

短的声音,或者当MP3上不建议代码打得好,你可以随时使用:

SystemSoundID soundID; 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]]; 

AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID); 
AudioServicesPlaySystemSound (soundID); 

不要忘了补充:对

#import <AudioToolbox/AudioToolbox.h> 
+0

非常感谢! – rwarner 2012-03-31 22:05:00

+1

谢谢,为我工作。使用ARC:AudioServicesCreateSystemSoundID((__ bridge CFURLRef)url,&soundID); – 2013-06-11 15:27:42

0

oalTouch示例代码Apple iOS开发人员网站可以完成您想要的任务,并且可以运行。

它还显示了如何测试另一个应用程序(例如ipod)是否已经在播放文件,如果您想允许用户听自己的音乐而不是您的音乐,这很方便。

5

恐怕答案中规定不再适用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]; 
1

我喜欢简单的代码,这里是我的解决方案:(记住添加开关按钮让音乐播放玩得开心)

#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]; 
     } 
    } 
}