I am making a media player which plays music using an URL. I implemented a foreground service and also a notification. They are working fine. The problem is when I implement .stop function in media player , which is (rathu.stop)in my code, Apps gives an error displaying app stops working. I understand that I have used stop media player function in onDestroy method of service, but I dont know other place to implement that function. Then I have to set that method to a Onclicklistner also. Please help me.
Following are my codes.
MainActivity.java
package com.example.yomal.rathumakarafm;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
import java.security.Key;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
int Volume=0;
ImageView aboutImage;
private Button buttonStart;
private Button buttonStop;
private Button buttonPause;
public static final String CHANNEL_ID = "exampleServiceChannel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
initControls();
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
// buttonPause = (Button)findViewById(R.id.buttonPause);
aboutImage = (ImageView) findViewById(R.id.about);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
aboutImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == buttonStart) {
startService(new Intent(this, RathuMakara.class));
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
Toast play = Toast.makeText(getApplicationContext(), "??? 3, 2, 1 ... ?????? ????", Toast.LENGTH_LONG);
play.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
play.show();
} else if (v == buttonStop) {
stopService(new Intent(this, RathuMakara.class));
Toast play = Toast.makeText(getApplicationContext(), "Stopping Play", Toast.LENGTH_SHORT);
play.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
play.show();
buttonStop.setEnabled(false);
buttonStart.setEnabled(true);
} else if (v == aboutImage)
aboutImage = (ImageView) findViewById(R.id.about);
aboutImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(aboutIntent);
}
});
/*else if (v == buttonPause){
onPause();
}*/
}
private void initControls()
{
try {
volumeSeekbar = (SeekBar) findViewById(R.id.sb);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
Toast volume = Toast.makeText(getApplicationContext(), "Volume: " + Integer.toString(Volume), Toast.LENGTH_SHORT);
volume.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
volume.show();
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
Volume = progress , 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event){
AudioManager audio = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
volumeSeekbar.setProgress(currentVolume);
return false;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
volumeSeekbar.setProgress(currentVolume);
return false;
}
return super.onKeyDown(keyCode, event);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…