Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 如何在服务器上远程播放mp3文件而不阻塞ios中的UI_Xcode_Url_Ios8_Avaudioplayer - Fatal编程技术网

Xcode 如何在服务器上远程播放mp3文件而不阻塞ios中的UI

Xcode 如何在服务器上远程播放mp3文件而不阻塞ios中的UI,xcode,url,ios8,avaudioplayer,Xcode,Url,Ios8,Avaudioplayer,我正在使用iOS应用程序,让我从url播放mp3文件。一旦加载,它就可以正常工作。问题是加载大约需要30秒,并且会阻止UI转换到其他视图控制器。 代码: 我正在使用AVAudioPlayer类,以下是我的设置 (void)setupAudioPlayer:(NSString*)fName { // calls and pass url name to MyAudioPlayer class method [self.audioPlayer initWithD

我正在使用iOS应用程序,让我从url播放mp3文件。一旦加载,它就可以正常工作。问题是加载大约需要30秒,并且会阻止UI转换到其他视图控制器。 代码:

我正在使用AVAudioPlayer类,以下是我的设置

(void)setupAudioPlayer:(NSString*)fName
{

         // calls and pass url name to MyAudioPlayer class method
         [self.audioPlayer initWithData:fName];
         self.currentTimeSlider.maximumValue = 
         [self.audioPlayer getAudioDuration];

         //init the current time display and the labels.
         //if a current time was stored
        //for this player then take it and update the time display
        self.timeElapsed.text = @"0:00";

        self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:
                         [self.audioPlayer  getAudioDuration]]];

}
  • 然后调用上述方法并将url作为“”传递

(无效)viewDidLoad { [超级视图下载]; self.audioPlayer=[[MyAudioPlayer alloc]init]; //传递mp3所在的url,即http://www.test.com/test.mp3 [self-setupAudioPlayer:self.urlStr]; } 然后,按下下面的按钮时播放

- (IBAction)playAudioPressed:(id)playButton { [self.timer invalidate]; //play audio for the first time or if pause was pressed if (!self.isPaused) { //start a timer to update the time label display self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; [self.audioPlayer playAudio]; self.isPaused = TRUE; } else { [self.audioPlayer pauseAudio]; self.isPaused = FALSE; } } -(iAction)播放音频按下:(id)播放按钮 { [自动定时器失效]; //首次播放音频或按暂停键时播放音频 如果(!self.isPaused){ //启动计时器以更新时间标签显示 自动定时器= [NSTimer scheduledTimerWithTimeInterval:1.0 目标:自我 选择器:@selector(更新对象:) 用户信息:无 重复:是]; [self.audioPlayer播放音频]; self.isPaused=TRUE; }否则{ [self.audioPlayer pauseAudio]; self.isPaused=FALSE; } } 正如我前面提到的,问题是加载时间太长并且阻塞UI。请帮助或建议我如何减少加载时间和避免阻塞ui

任何帮助都将不胜感激


谢谢

以下是我的远程mp3文件流式播放代码:

@import AVFoundation;
...

@property (nonatomic, retain) AVPlayer *objAVPlayer;
@property (nonatomic, getter=isPlaying) BOOL playing;

// in viewDidLoad
_objAVPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.asdasd.com/file.mp3"]];

...

- (void)playPressed {
    if([self isPlaying]) {
        _playing = false;
        [_objAVPlayer pause];
    } else {
        _playing = true;
        [_objAVPlayer play];
    }
}
可能的时间延迟可能是由于连接缓慢造成的。我希望这能帮助你

@import AVFoundation;
...

@property (nonatomic, retain) AVPlayer *objAVPlayer;
@property (nonatomic, getter=isPlaying) BOOL playing;

// in viewDidLoad
_objAVPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.asdasd.com/file.mp3"]];

...

- (void)playPressed {
    if([self isPlaying]) {
        _playing = false;
        [_objAVPlayer pause];
    } else {
        _playing = true;
        [_objAVPlayer play];
    }
}