iOS 音視頻開發之mp3播放功能開發教程,ios音視頻
1.建立播放控制項
2.代碼
#import "ViewController.h"#import @interface ViewController ()@property (nonatomic, strong) AVAudioPlayer *audioPlayer;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //在AppDelegate - application: didFinishLaunchingWithOptions:必須設定,否則真機測試沒有聲音// [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; // 1.載入本地的音樂檔案 NSURL *url = [[NSBundle mainBundle] URLForResource:@"西遊記.mp3" withExtension:nil]; // 2. 建立音樂播放對象 self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; // 3.準備播放 (音樂播放的記憶體空間的開闢等功能) 不寫這行代碼直接播放也會預設調用prepareToPlay [self.audioPlayer prepareToPlay];}// 播放- (IBAction)playMusic{ [self.audioPlayer play];}// 暫停- (IBAction)pause{ [self.audioPlayer pause];}// 停止- (IBAction)stop{ // 停止的效果和暫停是相同 [self.audioPlayer stop];}@end