https://www.youtube.com/watch?v=aD0WWoVDl0I
I have tried this code. Does nothing
My present code is this answer's
https://stackoverflow.com/a/6312631/10903899
Code
public class Radio2 extends Fragment {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_radio2, container, false);
playSeekBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) rootView.findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
buttonStopPlay = (Button) rootView.findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
initializeMediaPlayer();
return rootView;
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
public void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
The worst part is that the stacktrace is empty, which means that im not getting any error in the catch blocks. The code inside the try block is executing. But nothing happens. I even tried changing the URL but nothing.
Related
I am calling stop() method on my mediaplayer instance and it is showing "Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference" which means mediaplayer object is null but when I write it like if(mediaplayer != null) stop(); it executes fine. But if the object is actually null this condition should not execute. Can anybody explain what is happening?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hello_moon, container, false);
Button play = v.findViewById(R.id.play);
Button stop = v.findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp == null) {
mp = MediaPlayer.create(getActivity(), R.raw.one_small_step);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});
mp.start();
}
else {
mp.stop();
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
mp.release();
mp = null;
}
}
});
return v;
}
//this is not working
#Override
public void onDestroy() {
super.onDestroy();
mp.stop();
}
//this works fine
#Override
public void onDestroy() {
super.onDestroy();
if(mp != null)
mp.stop();
}
Please try like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hello_moon, container, false);
Button play = v.findViewById(R.id.play);
Button stop = v.findViewById(R.id.stop);
if(mp == null) {
mp = MediaPlayer.create(getActivity(), R.raw.one_small_step);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});
}
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
mp.start();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
mp.release();
}
}
});
return v;
}
#Override
public void onDestroy() {
super.onDestroy();
if(mp != null)
mp.stop();
}
Actually i have 2 activities , in the main activity i am displaying all the songs using recycler view and setup an onClickListner on it, and when someone taps on it , it launches a new activity(MediaPlayer Activity) and start playing the song along with media controls like (paly, pause and seekBar).
But when i get back to the main activity and click on a button (which basically takes to the MediaPlayer activity again) all the mediaPlayer and seekBar progress are gone also the (play, pause, next and previous buttons) are not responding to the clicks.
This is from where i am starting the MediaPlayer activity :
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.Song.setText(mData.get(position).title);
holder.Artist.setText(mData.get(position).artist);
Glide.with(mcontext).load(mData.get(position).imagePath).
placeholder(R.drawable.unknown_art).into(holder.PhotoId);
holder.Options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mcontext, "You clicked on options", Toast.LENGTH_SHORT).show();
}
});
holder.RL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mcontext, MediaPlayerActivity.class);
intent.putExtra("songPosition",position);
mcontext.startActivity(intent);
}
});
These are the codes in the MediaPlayerActivity :
public class MediaPlayerActivity extends AppCompatActivity {
static MediaPlayer mediaPlayer=null;
static int pos=0;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent intent = getIntent();
pos = intent.getIntExtra("songPosition",pos);
seekBar = (SeekBar)findViewById(R.id.seekBar);
Button next = (Button)findViewById(R.id.button_1);
Button previous = (Button)findViewById(R.id.button_2);
final Button play_pause = (Button)findViewById(R.id.button_3);
final TextView info = (TextView)findViewById(R.id.songInfo);
info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
playMedia(pos);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
playMedia(++pos);
info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);
}catch (IndexOutOfBoundsException e){
Toast.makeText(MediaPlayerActivity.this,"Already the last song!!!",Toast.LENGTH_SHORT).show();
}
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
playMedia(--pos);
info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);
}catch (IndexOutOfBoundsException e){
Toast.makeText(MediaPlayerActivity.this,"Already the first song!!!",Toast.LENGTH_SHORT).show();
}
}
});
play_pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
play_pause.setText(R.string.mp_play);
mediaPlayer.pause();
}
else{
play_pause.setText(R.string.mp_Pause);
mediaPlayer.start();
}
}
});
}
public void playMedia(int pos){
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(VerticalRecyclerViewAdapter.mData.get(pos).songPath);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
setSeekbarProgress();
onSeekbarChange();
playNext();
}
public void playNext(){
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
mediaPlayer.release();
playMedia(++MediaPlayerActivity.pos);
}catch (IndexOutOfBoundsException e){
Toast.makeText(MediaPlayerActivity.this, "Already the last song", Toast.LENGTH_SHORT).show();
}
}
});
}
public void setSeekbarProgress(){
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
try {
if (mediaPlayer != null) {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
}
catch (RuntimeException e){
Log.i("CRASH",""+e.getMessage());
}
}
},0,1000);
}
public void onSeekbarChange(){
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b) {
mediaPlayer.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
And this is where from i wanted to go back to the MediaPlayer Activity (on button click ) :
mFloatingToolbar.setClickListener(new FloatingToolbar.ItemClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.music:
Intent intent = new Intent(getActivity(),MediaPlayerActivity.class);
startActivity(intent);
break;
case R.id.pause :
break;
case R.id.next :
Toast.makeText(getContext(), "Next", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getContext(), "Back", Toast.LENGTH_SHORT).show();
verticalRecyclerViewAdapter.notifyDataSetChanged();
break;
}
}
#Override
public void onItemLongClick(MenuItem item) {
}
});
When I click the image button for more than 15 times it should stop functioning. What is the code that I can use here?
I am new developer and I really need your help. I hope you can help me
here is my code:
public class MainActivity extends Activity{
MediaPlayer mp;
#Override
public 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.main);
ImageButton One = (ImageButton) findViewById(R.id.btnkick);
One.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.kick1);
try
{
mp.prepare();
}
catch (IOException e)
{}
catch (IllegalStateException e)
{}
mp.start();
}
});
ImageButton two = (ImageButton) findViewById(R.id.btnkick1);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.kick1);
try
{
mp.prepare();
}
catch (IOException e)
{}
catch (IllegalStateException e)
{}
mp.start();
}
});
int count=0;
One.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
count=count+1;
if(count<=15)
{
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.kick1);
try
{
mp.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
mp.start();
}
}
});
int counttwo=0;
two.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counttwo=counttwo+1;
if(counttwo<=15)
{
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.kick1);
try
{
mp.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
mp.start();
}
}
});
use this in your code.
I have a problem with pausing a MediaPlayer. When I play it, and click the pause button it's working, but when it's playing longer than 10 seconds it's not pausing.
Here is my code:
private TextView tvState;
private MediaPlayer mediaPlayer = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_player);
ImageButton btnPlay = (ImageButton) findViewById(R.id.btnPlay);
ImageButton btnPause = (ImageButton) findViewById(R.id.btnPause);
TextView tvAuthor = (TextView) findViewById(R.id.tvAuthor);
TextView tvSong = (TextView) findViewById(R.id.tvSong);
tvState = (TextView) findViewById(R.id.tvState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String json = bundle.getString("songModel");
final SongModel songModel = new Gson().fromJson(json, SongModel.class);
tvAuthor.setText(songModel.getAuthor());
tvSong.setText(songModel.getName());
PrepareMediaPlayer(songModel);
}
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
if (mediaPlayer.isPlaying()) {
tvState.setText("Played");
}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
if (!mediaPlayer.isPlaying()) {
tvState.setText("Paused");
}
}
});
}
public void PrepareMediaPlayer(SongModel song) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(song.getLink());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
}
My OS: Android 4.0.4, API 15
This is just a simple android app I am developing, it's meant to play a sound every time you click the button....it works when I click the button at a slow pace, but always crashes if I click the button at a fast pace due to a runtime error - NullPointerException!.....I don't know what I am doing wrong.
public class OSDC_Mini_ProjectActivity extends Activity implements MediaPlayer.OnCompletionListener {
private ImageButton play,pause,stop;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (ImageButton)findViewById(R.id.play);
pause = (ImageButton)findViewById(R.id.pause);
stop = (ImageButton)findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
setup();
}
#Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
mp.stop();
}
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void play() {
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
play.setEnabled(true);
}
catch (Throwable t) {
error(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.test);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
error(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void error(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("報錯啦!")
.setMessage(t.toString())
.setPositiveButton("確定", null)
.show();
}
}
Add null check in every method. Also start media play only if mp is currently not playing anything