I have this class that plays a song through MediaPlayer . This is working well. Now I want to implement playback controls in the notification area, but I'm having trouble calling the class I have in NotificationReturnSlot where I grab the corresponding action of each button.
I tried to put the inner class in another file but then I could not call the methods of the class AudioInterface
. How can I do this? o How could I make it work?
I clarify that this is a Interface
call from a WebView , which creates the Play controls and pauses in the notification bar.
public class AudioInterface {
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
MediaPlayer mPlayer;
Context mContext;
AudioInterface(Context c) {
mContext = c;
}
//Play an audio file from the webpage
@JavascriptInterface
public void playAudio(String aud) { //String aud - file name passed
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//String url = aud;
String url = "http://mysite.com/file.mp3";
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(mContext, "Wrong URI", Toast.LENGTH_LONG).show();
}
nBuilder = new NotificationCompat.Builder(mContext)
.setContentTitle("Test")
.setSmallIcon(R.drawable.splash_image)
.setOngoing(true);
remoteView = new RemoteViews(mContext.getPackageName(), R.layout.notificationview);
//set the button listeners
setListeners(remoteView);
nBuilder.setContent(remoteView);
nManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}
public void play(){
this.mPlayer.start();
}
public void pause(){
this.mPlayer.pause();
}
public void setListeners(RemoteViews view){
//listener 1
Intent playMP = new Intent(mContext,NotificationReturnSlot.class);
playMP.putExtra("DO", "play");
PendingIntent btn1 = PendingIntent.getActivity(mContext, 0, playMP, 0);
view.setOnClickPendingIntent(R.id.btn1, btn1);
//listener 2
Intent pauseMP = new Intent(mContext, NotificationReturnSlot.class);
pauseMP.putExtra("DO", "pause");
PendingIntent btn2 = PendingIntent.getActivity(mContext, 1, pauseMP, 0);
view.setOnClickPendingIntent(R.id.btn2, btn2);
}
public void notificationCancel() {
nManager.cancel(2);
}
public class NotificationReturnSlot extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String action = (String) getIntent().getExtras().get("DO");
if (action.equals("play")) {
Log.i("NotificationReturnSlot", "play");
Toast.makeText(this, "playing", Toast.LENGTH_SHORT).show();
play();
} else if (action.equals("pause")) {
//Log.i("NotificationReturnSlot", "stopNotification");
Log.i("NotificationReturnSlot", "pause");
Toast.makeText(this, "paused", Toast.LENGTH_SHORT).show();
pause();
}
//finish();
}
}
}