Play SoundCloud Track

前端 未结 2 1409
清歌不尽
清歌不尽 2021-01-01 08:02

can i play a track from SoundCloud in my android app? I\'m trying this code but it doesn\'t works:

String res = \"https://api.soundcloud.com/tracks/84973999         


        
2条回答
  •  余生分开走
    2021-01-01 08:34

    I was having the same problem trying to play a Soundcloud stream from Android.

    What fixed it for me was adding the following permissions to my AndroidManifest.xml

    
    
    
    
    

    For me it was a facepalm moment. Here is my mediaplayer implementation.

    For my mediaplayer I have a class that implements

    MediaPlayer.OnPreparedListener
    

    And in that class I use the following code to setup the MediaPlayer.

     mMediaPlayer = new MediaPlayer();
     mMediaPlayer.setOnPreparedListener(this);
     mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try{
            mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/7399237/stream?client_id=XXX");
        }catch (IllegalArgumentException e){
            e.printStackTrace();
    
        } catch (IOException e) {
            e.printStackTrace();
    
        }
        mMediaPlayer.prepareAsync();
    

    And in my onPrepared callback I simply start playing the stream.

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mMediaPlayer.start();
    }
    

提交回复
热议问题