Пример #1
0
  @Override
  public boolean onQueryTextSubmit(String query) {
    Log.d(TAG, query);
    Soundcloud.getService()
        .searchSongs(
            query,
            new Callback<List<Track>>() {
              @Override
              public void success(List<Track> tracks, Response response) {
                getTracks(tracks);
              }

              @Override
              public void failure(RetrofitError error) {}
            });
    return true;
  }
Пример #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnPreparedListener(
        new MediaPlayer.OnPreparedListener() {
          @Override
          public void onPrepared(MediaPlayer mp) {
            togglePlayPauseState();
          }
        });
    mMediaPlayer.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
            mPlayerState.setImageResource(R.drawable.ic_play);
          }
        });

    SoundCloudService service = Soundcloud.getService();
    service.getRecentSongs(
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
        new Callback<List<Track>>() {
          @Override
          public void success(List<Track> tracks, Response response) {
            getTracks(tracks);
          }

          @Override
          public void failure(RetrofitError error) {}
        });

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.songs_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    mTracks = new ArrayList<Track>();
    mAdapter = new TracksAdapter(mTracks, this);
    mAdapter.setmOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Track selectedTrack = mTracks.get(position);
            mCurrentTrackTitle.setText(selectedTrack.getmTitle());
            Picasso.with(MainActivity.this)
                .load(selectedTrack.getArtworkURL())
                .into(mCurrentTrackArtwork);

            if (mMediaPlayer.isPlaying()) {
              mMediaPlayer.stop();
              mMediaPlayer.reset();
            }

            try {
              mMediaPlayer.setDataSource(
                  selectedTrack.getmTrackURL() + "?client_id=" + SoundCloudService.CLIENT_ID);
              mMediaPlayer.prepareAsync();
            } catch (IOException e) {
              e.printStackTrace();
            }

            mSoundcloudLink.setText(selectedTrack.getmLink());
          }
        });
    recyclerView.setAdapter(mAdapter);

    android.support.v7.widget.Toolbar toolbar =
        (android.support.v7.widget.Toolbar) findViewById(R.id.player_toolbar);
    mCurrentTrackTitle = (TextView) findViewById(R.id.current_track_title);
    mCurrentTrackArtwork = (ImageView) findViewById(R.id.current_track_artwork);
    mPlayerState = (ImageView) toolbar.findViewById(R.id.player_state);
    mPlayerState.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            togglePlayPauseState();
          }
        });

    Button emailButton = (Button) findViewById(R.id.email_button);
    emailButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Context context = getApplicationContext();
            if (!mCurrentTrackTitle.getText().equals("")) {
              Intent sendEmail = new Intent(Intent.ACTION_SEND);
              sendEmail.setData(Uri.parse("mailto:"));
              sendEmail.setType("text/plain");
              sendEmail.putExtra(Intent.EXTRA_SUBJECT, "I want to share a track!");
              sendEmail.putExtra(
                  Intent.EXTRA_TEXT,
                  "I really like this song I found on Soundcloud via my Soundroid App: "
                      + mCurrentTrackTitle.getText()
                      + "\nClick on the link below to listen!\n"
                      + mSoundcloudLink.getText());
              try {
                releaseMediaPlayer();
                startActivity(Intent.createChooser(sendEmail, "Send mail..."));
                finish();
                Toast emailSent = Toast.makeText(context, "Email sent", Toast.LENGTH_SHORT);
              } catch (Exception e) {
                Toast noEmailClient =
                    Toast.makeText(context, "No email client installed", Toast.LENGTH_LONG);
                noEmailClient.show();
              }
            } else {
              Toast noTrackPlayed = Toast.makeText(context, "Play a song!", Toast.LENGTH_SHORT);
              noTrackPlayed.show();
            }
          }
        });
  }