试图创建从soundcloud播放歌曲的媒体播放器..

时间:2016-01-07 10:26:40

标签: javascript android media-player

我想用前进,后退,播放,暂停按钮创建媒体播放器。从SoundCloud.com播放歌曲。这是我的代码..

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);
    b4 = (Button) findViewById(R.id.button4);

    seekbar = (SeekBar) findViewById(R.id.seekBar);

    mp = new MediaPlayer();
    seekbar.setVisibility(SeekBar.VISIBLE);
    seekbar.setProgress(0);
    seekbar.setMax(mp.getDuration());

    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer player) {
            mp.start();

       }
   });

    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
            mp.start();

            finalTime = mp.getDuration();
            startTime = mp.getCurrentPosition();

            if (oneTimeOnly == 0) {
                seekbar.setMax((int) finalTime);
                oneTimeOnly = 1;
            }
            seekbar.setProgress((int) startTime);
            myHandler.postDelayed(UpdateSongTime, 100);
            b2.setEnabled(true);
            b3.setEnabled(false);
        }
    });

    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mp.isPlaying()) {
                Toast.makeText(getApplicationContext(), "Pausing sound", Toast.LENGTH_SHORT).show();
                mp.pause();
                mp.seekTo(0);
            }

            b2.setEnabled(false);
            b3.setEnabled(true);
        }
    });
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int temp = (int) startTime;

                    if ((temp + forwardTime) <= finalTime) {
                        startTime = startTime + forwardTime;
                        mp.seekTo((int) startTime);
                        Toast.makeText(getApplicationContext(), "You have Jumped forward 5 seconds", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    b4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int temp = (int) startTime;

            if ((temp - backwardTime) > 0) {
                startTime = startTime - backwardTime;
                mp.seekTo((int) startTime);
                Toast.makeText(getApplicationContext(), "You have Jumped backward 5 seconds", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_SHORT).show();
            }
        }
    });
    mListItems = new ArrayList<Track>();
    final ListView listView = (ListView)findViewById(R.id.listview);
    mAdapter = new SCTrackAdapter(this, mListItems);
    listView.setAdapter(mAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Track track = mListItems.get(position);
            if (mp.isPlaying()) {
                mp.stop();
                mp.reset();
            }

            try {
                mp.setDataSource(track.getStreamURL() + "?client_id=" + Config.CLIENT_ID);
                mp.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

    SCService scService = SoundCloud.getService();
    scService.getRecentTracks(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), new Callback<List<Track>>() {
        @Override
        public void success(List<Track> tracks, Response response) {
            loadTracks(tracks);
        }

        @Override
        public void failure(RetrofitError error) {
            Log.d(TAG, "Error: " + error);
        }
    });
}
private void loadTracks(List<Track> tracks) {
    mListItems.clear();
    mListItems.addAll(tracks);
    mAdapter.notifyDataSetChanged();
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
    startTime = mp.getCurrentPosition();
    seekbar.setProgress((int)startTime);
    myHandler.postDelayed(this, 100);
}
};

@Override
public void onDestroy() {
    super.onDestroy();
    mp.release();if (mp != null) {
        if (mp.isPlaying()) {
            mp.stop();
                      }
       mp.release();
        mp = null;
    }
}

我还添加了AndroidMenifest。

 <permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我分别创建了Config,SCService,SCTrackAdapter,SounndCloude,Track ..我遵循这个站点的代码“http://www.sitepoint.com/develop-music-streaming-android-app”。 但我得到了错误。

    01-07 14:59:46.055 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
    01-07 14:59:46.055 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: error (-38, 0)
   01-07 14:59:46.276 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Error (-38,0)
   01-07 14:59:46.895 25541-25547/com.vetecstech.www.mediap E/MediaPlayer: error (1, -1004)
  01-07 14:59:47.215 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Error (1,-1004)
  01-07 14:59:50.855 25541-25543/com.vetecstech.www.mediap D/dalvikvm: GC_CONCURRENT freed 1429K, 58% free 3082K/7175K, external 5368K/6641K, paused 4ms+4ms
  01-07 14:59:53.035 25541-25546/com.vetecstech.www.mediap I/dalvikvm: Jit: resizing JitTable from 512 to 1024

当我点击列表项时,我收到了错误。错误在下面。

    01-07 15:19:41.765 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Error (1,-2147483648)
 01-07 15:19:45.335 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: start called in state 0
  01-07 15:19:45.335 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: error (-38, 0)
  01-07 15:19:45.335 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
   01-07 15:19:45.335 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: error (-38, 0)
请帮助我。无法理解问题在哪里。

1 个答案:

答案 0 :(得分:0)

mp = new MediaPlayer();
...   
seekbar.setMax(mp.getDuration());

您正试图获取未准备好的MediaPlayer的持续时间,这会导致问题

01-07 14:59:46.055 25541-25541/com.vetecstech.www.mediap E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer

您可能希望在OnPreparedListener

中移动seekbar.setMax(mp.getDuration());
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer player) {
        seekbar.setMax(mp.getDuration()); 
        mp.start();

    }
});