|
| 1 | +package com.kk.taurus.avplayer.ui; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.media.AudioManager; |
| 5 | +import android.media.audiofx.Visualizer; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.text.TextUtils; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.EditText; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import androidx.annotation.Nullable; |
| 13 | +import androidx.appcompat.app.AppCompatActivity; |
| 14 | + |
| 15 | +import com.kk.taurus.avplayer.R; |
| 16 | +import com.kk.taurus.avplayer.view.VisualizerView; |
| 17 | +import com.kk.taurus.playerbase.AVPlayer; |
| 18 | +import com.kk.taurus.playerbase.entity.DataSource; |
| 19 | +import com.kk.taurus.playerbase.event.OnPlayerEventListener; |
| 20 | + |
| 21 | +public class MusicPlayActivity extends AppCompatActivity implements OnPlayerEventListener { |
| 22 | + |
| 23 | + private AVPlayer mPlayer; |
| 24 | + private EditText mEtUrl; |
| 25 | + |
| 26 | + private Visualizer mVisualizer; |
| 27 | + private VisualizerView mMusicWave; |
| 28 | + |
| 29 | + private byte[] waveType = new byte[]{ |
| 30 | + VisualizerView.WAVE_TYPE_BROKEN_LINE, |
| 31 | + VisualizerView.WAVE_TYPE_RECTANGLE, |
| 32 | + VisualizerView.WAVE_TYPE_CURVE}; |
| 33 | + |
| 34 | + private int typeIndex; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 38 | + super.onCreate(savedInstanceState); |
| 39 | + setContentView(R.layout.activity_test_play); |
| 40 | + mEtUrl = findViewById(R.id.music_url_et); |
| 41 | + mMusicWave = findViewById(R.id.visualizerView); |
| 42 | + |
| 43 | + setVolumeControlStream(AudioManager.STREAM_MUSIC); |
| 44 | + |
| 45 | + mPlayer = new AVPlayer(); |
| 46 | + mPlayer.setOnPlayerEventListener(this); |
| 47 | + |
| 48 | + initMusicWave(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onPlayerEvent(int eventCode, Bundle bundle) { |
| 53 | + switch (eventCode){ |
| 54 | + case OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED: |
| 55 | + releaseVisualizer(); |
| 56 | + updateVisualizer(); |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void initMusicWave() { |
| 62 | + mMusicWave.setWaveType(waveType[typeIndex]); |
| 63 | + mMusicWave.setColors(new int[]{Color.YELLOW, Color.BLUE}); |
| 64 | + mMusicWave.setOnClickListener(new View.OnClickListener() { |
| 65 | + @Override |
| 66 | + public void onClick(View v) { |
| 67 | + typeIndex++; |
| 68 | + typeIndex %= waveType.length; |
| 69 | + mMusicWave.setWaveType(waveType[typeIndex]); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + private void releaseVisualizer() { |
| 75 | + if(mVisualizer!=null){ |
| 76 | + mVisualizer.release(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void updateVisualizer() { |
| 81 | + mVisualizer = new Visualizer(mPlayer.getAudioSessionId()); |
| 82 | + mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); |
| 83 | + mVisualizer.setDataCaptureListener( |
| 84 | + new Visualizer.OnDataCaptureListener() { |
| 85 | + public void onWaveFormDataCapture(Visualizer visualizer, |
| 86 | + byte[] bytes, int samplingRate) { |
| 87 | + mMusicWave.updateVisualizer(bytes); |
| 88 | + } |
| 89 | + |
| 90 | + public void onFftDataCapture(Visualizer visualizer, |
| 91 | + byte[] bytes, int samplingRate) { |
| 92 | + |
| 93 | + } |
| 94 | + }, Visualizer.getMaxCaptureRate() / 2, true, false); |
| 95 | + mVisualizer.setEnabled(true); |
| 96 | + } |
| 97 | + |
| 98 | + public void startPlay(View view){ |
| 99 | + String url = mEtUrl.getText().toString(); |
| 100 | + if(TextUtils.isEmpty(url)){ |
| 101 | + Toast.makeText(this, "please input url", Toast.LENGTH_SHORT).show(); |
| 102 | + return; |
| 103 | + } |
| 104 | + DataSource dataSource = new DataSource(); |
| 105 | + dataSource.setData(url); |
| 106 | + mPlayer.reset(); |
| 107 | + mPlayer.setDataSource(dataSource); |
| 108 | + mPlayer.start(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void onDestroy() { |
| 113 | + super.onDestroy(); |
| 114 | + mPlayer.destroy(); |
| 115 | + releaseVisualizer(); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +} |
0 commit comments