2017-01-12 73 views
1

我有一個我從JSON響應中獲得的URL,當我在瀏覽器中點擊該URL時,該文件被下載,現在我想知道如何播放這樣的URL,這是一個下載網址。我一直在找很多帖子但是他們沒有人能幫助我。從URL下載音頻文件並在iOS中播放App

類型的網址我得到:http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1

下面是我試過的代碼。

NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url]; 
    NSLog(@"url:%@",audio); 

    NSURL *url = [NSURL fileURLWithPath:audio]; 
    NSData *soundData = [NSData dataWithContentsOfURL:url]; 
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                   NSUserDomainMask, YES) objectAtIndex:0] 
          stringByAppendingPathComponent:@"sound.caf"]; 
    [soundData writeToFile:filePath atomically:YES]; 
    NSError *error; 
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
                  fileURLWithPath:filePath] error:&error]; 
    [_audioPlayer play]; 
    NSLog(@"error %@", error); 
+0

您應該嘗試從網址中流式傳輸音頻文件。編輯:@Marek說什麼。 – Ramon

+0

@Ramon,但它是一個下載網址。 –

+0

@Shikha Sharma你想從下載的文件路徑或從您的在線網址播放音頻? –

回答

2

試試這個:

//download your audio file 
    NSString *urlString = @"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3"; 
    NSURL *url = [NSURL fileURLWithPath:urlString]; 
    NSData *soundData = [NSData dataWithContentsOfURL:url]; 
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                   NSUserDomainMask, YES) objectAtIndex:0] 
          stringByAppendingPathComponent:@"sound.caf"]; 
    [soundData writeToFile:filePath atomically:YES]; 

    // get the file from directory 
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
    NSString *documentsDirectory = [pathArray objectAtIndex:0]; 
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"]; 

    NSURL *soundUrl; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath]) 
    { 
     soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO]; 
    } 

    // play audio file 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 

你應該異步下載音頻文件。

+0

你是否試過這段代碼?它可以聽到嗎? –

+0

但我不能聽到它,我應用相同的代碼,雖然音量按鈕正在工作,否則它不顯示。但它不可聞 –

+0

它的工作原理非常完美。我檢查了幾次。 –

1

嘗試使用AVPlayer或AVPlayerViewController,您可以使用直接URL來播放MP3文件

即使本地文件,你會首先將其下載到設備上,然後從本地URL

玩不忘記該對象AVPlayer必須作爲您的類的屬性不是本地對象,因爲自動釋放,所以聲明您的播放器變量在課堂上,並設置功能不只是在本地功能,並嘗試播放...

[更新]

更多細節如何做到這一點 - >How to play video with AVPlayerViewController (AVKit) in Swift

+0

這就是我想知道的。如何正確下載並播放。因爲我嘗試了很多方法,但都沒有工作。我會很感激,如果你想給我提供一些代碼。 –

+0

您提供的鏈接並未適用於我 –

+0

哪裏有問題?你至少應該描述究竟是哪一步,哪一步不起作用 –

0

這裏是錯誤

NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url]; 
NSURL *url = [NSURL fileURLWithPath:audio]; 

我認爲它是一個遠程URL,你不能把它當作一個文件URL。所以你必須使用

NSURL *url = [NSURL urlWithString:audio]; 
2

謝謝Dipankar達斯爲你提供幫助。 我在這裏發佈整個解決方案。 絕對適合我。

NSString *[email protected]"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3"; 
    NSURL *url = [NSURL URLWithString:audio]; 
    NSData *soundData = [NSData dataWithContentsOfURL:url]; 
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                   NSUserDomainMask, YES) objectAtIndex:0] 
          stringByAppendingPathComponent:@"sound.caf"]; 
    [soundData writeToFile:filePath atomically:YES]; 

    // get the file from directory 
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
    NSString *documentsDirectory = [pathArray objectAtIndex:0]; 
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"]; 

    NSURL *soundUrl; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath]) 
    { 
     soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO]; 
    } 

    // plau audio file 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; 
    [_audioPlayer prepareToPlay]; 
    [_audioPlayer play]; 
    [_audioPlayer setVolume:5.0]; 
+1

是的。它看起來不錯。 –

+0

非常感謝你的幫助。非常感謝 –

+0

我有一個MP3我想在avplayer中播放的網址,我嘗試過所有可能的方式,但它只是不播放歌曲。任何人都知道從給定的URL支付歌曲的任何方法? – Moxarth