使用iPhone SDK播放MP3文件

Mom*_*omi 12 audio ios

使用暂停按钮播放音乐文件(如Mp3)的最简单方法是什么?非常简单的按钮播放和另一个按钮暂停音乐

Nit*_*hin 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
Run Code Online (Sandbox Code Playgroud)


Jul*_*lon 25

对于短音或当MP3在建议的代码上不能很好地播放时,您可以随时使用:

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

AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID); 
AudioServicesPlaySystemSound (soundID);
Run Code Online (Sandbox Code Playgroud)

别忘了添加:

#import <AudioToolbox/AudioToolbox.h>
Run Code Online (Sandbox Code Playgroud)


Mad*_*dav 7

还有这里可一个很好的教程.

主题是

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];
Run Code Online (Sandbox Code Playgroud)

当你想暂停时;

[audioPlayer pause];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Has*_*tar 5

我担心在iOS 7及更高版本中说明的答案不再适用.您需要使用以下代码:

在头文件(.h)中

为了处理委托方法,比如音频播放完成audioPlayerDidFinishPlaying:,继承自AVAudioPlayerDelegate.

@property (nonatomic, strong) AVAudioPlayer *player;
Run Code Online (Sandbox Code Playgroud)

在实现文件(.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];
Run Code Online (Sandbox Code Playgroud)