Android - after animation finished video is not playing - android

I'm new to android. I'm trying to play video after animation , but before animation video is playing, after animation video is not playing, i have used animationlistener, it's also not working for me.
public class MainActivity extends Activity{
ImageView image;
VideoView video;
ImageButton play;
RelativeLayout videolay;
Animation LR;
ZoomView zoomview;
String uriPath = null;
static boolean playpause = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
video = (VideoView) findViewById(R.id.video);
play = (ImageButton) findViewById(R.id.play);
videolay = (RelativeLayout) findViewById(R.id.videolay);
zoomview = (ZoomView) findViewById(R.id.zoomlay);
LR = AnimationUtils.loadAnimation(this, R.anim.move);
videolay.startAnimation(LR);
LR.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
uriPath = "android.resource://" + MainActivity.this.getPackageName() + "/" + "raw/video";
Log.v("uripath",""+uriPath);
MediaController ctrl = new MediaController(MainActivity.this);
ctrl.setVisibility(View.GONE);
video.setMediaController(ctrl);
video.setVideoURI(Uri.parse(uriPath));
}
});
video.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
video.pause();
playpause = false;
play.setVisibility(View.VISIBLE);
return false;
}
});
play.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("buttonclicked","");
if(playpause == false)
{
video.start();
Log.v("videostarted","");
playpause = true;
play.setVisibility(View.GONE);
}
}
});
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please help to fix this.

Related

Android gesture is not working in activity

i have some images in the activity i need to implement gesture when i swife images so bellow i have posted my code (there is full code but the gesture is not working)
As bellow is my code (activity class ), implemented gesture
i have created gesture in my code but it is not working,
it is not calling onTouch event also, when i click its not printing any logs
how can i solve this problem.
at the last there is onFling method is there
public class PageView extends AppCompatActivity implements ReadOutTextAnimation.ReadingPageCompleted, OnGestureListener {
public static final String TAG = PageView.class.getName();
private Button mPlayBtn;
private Button mBtnNext;
private Button mBackBtn;
private ImageView mBgImage;
private Integer mPageNumber;
private String mMainFolder;
private Handler handler = new Handler();
private Runnable mTimerCallback;
private boolean mIsPaused;
private GestureDetector gestureDetector;
private boolean isoneclick;
private LinearLayout mLenearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
gestureDetector = new GestureDetector(this, this);
// mLenearLayout = (LinearLayout)findViewById(R.id.clickhide);
// mLenearLayout.setVisibility(View.INVISIBLE);
isoneclick = true;
if (AppManager.getInstance().getNeedsBGMusic())
SoundManager.getInstance().resumeBackgroundMusic();
mPageNumber = 1;
mMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("Books", Context.MODE_PRIVATE));
mMainFolder += "/Downloads" + AppManager.getInstance().getCurrentBook().getBookId().toString();
mTimerCallback = new Runnable() {
#Override
public void run() { loadNextPage(); }
};
ReadOutTextAnimation.getInstance().setListner(this);
mBgImage = findViewById(R.id.page_view_bgimage);
mBgImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onSlideViewButtonClick(v);
//loadNextPage();
}
});
//Next Button Action
mBtnNext = findViewById(R.id.page_view_nextbtn);
mBtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadNextPage();
}
});
//Back Button Action
mBackBtn = findViewById(R.id.page_view_backbtn);
mBackBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadPreviousPage();
}
});
//Play & Pause Button Action
mPlayBtn = findViewById(R.id.page_view_palybtn);
mPlayBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mIsPaused){
resumeApp();
} else {
pauseApp();
}
}
});
loadPageDetails();
enableBtn();
Button mDoneBtn = findViewById(R.id.page_view_done);
mDoneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
#Override
protected void onPause() {
super.onPause();
pauseApp();
}
private void enableBtn(){
if(mPageNumber > 1) {
mBackBtn.setEnabled(true);
mBtnNext.setEnabled(true);
mBackBtn.setAlpha(.9f);
}else {
mBackBtn.setAlpha(.5f);
mBtnNext.setEnabled(true);
}
}
private void loadPageDetails(){
ReadOutTextAnimation.getInstance().stopReadOut();
PageDetailProperties mCurrentPage = AppManager.getInstance().getCurrentBook().getPageDetailsForNumber(mPageNumber);
if (mCurrentPage.getPageAudioDurationJSON() == null)
{
loadNextPage();
return;
}
File imgFile = new File(mMainFolder,ContentfulConstants.PAGE_IMAGE+ mPageNumber.toString() +".png");
if (imgFile.exists()){
Bitmap bookImage = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
mBgImage.setImageBitmap(bookImage);
}
File soundFile = new File(mMainFolder,ContentfulConstants.PAGE_AUDIO+mPageNumber.toString()+".mp3");
if (soundFile.exists()){
SoundManager.getInstance().playPageAudio(soundFile.getPath());
}
ReadOutTextAnimation.getInstance().startReadOut(mPageNumber);
mIsPaused = false;
mPlayBtn.setBackgroundResource(R.drawable.pause);
}
private void loadNextPage(){
handler.removeCallbacks(mTimerCallback);
mPageNumber++;
if (mPageNumber > AppManager.getInstance().getCurrentBook().getPageDetail().size()){
mPageNumber = AppManager.getInstance().getCurrentBook().getPageDetail().size();
ReadOutTextAnimation.getInstance().setListner(null);
ReadOutTextAnimation.getInstance().stopReadOut();
SoundManager.getInstance().stopALL();
Intent intent = new Intent(this , BookCompleteView.class);
this.finish();
startActivity(intent);
} else {
loadPageDetails();
enableBtn();
}
}
private void loadPreviousPage(){
mPageNumber--;
handler.removeCallbacks(mTimerCallback);
if (mPageNumber < 1){
mPageNumber = 1;
} else {
loadPageDetails();
}
enableBtn();
}
#Override
public void readingPageCompleted() {
handler.postDelayed(mTimerCallback, 700);
}
#Override
public void onBackPressed() {
ReadOutTextAnimation.getInstance().setListner(null);
handler.removeCallbacks(mTimerCallback);
ReadOutTextAnimation.getInstance().stopReadOut();
SoundManager.getInstance().stopALL();
super.onBackPressed();
}
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onTouchEvent(MotionEvent motionEvent) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "touch" , Toast.LENGTH_SHORT).show();
gestureDetector.onTouchEvent(motionEvent);
return false;
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
System.out.println("Gesture event touch ");
if (motionEvent.getX() - motionEvent1.getX() > 50) {
loadNextPage();
return true;
}
if (motionEvent1.getX() - motionEvent.getX() > 50) {
loadPreviousPage();
return true;
}
return false;
}
}
If I understood you correctly. You have to set touch listener for your image view, instead of activity.
mBgImage.setOnTouchListener(yourTouchListener);

Android animation using radio button

I created one animation in which I used one radio button as object and a matrix background. when the activity starts the object will move along the each cells in the matrix. now I want the object to be stopped when i click on it. how can i do this??
public class MainActivity extends Activity {
RadioButton rad;
Animation animation1;
Context context = this;
Button move;
Handler handler = new Handler();
Runnable handlerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridView1);
rad = (RadioButton) findViewById(R.id.radioButton1);
// move=(Button)findViewById(R.id.button1);
gridview.setAdapter(new ImageAdapter(this));
animation1 = AnimationUtils.loadAnimation(this, R.anim.animation);
// userInputHandler();
/*
* move.setOnClickListener(new View.OnClickListener() {
*
* #Override public void onClick(View arg0) { // TODO Auto-generated
* method stub
*/
handlerTask = new Runnable() {
public void run() {
if (rad.isChecked()) {
animation1.cancel();
} else {
rad.startAnimation(animation1);
}
}
};
handlerTask.run();
// }
// });
}
public void onAnimationEnd(Animation animation) {
rad.setVisibility(View.VISIBLE);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
}

Show Text in TextView when video reaches current time Android

I want to make a text appear in TextView when the video reaches current time. I've tried this code :
public class VideoActivity extends Activity {
private static final String MY_AD_UNIT_ID = null;
protected static final String TAG = null;
private AdView adView;
int waktu;
Button btncek;
private MediaController controller = null;
TextView textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);
Bundle bundle = getIntent().getExtras();
String urlbarus = getIntent().getStringExtra("urlbaru");
final VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath(urlbarus);
MediaController.MediaPlayerControl mcEvents = new MediaController.MediaPlayerControl() {
public void start() {
videoView.start();
}
public void seekTo(int pos) {
videoView.seekTo(pos);
}
public void pause() {
videoView.pause();
}
public boolean isPlaying() {
return videoView.isPlaying();
}
public int getDuration() {
return videoView.getDuration();
}
public int getCurrentPosition() {
return videoView.getCurrentPosition();
}
public boolean canSeekForward() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canPause() {
return true;
}
#Override
public int getBufferPercentage() {
// TODO Auto-generated method stub
return 0;
}
};
controller = new MediaController(this);
controller.setMediaPlayer(mcEvents);
controller.setAnchorView(findViewById(R.id.wrapper));
videoView.start();
if(mcEvents.getCurrentPosition()>=10000){
textView2.setText("kebaca");
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
controller.show();
return false;
}
This is the code for showing text :
if(mcEvents.getCurrentPosition()>=10000){
textView2.setText("kebaca");
}
I have tried that code, but nothing happens (textview2 does not show the text)
Can anyone help me?
Try something like..
final MediaController.MediaPlayerControl mcEvents = ....
textView2.setText(""); // edited
new Thread(new Runnable() {
#Override
public void run() {
while (mcEvents.getCurrentPosition()<10000){
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) { }
}
runOnUiThread(new Runnable() {
#Override
public void run() {
textView2.setText("kebaca");
}
});
}
}).start();

How to play video in viewflipper

I have a viewflipper in which there are videos and images.Everything works fine when it run for first time.After the last child is shown and the view are repeated,in that case videos are not playing and a black screen remain all the time.
Thanks in advance.
My code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.kiosk_mode);
mSelectedPressoId=PreferenceConnector.readString(KioskModeActivity.this, PreferenceConnector.SELECTED_PRESSO_ID, "");
bindControl();
fetchSlidesFromDb();
}
public void bindControl(){
mFlipper=(ViewFlipper)findViewById(R.id.viewflipper);
mParentView=(FrameLayout)findViewById(R.id.parent_background);
mKioskModeIcon=(ImageView)findViewById(R.id.kiosk_mode_icon);
mExitBar=(LinearLayout)findViewById(R.id.exit_bar);
detector = new GestureDetector(new SwipeGestureDetector());
mCounter=new MyCounter(10000, 1000);
mActivePointers = new SparseArray<PointF>();
mKioskModeIcon.setOnClickListener(this);
mHomeIcon=(ImageView)findViewById(R.id.homeIcon);
mHomeIcon.setOnClickListener(this);
mCommonMethod=new CommonMethod(getApplicationContext());
position=new ArrayList<String>();
mTransitionTime=PreferenceConnector.readString(getApplicationContext(), PreferenceConnector.TRANSITION_TIME, "");
Log.v("transition time from prefrence isss",mTransitionTime+">>>");
if(mTransitionTime.equalsIgnoreCase("")){
mFlipper.setFlipInterval(10000);
}else{
int mTransitionTimeInMills=Integer.parseInt(mTransitionTime)*1000;
mFlipper.setFlipInterval(mTransitionTimeInMills);
}
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(
getBaseContext(), R.anim.push_left_out));
mFlipper.setInAnimation(AnimationUtils.loadAnimation(
getBaseContext(), R.anim.push_left_in));
mFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {
Log.d("new view is ",">>>>>>>>>>>>>>>");
}
public void onAnimationEnd(Animation animation) {
mVideoPosition=mFlipper.getDisplayedChild();
Log.d("new view is flipped",mVideoPosition+">>>>>>>>>>>>>>>"+mPressoPathList.get(mVideoPosition));
if(mSlideTypeList.get(mVideoPosition).equalsIgnoreCase("video")){
Log.d("onside iffffff",myVideoView+">>>>>");
myVideoView.start();
}
}
});
mFlipper.startFlipping();
mFlipper.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
detector.onTouchEvent(event);
return true;
}
});
}
public void fetchSlidesFromDb(){
PresentationDatabase databaseClass = new PresentationDatabase(getApplicationContext(), DatabaseUtil.PresenationTable.DATABASE_NAME, null, DatabaseUtil.PresenationTable.DATABASE_VERSION);
SQLiteDatabase db = databaseClass.getReadableDatabase();
mPressoPathList=new ArrayList<String>();
mSlideTypeList=new ArrayList<String>();
String where = DatabaseUtil.PresenationTable.PRESENTATION_ID + "="
+"'"+mSelectedPressoId+"'";
Cursor mCursor=db.query(DatabaseUtil.PresenationTable.TABLE_NAME, null, where,
null, null, null, null);
Log.e("cursor size isssssss",mCursor.getCount()+">>");
if(mCursor.getCount()>0){
mCursor.moveToFirst();
do{
String slide_content_path=mCursor.getString(mCursor.getColumnIndex(DatabaseUtil.PresenationTable.CONTENT_LOCAL_PATH));
String mSlideType=mCursor.getString(mCursor.getColumnIndex(DatabaseUtil.PresenationTable.SLIDE_TYPE));
if(!mSlideType.equalsIgnoreCase("pdf")){
mPressoPathList.add(slide_content_path);
mSlideTypeList.add(mSlideType);
}
}while(mCursor.moveToNext());
}
Log.d("slide path issssssss",mPressoPathList+"");
setFlipperImage(mSlideTypeList, mPressoPathList);
mCursor.close();
db.close();
}
//create view to be put in view flipper
private void setFlipperImage(ArrayList<String>mSlideType,ArrayList<String>mSlidePath) {
int size=mSlideType.size();
for(int i=0;i<size;i++){
if(mSlideType.get(i).equalsIgnoreCase("image")){
ImageView image = new ImageView(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(layoutParams);
image.setScaleType(ScaleType.FIT_XY);
//image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
bmp = BitmapFactory.decodeFile(mSlidePath.get(i),options);
image.setImageBitmap(bmp);
image.setTag(i);
position.add(i+"");
mFlipper.addView(image);
}else{
myVideoView = new VideoView(this);
myVideoView.setVideoPath(mSlidePath.get(i));
Log.e("my video path isssss",mSlidePath.get(i));
myVideoView.requestFocus();
myVideoView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// l.setBackgroundColor(Color.BLACK);
myVideoView.setKeepScreenOn(true);
mFlipper.addView(myVideoView);
myVideoView.start();
myVideoView.seekTo(500);
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mFlipper.stopFlipping();
}
});
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("inside oncomplete listner",">>>>>");
myVideoView.stopPlayback();
mVideoPosition=mVideoPosition+1;
mFlipper.setDisplayedChild(mVideoPosition);
mFlipper.startFlipping();
myVideoView.seekTo(500);
}
});
}
//
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//do nothing*
}
I solved this issue by adding
myVideoView.start();
into the Event Listener
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mFlipper.stopFlipping();
myVideoView.start();
}
});

Playing audio file in android

I got stuck with one requirement i need to play a media file in android.I will post the screen shot of the page
Actual requirement is the user wants to play the audio file in his recordings list.The recordings list are shown through list adapter.I want to display the play progress of the audio file on the same page(as like shown in the screen shot).
Iam pasting the code which i have tried.
public class RecordingActivity extends ListActivity implements MediaPlayerControl {
MediaPlayer mMediaPlayer;
MediaController mMediaController;
Handler mHandler;
String OUTPUT_FILE;
RelativeLayout rl;
Context context = null;
Point p;
static final String[] recordings = new String[] {"Example1","Example2",
"Example3","Example4","Example5" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new FirstAdapter(this, recordings));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
showPopup(RecordingActivity.this, p);
// get selected items
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this);
mHandler = new Handler();
mMediaController.setMediaPlayer(RecordingActivity.this);
OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/recorder.mp3";
try{
mMediaPlayer.setDataSource(OUTPUT_FILE);
mMediaPlayer.prepare();
}
catch(Exception e){
}
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show(10000);
mMediaPlayer.start();
mMediaController.setEnabled(true);
}
});
}
});
}
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
RelativeLayout viewGroup = (RelativeLayout) context.findViewById(R.id.reviewlist);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.activity_play, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY,p.x+OFFSET_X,p.y+OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
ImageButton close = (ImageButton) layout.findViewById(R.id.close);
Button b1 =(Button) layout.findViewById(R.id.button1);
Button b2 =(Button) layout.findViewById(R.id.button2);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.stop();
mMediaPlayer.release();
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public int getBufferPercentage() {
int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
return percentage;
}
#Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
#Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
#Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
#Override
public void pause() {
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
}
#Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
#Override
public void start() {
mMediaPlayer.start();
mMediaController.show();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mMediaController.show();
return false;
}
}
The problem am facing is i am able to play the audio file ,but it is not showing any popup layout.
Media player
MediaPlayer mp = new MediaPlayer();
// Set data source -
setDataSource("/sdcard/path_to_song");
// Play audio
mp.start();
// Pause audio
mp.pause();
// Reset mediaplayer
mp.reset();
// Get song length duration - in milliseconds
mp.getDuration();
// Get current duration - in milliseconds
mp.getCurrentDuration();
// Move song to particular second - used for Forward or Backward
mp.seekTo(positon); // position in milliseconds
// Check if song is playing or not
mp.isPlaying(); // returns true or false
Hope this will give you some solution. Refer this link. It will solve your problem
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(PlayAudioActivity.this);
mMediaController.setAnchorView(findViewById(R.id.audioView));
String audioFile = "" ;
try {
mMediaPlayer.setDataSource(audioFile);
mMediaPlayer.prepare();
} catch (IOException e) {
System.out.println("Error in playing audio");
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show(10000);
mMediaPlayer.start();
}
});
}
});
}
if you have done this then.
#Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.stop();
mMediaPlayer.release();
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public int getBufferPercentage() {
int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
return percentage;
}
#Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
#Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
#Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
#Override
public void pause() {
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
}
#Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
#Override
public void start() {
mMediaPlayer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mMediaController.show();
return false;
}
}
public class PlayAudioActivity extends Activity implements MediaPlayerControl {
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
use this .

Categories

Resources