I have SurfaceView and i play video from sdcard.When video is finished i try to show image 5 second and then play again my video.when i run my app and video has finished i have IllegalStateException
videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
player = new MediaPlayer();
controller = new VideoControllerView(this);
player.setOnPreparedListener(this);
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
String filePath = Environment.getExternalStorageDirectory() + "/myvideo/Sample.mp4";
player.setDataSource(filePath);
player.setOnPreparedListener(this);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoSurface.setVisibility(View.INVISIBLE);
findViewById(R.id.imageView).setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
findViewById(R.id.imageView).setVisibility(View.INVISIBLE);
videoSurface.setVisibility(View.VISIBLE);
}
});
player.start();
}
}, 5000);
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
controller.show();
return false;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
player.prepareAsync();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(this);
controller.setAnchorView((RelativeLayout) findViewById(R.id.videoSurfaceContainer));
player.start();
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return true;
}
#Override
public boolean canSeekForward() {
return true;
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public int getCurrentPosition() {
return player.getCurrentPosition();
}
#Override
public int getDuration() {
return player.getDuration();
}
#Override
public boolean isPlaying() {
return player.isPlaying();
}
#Override
public void pause() {
player.pause();
}
#Override
public void seekTo(int i) {
player.seekTo(i);
}
#Override
public void start() {
player.start();
}
#Override
public boolean isFullScreen() {
return false;
}
#Override
public void toggleFullScreen() {
}
maybe problem is in this line
videoSurface.setVisibility(View.VISIBLE);
what am i doing wrong in my code?
Try player.release() in onComplation before player.start()
Related
I have a fragment where i am using media player and using my own custom controller class. I am not being able to set controller.setMediaPlayer inside fragment. I have implemented VideoControllerView.MediaPlayerControl class in fragment.
My fragment class is :
public class HomeFragment extends Fragment implements SurfaceHolder.Callback, VideoControllerView.MediaPlayerControl {
#BindView(R.id.videoSurfaceContainer)
FrameLayout videoContainer;
#BindView(R.id.videoSurface)
SurfaceView videoSurface;
#BindView(R.id.progress)
ProgressBar progressBar;
#BindView(R.id.errorTv)
RelativeLayout relativeLayout;
#BindView(R.id.errorTextView)
TextView errorTextView;
public MediaPlayer player;
public VideoControllerView controller;
private Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, v);
player = new MediaPlayer();
controller = new VideoControllerView(getActivity());
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
progressBar.setVisibility(View.VISIBLE);
playVideo("http://202.166.200.170:8081/otv/aastha.stream/playlist.m3u8");
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
relativeLayout.setVisibility(View.GONE);
player.reset();
progressBar.setVisibility(View.VISIBLE);
playVideo("http://202.166.200.170:8081/otv/aastha.stream/playlist.m3u8");
} catch (Exception e) {
e.printStackTrace();
}
}
});
return v;
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
player.setDisplay(surfaceHolder);
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
public void playVideo(String link) {
try {
videoSurface.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (controller.isShowing())
controller.hide();
else
controller.show();
return false;
}
});
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer();
controller.setAnchorView((FrameLayout) getActivity().findViewById(R.id.videoSurfaceContainer));
player.start();
progressBar.setVisibility(View.GONE);
if (relativeLayout.isShown())
relativeLayout.setVisibility(View.GONE);
}
});
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(getActivity(), Uri.parse(link));
try {
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
progressBar.setVisibility(View.GONE);
errorMessage("Streaming Error");
player.reset();
errorTextView.setText(getResources().getString(R.string.error_msg_video));
return false;
}
});
player.setScreenOnWhilePlaying(true);
Log.d("after click", "last try");
} catch (Exception e) {
e.printStackTrace();
}
}
public void errorMessage(String message) {
try {
relativeLayout.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void start() {
}
#Override
public void pause() {
}
#Override
public int getDuration() {
return 0;
}
#Override
public int getCurrentPosition() {
return 0;
}
#Override
public void seekTo(int pos) {
}
#Override
public boolean isPlaying() {
return false;
}
#Override
public boolean canPause() {
return false;
}
#Override
public boolean isFullScreen() {
return false;
}
#Override
public void toggleFullScreen() {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = activity;
}
if you see inside playVideo method inside setOnpreparedListener i am getting error in controller.setMediaPlayer() because i am not being able to pass the context here.
what can be done here to solve the error?
This is how we can use controller.setMediaPlayer()
// Implement MediaPlayer.OnPreparedListener
#Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(this);
controller.setAnchorView((FrameLayout)
findViewById(R.id.videoSurfaceContainer));
player.start();
}
// End MediaPlayer.OnPreparedListener
This is example to use costume controller
If you want, you could get the context from there using HomeFragment.this.getContext(), like:
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(HomeFragment.this.getContext());
controller.setAnchorView((FrameLayout) getActivity().findViewById(R.id.videoSurfaceContainer));
player.start();
progressBar.setVisibility(View.GONE);
if (relativeLayout.isShown())
relativeLayout.setVisibility(View.GONE);
}
});
I just moved controller.setMediaPlayer(this); to onCreateView and it worked.
I'm trying to implement live stream radio by using fragment and background service, however every time I click to radio button on navigation drawer I couldn't reach the player because it generates one more and I can only control the second one. Here is my fragment class.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_radio, container, false);
init(v);
ApiManager.getInstance().getRadioBroadcast(new ApiResponseLive.GetLiveBroadcastListener() {
#Override
public void onSuccess(StreamModel days) {
turnRadioOn(ApiManager.streamModel);
}
#Override
public void onFailure(String responseString) {
}
});
registerHandler(v);
return v;
}
private void turnRadioOn(StreamModel streamModel) {
HashMap<Integer, ArrayList<GenericStreamItem>> broadcastDays = new HashMap<>();
streamModel.setStreamAllArrays(broadcastDays);
BroadcastPagerAdapter radioPagerAdapter = new BroadcastPagerAdapter(getChildFragmentManager(), getContext(), TAG);
radioPager.setAdapter(radioPagerAdapter);
}
private void init(View v) {
radioPager = (ViewPager) v.findViewById(R.id.broadcastRadioPager);
radioTabLayout = (TabLayout) v.findViewById(R.id.tabsDays);
radioTabLayout.setupWithViewPager(radioPager);
radioPlay = (ToggleButton) v.findViewById(R.id.playButton);
radioScrollView = (ScrollView) v.findViewById(R.id.radioScroll);
}
private void start(String url) {
try {
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setDataSource(url);
mp.prepareAsync();
mp.setOnCompletionListener(this);
Log.e("Radio::", "Working");
} catch (Exception e) {
Log.e("StreamAudioDemo", e.getMessage());
e.printStackTrace();
}
}
private void stop() {
try {
mp.pause();
mp.stop();
mp.reset();
mp.release();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void stopRadio() {
try {
Log.e("stopRadio::", "Pause");
mp.pause();
mp.stop();
mp.reset();
mp.release();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
#Override
public void onCompletion(MediaPlayer mp) {
}
private void registerHandler(View v) {
radioPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.e("Toggle::", "Enabled");
radioService = new RadioService();
radioService.initMediaPlayer(ApiManager.RADIO_URL);
} else {
Log.e("Toggle::", "Disabled");
stop();
stopRadio();
radioService.onDestroy();
}
}
});
v.findViewById(R.id.radioBroadcast).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
radioScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
public class RadioService extends Service implements MediaPlayer.OnErrorListener {
public void initMediaPlayer(String url) {
// ...initialize the MediaPlayer here...
ApiManager.getInstance().getRadioURL(new ApiResponseLive.GetTVStreamUrlListener() {
#Override
public void onSuccess(String tvUrl) {
start(ApiManager.RADIO_URL);
mp.setOnErrorListener(RadioService.this);
}
#Override
public void onFailure(String responseString) {
}
});
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// ... react appropriately ...
// The MediaPlayer has moved to the Error state, must be reset!
mp.reset();
return false;
}
#Override
public void onDestroy() {
if (mp != null) mp.release();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
}
Here is how I call fragment:
RadioFragment radioFragment = new RadioFragment();
fragment = fm.findFragmentByTag("radio");
radioDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (fragment == null) {
fm.beginTransaction()
.remove(fm.findFragmentById(R.id.activity_content))
.add(R.id.activity_content, radioFragment,
"radio").addToBackStack("main").commit();
} else {
fm.beginTransaction().replace(R.id.activity_content,
radioFragment, "radio");
}
fullView.closeDrawers();
}
});
I use MediaPlayer for displaying video. I need to loop it. I use native method setLooping(), but it doesn't work. Video finished and that's all. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
TextureView videoViewFullScreen = (TextureView) findViewById(R.id.video_screen);
path = getIntent().getExtras().getString(SettingsActivity.FILE_PATH);
cycle = getIntent().getExtras().getBoolean(SettingsActivity.CYCLE);
position = getIntent().getIntExtra(FullScreenActivity.POSITION, -1);
videoViewFullScreen.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
final Surface surf = new Surface(surface);
try {
mediaPlayerFullScreen = new MediaPlayer();
mediaPlayerFullScreen.setDataSource(path);
mediaPlayerFullScreen.setSurface(surf);
if (cycle) {
mediaPlayerFullScreen.setLooping(true);
}
mediaPlayerFullScreen.prepareAsync();
mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if (position != -1) {
Log.d("Position", "Position Full " + position);
mediaPlayerFullScreen.seekTo(position);
}
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mediaPlayerFullScreen.stop();
mediaPlayerFullScreen.release();
mediaPlayerFullScreen = null;
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayerFullScreen != null) {
mediaPlayerFullScreen.stop();
mediaPlayerFullScreen.release();
mediaPlayerFullScreen = null;
}
}
I looked all similar questions and tried all answers that find here, but none of them helped me.
I have Android version 4.4.2, Firmware version 4.5, Kernel version 3.3.0
Could someone help me? Is there another way to loop video?
I find replacement for setLooping(true), this works for me:
mediaPlayerFullScreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayerFullScreen.reset();
try {
mediaPlayerFullScreen.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayerFullScreen.setSurface(surf);
mediaPlayerFullScreen.prepareAsync();
mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
}
});
I am trying to use the mediacontroller to play audio. I am able to get the audio working but when the song starts playing the play button on my mediacontroller does not update. If press the play button on the mediacontroller it pauses the song and seeks the progressbar. Then if i press pause it plays the song again. i am calling
mp.start()
does this not update the play button. How can I get the button to change to the pause button when I call start?
public class QuickMusicPlayer implements MediaController.MediaPlayerControl {
private final MediaPlayer mMediaPlayer;
private final MediaController mMediaController;
private Activity mActivity;
public QuickMusicPlayer(View anchorView, Activity activity) {
mActivity = activity;
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(activity);
mMediaController.setMediaPlayer(this);
mMediaController.setAnchorView(anchorView);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
public void setUrl(String url) {
mMediaPlayer.reset();
if (TextUtils.isEmpty(url)) {
ViewUtil.showCroutonAlert(mActivity, R.string.cant_play_music);
} else {
try {
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync();
mMediaController.setEnabled(true);
mMediaController.show(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void start() {
mMediaPlayer.start();
}
#Override
public void pause() {
mMediaPlayer.pause();
}
#Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
#Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
#Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
#Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return true;
}
#Override
public boolean canSeekForward() {
return true;
}
#Override
public int getAudioSessionId() {
return 0;
}
}
Figured it out. I need to call show again after start to update the buttons
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mMediaController.show(0);
}
});
I can't find a way to stop hiding the seekbar. As soon as the song plays the seekbar hides after 2-3 sec. I am really new into this so not really sure how I can approach to this issue. I tried putting mediaController.show() into my methods when the song is playing but no luck. I found this code from a Youtube Channel below is the code:
public class MainActivity extends ListActivity implements OnPreparedListener,
MediaController.MediaPlayerControl {
private static final String TAG = "AudioPlayer";
private ListView list;
private MainArrayAdapter adapter;
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = getListView();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
ArrayList<String> array_list_music = new ArrayList<String>();
array_list_music.add("Sammy Kaye And His Orchestra" + " ### "
+ "Minka (1941)" + " ### "
+ "http://incoming.jazz-on-line.com/a/mp3w/1941_149.mp3");
adapter = new MainArrayAdapter(MainActivity.this, array_list_music);
setListAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Object item = getListView().getItemAtPosition(arg2);
String the_list_item = item.toString();
Toast.makeText(MainActivity.this,
"You clicked " + the_list_item, Toast.LENGTH_LONG)
.show();
String[] aux = the_list_item.split(" ### ");
String url_to_play = aux[2];
playAudio(url_to_play);
}
});
}
private void playAudio(String url_to_play) {
//----- stop & reset
try {
mediaPlayer.stop();
mediaPlayer.reset();
} catch (Exception e) {
// TODO: handle exception
}
try {
mediaPlayer.setDataSource(url_to_play);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
Log.e(TAG, "Could not open file " + url_to_play + " for playback.",
e);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mediaController.show();
return true;
}
#Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
}
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return true;
}
#Override
public boolean canSeekForward() {
return true;
}
#Override
public int getBufferPercentage() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
#Override
public int getDuration() {
return mediaPlayer.getDuration();
}
#Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
#Override
public void pause() {
mediaPlayer.pause();
}
#Override
public void seekTo(int arg0) {
mediaPlayer.seekTo(arg0);
}
#Override
public void start() {
mediaPlayer.start();
}
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(list);
mediaController.show();
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
}