2009-12-29 149 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]; 
     } 
    } 
}