如何通过我的应用程序在soundcloud上传音频,我想将该ID也发送到我的服务器

时间:2013-10-25 11:21:38

标签: java android audio upload soundcloud

我从soundcloud获取音频,我也可以在我的应用中传输该音频,现在问题是如何将音频上传到soundcloud,然后我想将ID发送到我的服务器端

我的要求是使用上传按钮我想从外部存储目录中获取文件,然后我想发送上传音频。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          mDbHelper = new GinfyDbAdapter(this);
        setContentView(R.layout.upload_audiogallery);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        upload = (ImageButton) findViewById(R.id.btnupload);
        btnstop = (Button) findViewById(R.id.btnstop);
        //Bundle extras = getIntent().getExtras();  

        token = (Token) this.getIntent().getSerializableExtra("token");
        wrapper = new ApiWrapper("3b70c135a3024d709e97af6b0b686ff3",
                "51ec6f9c19487160b5942ccd4f642053",
                null,
                token);

       //for speech to text and recording purpose           
       setButtonHandlers();
       enableButtons(false);

       mp = new MediaPlayer();  


  upload .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //String rootpath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //loadAllAudios(rootpath);

                     File file = new File("/mnt/sdcard/Download/57FYsUnoWxj2.128.mp3");
                     String path = file.getAbsolutePath();
             new MyAsyncTask().execute(path);
             UploadToSoundCloudTask uploadTask = new UploadToSoundCloudTask(this, wrapper);
            uploadTask.execute(new AudioClip(path));        


        }

  });
  }

 private class UploadToSoundCloudTask extends AsyncTask<AudioClip, Integer, Integer> {
    private Uploadaudiogallery recordActivity;
    private ApiWrapper wrapper;
    private String clipName;

    public UploadToSoundCloudTask(OnClickListener onClickListener, ApiWrapper wrapper) {
        this.recordActivity =  (Uploadaudiogallery) onClickListener;
        this.wrapper = wrapper;
    }

    @SuppressLint("NewApi")
    protected Integer doInBackground(AudioClip... clips) {
        try {
            Log.d("DDDDD", "uploading in background...");

            File audioFile = new File(clips[0].path);
            audioFile.setReadable(true, false);
            HttpResponse resp = wrapper.post(Request.to(Endpoints.TRACKS)

                    .add(Params.Track.TAG_LIST, "demo upload")
                    .withFile(Params.Track.ASSET_DATA, audioFile));
            Log.d("DDDDD", "background thread done...");
            return Integer.valueOf(resp.getStatusLine().getStatusCode());

        } catch (IOException exp) {
            Log.d("DDDDD",
                    "Error uploading audioclip: IOException: "
                            + exp.toString());
            return Integer.valueOf(500);
        }
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(Integer result) {
        Log.d("DDDDD", "UI thread resume: got result...");
        if (result.intValue() == HttpStatus.SC_CREATED) {
            Toast.makeText(
                    this.recordActivity,
                    "upload successful: "
                            + ": " + clipName, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(
                    this.recordActivity,
                    "Invalid status received: " + result.toString()
                            + ": " + clipName, Toast.LENGTH_SHORT).show();
        }


    }
}

我也使用了Java api-wrapper jar文件。点击上传其节目应用程序已停止

Logcat错误

10-25 10:35:27.203: E/AndroidRuntime(1921): FATAL EXCEPTION: main
10-25 10:35:27.203: E/AndroidRuntime(1921): java.lang.ClassCastException: com.ibetter.Ginfy.Uploadaudiogallery$4 cannot be cast to com.ibetter.Ginfy.Uploadaudiogallery
10-25 10:35:27.203: E/AndroidRuntime(1921):     at com.ibetter.Ginfy.Uploadaudiogallery$UploadToSoundCloudTask.<init>(Uploadaudiogallery.java:85)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at com.ibetter.Ginfy.Uploadaudiogallery$4.onClick(Uploadaudiogallery.java:192)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.view.View.performClick(View.java:4204)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.view.View$PerformClick.run(View.java:17355)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.os.Handler.handleCallback(Handler.java:725)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.os.Looper.loop(Looper.java:137)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at java.lang.reflect.Method.invoke(Method.java:511)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 10:35:27.203: E/AndroidRuntime(1921):     at dalvik.system.NativeStart.main(Native Method)

如何将路径和上传音频发送到soundcloud,然后将id发送到我的服务器端..

1 个答案:

答案 0 :(得分:3)

替换此

UploadToSoundCloudTask uploadTask = new UploadToSoundCloudTask(this, wrapper); uploadTask.execute(new AudioClip(path)); 

通过

UploadToSoundCloudTask uploadTask = new UploadToSoundCloudTask(ActivtiyName.this, wrapper); uploadTask.execute(new AudioClip(path)); 

在您的情况下,this不会引用活动上下文

上传查看此处的示例

https://github.com/soundcloud/java-api-wrapper/blob/master/src/examples/java/com/soundcloud/api/examples/UploadFile.java

Downalod java-wrapper-api.jar并将其添加到libs文件夹

从sdcard获取音频文件的路径

上传

http://developers.soundcloud.com/docs#uploading

从上述链接中引用

要上传声音,请向/ tracks端点发送POST请求

创建一个包装器实例:

ApiWrapper wrapper = new ApiWrapper("client_id", "client_secret", null, null);

获得一个代币:

wrapper.login("username", "password");

向/ tracks端点发出POST请求。在按钮上单击调用AsyncTask

class TheTask extends AsyncTask<Void,Void,Void>
{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
           try {
                wrapper = new ApiWrapper("client_id",
                         "client_secret",
                         null,
                         null);
                token = wrapper.login("username", "password");
                upload();
            } catch (IOException e) {
                e.printStackTrace();
            }

        return null;
    }

}

上传方法。

public void upload()
{
try {
    Log.d("DDDDD", "uploading in background...");
    File audioFile = new File("/mnt/sdcard/Music/A1.mp3");
            // replace the hardcoded path with the path of your audio file
    audioFile.setReadable(true, false);
    HttpResponse resp = wrapper.post(Request.to(Endpoints.TRACKS)
            .add(Params.Track.TITLE, "A1.mp3")
            .add(Params.Track.TAG_LIST, "demo upload")
            .withFile(Params.Track.ASSET_DATA, audioFile));
    Log.i("......",""+Integer.valueOf(resp.getStatusLine().getStatusCode()));
    Log.d("DDDDD", "background thread done...");

} catch (IOException exp) {
    Log.d("DDDDD",
            "Error uploading audioclip: IOException: "
                    + exp.toString());

}
}