2017-02-23 1 views
0

SoundCloud에서 오디오를 스트리밍 할 수있는 Ionic 2 앱이 있습니다.soundcloud에서 오디오를 다운로드하는 방법

public loadSelectedTrack(): void { 

     SC.get('/tracks/' + this.audio.id, { 
      filter: 'public' 
     }).then((track) => { 

      this.soundCloudCurrentTrack = track; 
      this.playSelectedTrack(); 
     }); 
    } 

    public playSelectedTrack(): void { 
     SC.stream('/tracks/' + this.soundCloudCurrentTrack.id).then((player) => { 


      this.togglePlayPause(); 

      player.on('buffering_start',() => { 
       console.log('buffering...'); 
      }); 

      player.on('buffering_end',() => { 
       console.log('party!'); 
       this.audioLoaded = true; 
      }); 

      player.on('finish',() => { 
       this.navCtrl.pop(); 
      }); 

      this.player = player; 

      player.on('time',() => { 
       this.updateTrackTime(); 
       this.updateTrackPercent(); 
      }); 

      this.saveUserData(); 

     }); 
    } 

는 지금, 나는 오디오를 다운로드하는 방법을 알아야하고, 어떻게 장치를 찾을 :

이것은 내가 스트리밍 할 방법이다. 나는 오디오를 다운로드 할 수있는 경우

은 내가 Storage 또는

NativeStorage 스토리지 ID를 수 있지만이 할 수있는 최선의 방법 인 경우 나도 몰라, 생각했다. 누군가 그것을 언제든지 했습니까?


편집 : 글쎄, 오디오를 다운로드하려고했지만 문제가 있습니다. 다운로드 버튼을 누르면 아무 것도 볼 수 없습니다. 따라서 앱 폴더로 이동하면 오디오 파일을 볼 수는 있지만 항상 크기가 600 바이트이므로 끊어졌으며 수동으로 재생할 수 없습니다.

나는 진행 상황을보고 싶었 기 때문에 조사를 한 후에 그 사건을 발견했다. 그래서 토스트 알림을 통해 임시 진행 상황을 보여 주려고했지만 "97".. "98"... 그리고 더 이상 진행되지 않습니다. 내 탐색기로 app 폴더로 이동하면 오디오 파일이 보이지만 깨졌습니다. 그리고 그것은 오류를 보여주지 않습니다!

public download(audio: any): void { 

     this.platform.ready().then(() => { 
      console.log("Clicked to download: " + audio.id); 

      let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${this.SC_CLIENT_ID}`; 

      let pathToSaveTo: string = ''; 

      if (this.platform.is('android')) { 
       pathToSaveTo = cordova.file.externalApplicationStorageDirectory + audio.id + '.wav'; 

       let fileTransfer = new Transfer(); 

       fileTransfer.onProgress(this.onProgress); 

       fileTransfer.download(url, pathToSaveTo) 
        .then((entry) => { 
         console.log('download complete: ' + entry.toURL()); 
        }, (error) => { 

         let prompt = this.alertCtrl.create({ 
          title: 'Error', 
          subTitle: error, 
          buttons: ['Accept'] 
         }); 

         prompt.present(); 
        }); 
      } 


     }); 

    } 

    onProgress = (progressEvent: ProgressEvent) : void => { 
     this.ngZone.run(() => { 
      if (progressEvent.lengthComputable) { 

       let progress: any = Math.round((progressEvent.loaded/progressEvent.total) * 100); 
       console.log(progress); 

       let toast = this.toastCtrl.create({ 
        message: progress, 
        duration: 100 
       }); 
       toast.present(); 

      } 
     }); 
    } 
+0

방금 ​​내 코드로 게시물을 편집했습니다. 죄송합니다. –

+0

보고 싶은데, 어떻게하면 오디오를 다운로드하려고하지만 문제가 있는지 추가했습니다. –

답변

2

쉽게 파일을 다운로드 할 수 있습니다 Transfer 모듈 이온 2 개 ​​이벤트를 사용하여 : 이것은 코드입니다. 다른 플랫폼의 다른 디렉토리에 파일을 다운로드하고 문서를 읽으십시오. here 파일을 다운로드 할 때 나중에 사용하기 위해 파일에 대한 데이터를 저장하는 것이 좋습니다. Storage을 사용하면 파일에 대해 필요한 모든 데이터 (예 : 이름)를 저장할 수 있습니다. 그런 다음. 플랫폼에 따라이 파일에 액세스 할 수 있습니다. 파일 액세스에 대한 자세한 내용은 cordova-plugin-file의 문서를 다시 참조하십시오.

+0

니스! 나는 그것을 연구 할 것입니다. –

+0

방금 ​​내 글을 편집했습니다. 너 볼 수있어, 제발? 고맙습니다! –

관련 문제