使用iPhone SDK播放MP3文件

时间:2009-12-29 10:19:11

标签: ios audio

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

8 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>

答案 2 :(得分:7)

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

希望这会有所帮助。

答案 3 :(得分: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];

答案 4 :(得分: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];
        }
    }   
}

答案 5 :(得分:0)

The Apple documentation here应该包含您需要了解的所有内容。

答案 6 :(得分:0)

Apple iOS开发人员网站上的oalTouch示例代码可以满足您的需求,并且可以运行。

它还显示了如何测试另一个应用程序(例如ipod)是否正在播放文件,如果您希望允许您的用户收听他们自己的音乐而不是您的音乐,这将非常方便。

答案 7 :(得分:0)

DIRAC API免费且易于使用。 我们在我的谈话机器人应用程序http://itunes.apple.com/us/app/daidai/id484833168中使用它,对我来说如何设法改变声音的速度和音高一直很令人惊讶

http://www.dspdimension.com/download/