gauravk95 / audio-visualizer-android

🎵 [Android Library] A light-weight and easy-to-use Audio Visualizer for Android.
Apache License 2.0
844 stars 127 forks source link

Android version above 5.1 mVisualizer.setAudioSessionId(audioSessionId) not working #3

Closed chashikajw closed 5 years ago

chashikajw commented 5 years ago

mVisualizer.setAudioSessionId(audioSessionId) part is not working in android above 5.1 versions

gauravk95 commented 5 years ago

Tested it on multiple devices and seem to work fine. Have you built and tired the Sample App?

Note: The visualization depends on the system volume. So, if the media volume is 0, then the visualization will also be 0 i.e will seem that its not working.

If the sample is working, its possible that there's some problem in your implementation. Please provide some code and detailed LogCat, so that I can investigate the issue further.

chashikajw commented 5 years ago

Tested it multiple devices and seem to work fine. Have you built and tired the Sample App?

Note: The visualization depends on the system volume. So, if the media volume is 0, then the visualization will also be 0 i.e will seem that its not working.

If the sample is working, its possible that there's some problem in your implementation. Please provide some code and detailed LogCat, so that I can investigate the issue further.

I'm not using a raw audio file. I'm using an online radio stream. I think that's why that problem occurs.I saw your sample app. My solution works fine for below 5.1 versions.

here it occurs an error in ` mVisualizer.setAudioSessionId(mediaPlayer.getAudioSessionId()); code line `` public class MainActivity extends AppCompatActivity {

ImageButton imgButton;
com.gauravk.audiovisualizer.visualizer.BlastVisualizer mVisualizer;
MediaPlayer mediaPlayer;
//String stream = "http://109.236.85.141:7316/;";
String stream = "http://stream.radioreklama.bg/radio1rock128";

boolean prepared = false;
boolean started = false;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgButton =(ImageButton)findViewById(R.id.playbtn);
    imgButton.setImageResource(R.mipmap.playbtn);
    mVisualizer = findViewById(R.id.blast);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    int audioSessionId = mediaPlayer.getAudioSessionId();
    if (audioSessionId != -1 && audioSessionId !=AudioManager.ERROR){
        mVisualizer.setAudioSessionId(mediaPlayer.getAudioSessionId());
    }

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    new PlayerTask().execute(stream);

    imgButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if(started){
               started = false;
               mediaPlayer.pause();
               imgButton.setImageResource(R.mipmap.playbtn);
           }else{
               started = true;
               mediaPlayer.start();
               mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                   public void onPrepared(MediaPlayer mp) {
                       mp.start();
                   }
               });
               imgButton.setImageResource(R.mipmap.pausebtn);
           }
        }
    });
}

class PlayerTask extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings) {

        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.prepare();
            prepared = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prepared;
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);

        if (progressDialog.isShowing()) {
            progressDialog.cancel();
        }
        imgButton.setImageResource(R.mipmap.playbtn);
    }
}

@Override
protected void onPause() {
    super.onPause();
    if(started){
        mediaPlayer.pause();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(started){
        //mediaPlayer.start();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mediaPlayer.prepareAsync();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(prepared){
        mediaPlayer.release();
    }
    if (mVisualizer != null)
        mVisualizer.release();
}

}

`

gauravk95 commented 5 years ago

I think you are missing the Permissions Required.

The First line in Usage section is

Note: Use of the visualizer requires the permission android.permission.RECORD_AUDIO so add it to your manifest file. Also for Android 6.0 and above you will need to request permission in runtime.

I have added the streaming example to the sample also. You many look into it to how to get Runtime Permissions for using the Visualizer.

I hope this will solve the issue. Thanks!