iOS開發(fā)播放本地以及網(wǎng)絡(luò)音樂

1、播放本地音樂
將音頻文件添加到您的Xcode項(xiàng)目中。確保將音頻文件的目標(biāo)成員身份設(shè)置為您的應(yīng)用程序。
導(dǎo)入AVFoundation框架,并在您的視圖控制器或其他適當(dāng)?shù)奈恢锰砑右韵麓a:

#import <AVFoundation/AVFoundation.h>

在視圖控制器的屬性或適當(dāng)?shù)念悢U(kuò)展中聲明AVAudioPlayer實(shí)例變量:
需要注意:
AVAudioPlayer不能作為局部變量!
AVAudioPlayer不能作為局部變量!
AVAudioPlayer不能作為局部變量!
否則會(huì)出現(xiàn)播放沒聲音的情況。

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

在需要播放音樂的地方,初始化并播放AVAudioPlayer實(shí)例。

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];

NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
if (self.audioPlayer) {
    [self.audioPlayer play];
} else {
    NSLog(@"Failed to initialize audio player: %@", [error localizedDescription]);
}

請(qǐng)確保將"music"替換為您的音頻文件的名稱,并將"mp3"替換為您的音頻文件的擴(kuò)展名。
若要停止音樂播放,您可以調(diào)用stop方法:

[self.audioPlayer stop];

根據(jù)您的需求,您可能還需要處理音頻的其他方面,例如處理播放進(jìn)度、音量控制等。有關(guān)更多高級(jí)功能,請(qǐng)參考Apple的AVFoundation框架文檔。

2、播放網(wǎng)絡(luò)URL音樂
導(dǎo)入AVFoundation框架,并在您的視圖控制器或其他適當(dāng)?shù)奈恢锰砑右韵麓a:

#import <AVFoundation/AVFoundation.h>

在視圖控制器的屬性或適當(dāng)?shù)念悢U(kuò)展中聲明AVAudioPlayer實(shí)例變量:

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

在需要播放音樂的地方,初始化并播放AVAudioPlayer實(shí)例。

NSURL *audioURL = [NSURL URLWithString:@"https://example.com/music.mp3"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:audioURL];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
[self.player play];

請(qǐng)將上述代碼中的URL替換為您要播放的音樂的實(shí)際URL。

若要停止音樂播放,您可以調(diào)用pause方法:

[self.player pause];

播放網(wǎng)絡(luò)URL音樂無限循環(huán)播放

#import <AVFoundation/AVFoundation.h>

@interface YourViewController ()
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@end

@implementation YourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 設(shè)置音頻URL
    NSURL *audioURL = [NSURL URLWithString:@"https://example.com/music.mp3"];
    self.playerItem = [AVPlayerItem playerItemWithURL:audioURL];
    
    // 監(jiān)聽播放完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:self.playerItem];
    
    // 初始化AVPlayer
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    
    // 開始播放音頻
    [self.player play];
}

// 播放完成通知回調(diào)
- (void)playerItemDidReachEnd:(NSNotification *)notification {
    // 重置播放時(shí)間為0
    [self.playerItem seekToTime:kCMTimeZero];
    
    // 重新開始播放
    [self.player play];
}

- (void)dealloc {
    // 移除通知觀察者
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:AVPlayerItemDidPlayToEndTimeNotification
                                                  object:self.playerItem];
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容