오디오 재생하는것을 하고있는데요
오디오를 재생하고 있을 때 앱을 나가거나 종료해도 notification을 통해서 지속적으로 보여주는 것을 했습니다.
그런데 음악을 재생후 바에 나오는 notification을 클릭해서 화면으로 들어가면 음악 정지를 누르면 정지가 되지 않더라구요..
왜 그런지 모르겠네요..
조언 부탁드립니다.
MainActivity.class
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
audioPlay.startAudio();
Intent startIntent = new Intent(MainActivity.this, ForgroundService.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(startIntent);
break;
case R.id.button2:
audioPlay.stopAudio();
Intent stopIntent = new Intent(MainActivity.this, ForgroundService.class);
stopIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
startService(stopIntent);
break;
case R.id.button3:
Intent startIntent1 = new Intent(MainActivity.this, ForgroundService.class);
startIntent1.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(startIntent1);
break;
default:
break;
}
}
foreground.class
public class ForgroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent previousIntent = new Intent(this, ForgroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);
Intent playIntent = new Intent(this, ForgroundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0, playIntent, 0);
Intent nextIntent = new Intent(this, ForgroundService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getService(this, 0, nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Truiton Music Player")
.setTicker("Truiton Music Player")
.setContentText("My Music")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous,
"Previous", ppreviousIntent)
.addAction(android.R.drawable.ic_media_play, "Play",
pplayIntent)
.addAction(android.R.drawable.ic_media_next, "Next",
pnextIntent).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i(LOG_TAG, "Clicked Previous");
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
//MainActivity.exoPlayer.stop();
Log.i(LOG_TAG, "Clicked Next");
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
// Used only in case of bound services.
Log.i(LOG_TAG, "on Bind");
return null;
}
}
audioPlay
public class audioPlay {
ExoPlayer exoPlayer;
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
private static final int PLAY_AUDIO_NUMBER = 0;
private Context context;
public audioPlay(Context context){
exoPlayer = ExoPlayer.Factory.newInstance(1);
this.context = context;
}
public void startAudio(){
exoPlayer.setPlayWhenReady(true);
}
public void stopAudio(){
Log.i("audio", "stop Audioi");
if(exoPlayer == null){
Log.i("audio", "Null");
} else {
Log.i("audio", "Not Null");
exoPlayer.setPlayWhenReady(false);
}
}
public void setAudio(){
Uri radioUri = Uri.parse("http://0.s3.envato.com/h264-video-previews/80fad324-9db4-11e3-bf3d-0050569255a8/490527.mp4");
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultUriDataSource(context, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(radioUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
exoPlayer.prepare(audioRenderer);
}
}