如何在objective-C中播放来自url的音频

时间:2018-01-19 05:40:36

标签: ios objective-c audio

这是我的代码

 NSURL *url = [NSURL URLWithString:recentActivity.url];
 NSData *data = [NSData dataWithContentsOfURL:url];
 AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
 [audioPlayer play];

并且它没有播放音频

3 个答案:

答案 0 :(得分:0)

试试这个。它会正常工作。

NSURL *url = [NSURL URLWithString:url];    
self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil];    
self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset];    
self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];    
[self.audioPlayer play];

适用于Swift 3.0

var player: AVAudioPlayer!
var slider: UISlider!

@IBAction func play(_ sender: Any) {
    var url = URL(fileURLWithPath: Bundle.main.path(forResource: "sound", ofType: ".mp3")!)
    var error: Error?
    do {
        player = try AVAudioPlayer(contentsOf: url)
    }
    catch let error {
    }
    if player == nil {
        print("Error: \(error)")
    }
    player.prepareToPlay()
    slider.maximumValue = Float(player.duration)
    slider.value = 0.0
    Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
    player.play()
}

func updateTime(_ timer: Timer) {
    slider.value = Float(player.currentTime)
}

@IBAction func slide(_ slider: UISlider) {
    player.currentTime = TimeInterval(slider.value)
}

答案 1 :(得分:0)

您可以尝试使用这一小段代码来播放来自网址的音频

创建新的帮助程序调用并将以下代码添加到PlayerHelper.h

    @interface PlayerHelper : NSObject
{
    AVPlayer *player;
}

+ (PlayerHelper*)shared;

- (BOOL)playAudio:(NSString*)urlPath;

- (void)stopPlayer;

- (void)pausePlayer;

- (void)seekTo:(float)time;

- (float)duration;
@end

将以下代码添加到PlayerHelper.m文件

 #import "PlayerHelper.h"

@implementation PlayerHelper
+ (PlayerHelper*)shared{
    static PlayerHelper *helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[PlayerHelper alloc] init];
    });
    return helper;
}

- (void)stopPlayer{
    if (player != nil) {
        player = nil;
    }
}

- (void)pausePlayer{
    [player pause];
}

- (BOOL)playAudio:(NSString*)urlPath{
    NSURL *URL = [NSURL URLWithString:urlPath];
    NSError *error = nil;
    if (player == nil){
        player = [[AVPlayer alloc] initWithURL:URL];
    }
    if (error == nil) {
        [player play];
        return TRUE;
    }
    NSLog(@"error %@",error);
    return FALSE;
}

- (void)seekTo:(float)time{
    [player seekToTime:CMTimeMake(time, 1)];
}

- (float)duration{
    return player.currentTime.value;
    return 0;
}
@end

用法,

NSString *url = @"https://isongs/media/rapon.mp3";
[[PlayerHelper shared] playAudio:url];

答案 2 :(得分:0)

您的代码存在问题(假设您使用的是ARC)是您正在分配AVAudioPlayer,然后发送play消息,但只要您的方法返回ARC,就会释放AVAudioPlayer {1}}因此你将无法听到任何声音。要解决此问题,请将AVAudioPlayer实例保留在ivar或某些媒体资源中。以下是ivar

的示例
@implementation ViewController {
  AVAudioPlayer *audioPlayer;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"];
  NSData *data = [NSData dataWithContentsOfURL:url];
  audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; // Now we are assigning it in an instance variable thus ARC will not deallocate it.
  [audioPlayer play];
}


@end