I tried creating an app that allow users watch live stream videos on their android device. From my lil research I found out VLC Sdk is cool because it support more protocols. From the code gotten from Tutorial on using vlc sdk
I was able to make the live watching app works, but the issue is I can't seem to add a control (such as pause, play, seeker etc) to the video. Please I need you help. Thanks
vlc sdk used is 1.9.8
Try this:
public class Show_Array extends AppCompatActivity implements IVLCVout.Callback {
private TextView container_extension;
private String stream_typee,stream_idd,container_extensionn ;
private String SAMPLE_URL = "";
public int mHeight;
public int mWidth;
private SurfaceView mVideoSurface = null;
private FrameLayout sdk;
private IVLCVout vlcVout;
private LibVLC mLibVlc = null;
private MediaPlayer mMediaPlayer = null;
private int flag = 0;
private ImageButton Resize;
private Media media;
private ArrayList<String> args;
private SurfaceHolder mSurfaceHolderVideo;
private MediaController controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.show__array);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Referances
Resize = findViewById(R.id.Resize);
mVideoSurface = findViewById(R.id.video_surface);
stream_typee = getIntent().getExtras().getString("stream_type");
stream_idd = getIntent().getExtras().getString("stream_id");
container_extensionn = getIntent().getExtras().getString("container_extension");
args = new ArrayList<>();
args.add("-vvv");
mLibVlc = new LibVLC(this, args);
mMediaPlayer = new MediaPlayer(mLibVlc);
vlcVout = mMediaPlayer.getVLCVout();
sdk = findViewById(R.id.sdk);
Resize_video();
setup_url();
controller = new MediaController(this);
controller.setMediaPlayer(playerInterface);
controller.setAnchorView(mVideoSurface);
mVideoSurface.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
controller.show(10000);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
mLibVlc.release();
}
void Resize_video()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mHeight = displayMetrics.heightPixels;
mWidth = displayMetrics.widthPixels;
Resize.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
flag+=1;
int width = 1000;
int height = 560;
if(flag%2!=0) {
LinearLayout.LayoutParams myImageLayout = new LinearLayout.LayoutParams(width, height);
myImageLayout.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
sdk.setLayoutParams(myImageLayout);
vlcVout.setWindowSize(width,height);
}
else
{
LinearLayout.LayoutParams myImageLayout = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
myImageLayout.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
sdk.setLayoutParams(myImageLayout);
vlcVout.setWindowSize(mWidth,mHeight);
}
}
});
}
void setup_url()
{
//TextView
container_extension = findViewById(R.id.URL);
if (stream_typee.equals("live"))
{
SAMPLE_URL = "http://uautv.ru:2095/"+stream_typee+"/webserver/6tE#BzW73#sX/"+stream_idd+".ts";
container_extension.setText( SAMPLE_URL);
}else
{
SAMPLE_URL = "http://uautv.ru:2095/"+stream_typee+"/webserver/6tE#BzW73#sX/"+stream_idd+"."+container_extensionn;
container_extension.setText( SAMPLE_URL);
}
}
#Override
protected void onStart() {
super.onStart();
vlcVout.setWindowSize(mWidth,mHeight);
vlcVout.setVideoView(mVideoSurface);
vlcVout.attachViews();
mMediaPlayer.getVLCVout().addCallback(this);
if(!SAMPLE_URL.isEmpty()) {
media = new Media(mLibVlc, Uri.parse(SAMPLE_URL));
mMediaPlayer.setMedia(media);
media.release();
mMediaPlayer.play();
}else
{
Toast.makeText(getApplicationContext(),"URL EMPTY",Toast.LENGTH_LONG).show();
}
}
#Override
protected void onStop() {
super.onStop();
mMediaPlayer.stop();
mMediaPlayer.getVLCVout().detachViews();
mMediaPlayer.getVLCVout().removeCallback(this);
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public void onNewLayout(IVLCVout vlcVout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen)
{ }
#Override
public void onSurfacesCreated(IVLCVout vlcVout) {
}
#Override
public void onSurfacesDestroyed(IVLCVout vlcVout) {
}
private MediaController.MediaPlayerControl playerInterface = new MediaController.MediaPlayerControl() {
public int getBufferPercentage() {
return 0;
}
public int getCurrentPosition() {
float pos = mMediaPlayer.getPosition();
return (int)(pos * getDuration());
}
public int getDuration() {
return (int)mMediaPlayer.getLength();
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public void pause() {
mMediaPlayer.pause();
}
public void seekTo(int pos) {
mMediaPlayer.setPosition((float)pos / getDuration());
}
public void start() {
mMediaPlayer.play();
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
#Override
public int getAudioSessionId() {
return 0;
}
};
}
I was looking into this as well. You need to implement this yourself.
Simply use FrameLayout and overlay a view ( button? ) per functionality on the video surface.
Then make calls to the VLC MediaPlayer play() stop() pause() etc.
If you really want to dive deep then get the sources of the master, and see how it's done:
git clone https://code.videolan.org/videolan/vlc-android.git
Related
I'm building app which make tic sound on clicking the button. But the sound stops after clicking 14 time. The code is here:
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view){
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tic_sound);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();
}
});
No need to initialise it every time you click the button.
private MediaPlayer _mediaPlayer;
#Override
protected void onResume()
{
super.onResume();
_mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tic_sound);
_mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState)
{
//...
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view) {
_mediaPlayer.start();
}
});
}
For short sounds i would recommend using SoundPool
Class SoundPlayer(){
private final static float leftVol = 1.0f;
private final static float rightVol = 1.0f;
private final static float rate = 1.0f;
private final static int loop = 0;
private int priority = 1;
private int audioId = = -1;
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
SoundPool soundPool= new SoundPool.Builder()
.setMaxStreams(2)
.setAudioAttributes(attributes)
.build();
public void loadSounds(Context context){
audioId = soundPool.load(context, R.raw.tic_sound, priority);
}
public void playAudio() {
soundPool.play(audioId, leftVol, rightVol, priority, loop, rate) ;
}
}
And use it like this
private SoundPlayer soundPlayer;
....
....
onCreate ...{
soundPlayer = SoundPlayer();
soundPLayer.loadSounds(this);
dTextVeiw.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View view){
soundPLayer.playAudio();
}
I'm working on the Android TV app which uses VideoView to play the video from the url. Each time the video is paused and then resumed - it takes some time to pre-download it first (few seconds during which progress bar appears). The goal is to buffer the video during playback so that when I pause it and then resume - it resumes from pre-buffered state immediately without delay. I've also tried videoView.resume() method, but it doesn't help either. Is there a way to do that or am I doing something wrong?
Here is the code which plays/resumes the video:
public void onFragmentPlayPause(final Video video,
final VideoQuality quality,
final int position,
final Boolean play) {
stopSeeking();
videoView.setVideoPath(video.videoUrl(quality));
if (position == 0 || playbackState == LeanbackPlaybackState.IDLE) {
setupCallbacks();
playbackState = LeanbackPlaybackState.IDLE;
}
if (play && playbackState != LeanbackPlaybackState.PLAYING) {
progressBar.setVisibility(View.VISIBLE);
playbackState = LeanbackPlaybackState.PLAYING;
if (position > 0) {
videoView.seekTo(position);
videoView.start();
}
playbackFragment.startProgressObservation(progressFlowable());
} else {
playbackState = LeanbackPlaybackState.PAUSED;
videoView.pause();
playbackFragment.stopProgressObservation();
}
updatePlaybackState(position);
updateMetadata(video);
}
Unfortunately, I've didn't find the way to achieve this with VideoView, only using ExoPlayer, thanks to #pskink for the right direction. The API was returning mp4 videos, so I've used ExtractorMediaSource. The complete demo example with support of multiple formats can be found on ExoPlayer GitHub page.
Here is the final code I've ended up with using ExoPlayer:
private void playPause(final Video video, final int position, final Boolean play) {
if (position == 0 || playbackState == LeanbackPlaybackState.IDLE) {
setupCallbacks();
playbackState = LeanbackPlaybackState.IDLE;
}
if (play && playbackState != LeanbackPlaybackState.PLAYING) {
progressBar.setVisibility(View.VISIBLE);
playbackState = LeanbackPlaybackState.PLAYING;
player.start();
playbackFragment.startProgressObservation(progressFlowable());
} else {
playbackState = LeanbackPlaybackState.PAUSED;
player.pause();
playbackFragment.stopProgressObservation();
}
updatePlaybackState(position);
updateMetadata(video);
}
And Player implementation:
public class Player implements MediaController.MediaPlayerControl, ExoPlayer.EventListener {
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private final SimpleExoPlayer exoPlayer;
private final Context context;
private OnErrorListener onErrorListener;
private OnPreparedListener onPreparedListener;
private OnCompletionListener onCompletionListener;
private String url;
public interface OnErrorListener {
void onError(final Exception e);
}
public interface OnPreparedListener {
void onPrepared();
}
public interface OnCompletionListener {
void onComplete();
}
public Player(final Context context) {
this.context = context;
final #SimpleExoPlayer.ExtensionRendererMode int extensionRendererMode =
SimpleExoPlayer.EXTENSION_RENDERER_MODE_OFF;
final TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
this.exoPlayer = ExoPlayerFactory.newSimpleInstance(
context,
new DefaultTrackSelector(videoTrackSelectionFactory),
new DefaultLoadControl(),
null,
extensionRendererMode
);
this.exoPlayer.addListener(this);
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return true;
}
#Override
public boolean canSeekForward() {
return true;
}
#Override
public int getAudioSessionId() {
throw new UnsupportedOperationException();
}
#Override
public int getBufferPercentage() {
return exoPlayer.getBufferedPercentage();
}
#Override
public int getCurrentPosition() {
return exoPlayer.getDuration() == com.google.android.exoplayer2.C.TIME_UNSET ? 0
: (int) exoPlayer.getCurrentPosition();
}
#Override
public int getDuration() {
return exoPlayer.getDuration() == com.google.android.exoplayer2.C.TIME_UNSET ? 0
: (int) exoPlayer.getDuration();
}
#Override
public boolean isPlaying() {
return exoPlayer.getPlayWhenReady();
}
#Override
public void start() {
exoPlayer.setPlayWhenReady(true);
}
#Override
public void pause() {
exoPlayer.setPlayWhenReady(false);
}
public void stop() {
exoPlayer.seekTo(0);
pause();
}
public void setOnErrorListener(final OnErrorListener onErrorListener) {
this.onErrorListener = onErrorListener;
}
public void setOnPreparedListener(final OnPreparedListener onPreparedListener) {
this.onPreparedListener = onPreparedListener;
}
public void setOnCompletionListener(final OnCompletionListener onCompletionListener) {
this.onCompletionListener = onCompletionListener;
}
public void setVolume(final float volume) {
exoPlayer.setVolume(volume);
}
public void release() {
exoPlayer.release();
}
public void updateUrl(final String url) {
this.url = url;
exoPlayer.prepare(buildMediaSource(Uri.parse(url)));
}
public SimpleExoPlayer exoPlayer() {
return exoPlayer;
}
#Override
public void seekTo(final int timeMillis) {
exoPlayer.seekTo(timeMillis);
}
#Override
public void onTimelineChanged(final Timeline timeline, final Object manifest) {
}
#Override
public void onTracksChanged(final TrackGroupArray trackGroups, final TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(final boolean isLoading) {
}
#Override
public void onPlayerStateChanged(final boolean playWhenReady, final int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) {
onPreparedListener.onPrepared();
}
if (playbackState == ExoPlayer.STATE_ENDED) {
onCompletionListener.onComplete();
}
}
#Override
public void onPlayerError(final ExoPlaybackException error) {
onErrorListener.onError(error);
}
#Override
public void onPositionDiscontinuity() {
}
public String url() {
return url;
}
private MediaSource buildMediaSource(final Uri uri) {
return new ExtractorMediaSource(uri, buildDataSourceFactory(true), new DefaultExtractorsFactory(),
null, null);
}
private DataSource.Factory buildDataSourceFactory(final DefaultBandwidthMeter bandwidthMeter) {
return new DefaultDataSourceFactory(context, bandwidthMeter,
buildHttpDataSourceFactory(bandwidthMeter));
}
private HttpDataSource.Factory buildHttpDataSourceFactory(final DefaultBandwidthMeter bandwidthMeter) {
return new DefaultHttpDataSourceFactory(Util.getUserAgent(context, Application.TAG), bandwidthMeter);
}
private DataSource.Factory buildDataSourceFactory(final boolean useBandwidthMeter) {
return buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
}
}
First, WebView starting load a url at invisible(setVisibility(View.INVISIBLE))on service. and wait until the url loading is done.
finally, set the visibility is show(setVisibility(View.VISIBLE)), the WebView show empty screen(white screen) about 0.5 second. then show rendered web page.
I want to show rendered web page immediately on WebView without empty screen.
like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, ServiceViewManager.class));
setUI();
}
private void setUI()
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(createLoadUrlInvisible());
layout.addView(createShowWebView());
this.setContentView(layout);
}
private Button createLoadUrlInvisible()
{
Button button = new Button(this);
button.setText("LoadUrlInvisible (Click first)");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WebPageServiceView.instance().loadUrlInvisible();
}
});
return button;
}
private Button createShowWebView()
{
Button button = new Button(this);
button.setText("ShowWebView (Click second)");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WebPageServiceView.instance().setVisibility(View.VISIBLE);
}
});
return button;
}
}
public class ServiceViewManager extends Service {
private static ServiceViewManager inst = null;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
public static ServiceViewManager Instance()
{
return inst;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
inst = this;
return START_NOT_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
inst = null;
super.onDestroy();
}
public void add(final View view, final WindowManager.LayoutParams params)
{
((WindowManager)getSystemService(Service.WINDOW_SERVICE)).addView(view, params);
}
}
public class WebPageServiceView extends FrameLayout {
private static WebPageServiceView inst = null;
public WebPageServiceView() {
super(ServiceViewManager.Instance());
}
public static WebPageServiceView instance() {
if(inst == null)
inst = new WebPageServiceView();
return inst;
}
private void addViewToService()
{
WindowManager.LayoutParams serviceParams = new WindowManager.LayoutParams();
serviceParams.x = 0;
serviceParams.y = 0;
serviceParams.type = WindowManager.LayoutParams.TYPE_PHONE;
serviceParams.flags = WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
serviceParams.format = PixelFormat.TRANSPARENT;
ServiceViewManager.Instance().add(this, serviceParams);
}
private void addWebViewAndLoadUrl()
{
WebView webView = new WebView(getContext());
webView.loadUrl("http://naver.com");
webView.setWebViewClient(new WebViewClient());
this.addView(webView);
}
public void loadUrlInvisible() {
addViewToService();
addWebViewAndLoadUrl();
setVisibility(INVISIBLE);
}
}
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();
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 .