首页 > 代码库 > xcode 中用AvAudioPlayer播放mp3文件

xcode 中用AvAudioPlayer播放mp3文件

       [playButton setEnabled:YES];

        NSError *playerError;

        AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[[NSURL alloc] initFileURLWithPath:mp3FilePath] autorelease] error:&playerError];

        self.player = audioPlayer;

        player.volume = 1.0f;

        if (player == nil)

        {

            NSLog(@"ERror creating player: %@", [playerError description]);

        }

        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategorySoloAmbient error: nil];

        player.delegate = self;

 

 

- (void)playPause

{

    //If the track is playing, pause and achange playButton text to "Play"

    if([player isPlaying])

    {

        [player pause];

        [playButton setTitle:@"Play" forState:UIControlStateNormal];

    }

    //If the track is not player, play the track and change the play button to "Pause"

    else

    {

        [player play];

        [playButton setTitle:@"Pause" forState:UIControlStateNormal];

    }

}