MediaPlayer App is crashing when i try to run - java

I'm beginner in android development and tried to create a simple mediaplayer app which plays media from local source.
But it's crashing when i try to open it.
This is my java code.
package com.example.media;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
Button play,pause;
SeekBar s;
MediaPlayer m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m=MediaPlayer.create(this,R.raw.live);
play=findViewById(R.id.button3);
pause=findViewById(R.id.button4);
s.findViewById(R.id.seekBar);
s.setMax(m.getDuration());
s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)m.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m.pause();
}
});
}
}

You need to initialize the seekbar object instead of calling the findViewById method.
So just replace the dot with equal sign :
s=findViewById(R.id.seekBar);

Related

music is not playing smoothly when updating seekbar every 1 second

After adding Timer() method seekbar should be updated every second, it is getting updated but their is a small pause like updating seekbar require a time to update... this has cause seekbar is not running smoothly gives a feel that there is a lag...
package com.example.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
AudioManager audioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this,R.raw.senorita);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int myMaxValume= audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int myCurrentValume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar valumeRocker = findViewById(R.id.seekBar);
valumeRocker.setMax(myMaxValume);
valumeRocker.setProgress(myCurrentValume);
valumeRocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//timeline part of music app
final SeekBar timeline = findViewById(R.id.timeline);
timeline.setMax(mediaPlayer.getDuration());
//set onChange listner on timeline
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//customise timeline seekbar
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}
, 0, 1000);
}
public void playMe(View view){
mediaPlayer.start();
}
public void pauseMe(View view){
mediaPlayer.pause();
}
}
if i remover Timer method.. every this is fine ... i guess i have done something woring in this ...
help me out
thank you in advance..
You need to check 'fromUser' variable is true or not on seekbar progress changed. Because you will get seekbar change callback in both cases, when the user change manually and when Timer update seekbar progress.
Updated code:-
package com.example.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
AudioManager audioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this,R.raw.senorita);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int myMaxValume= audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int myCurrentValume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar valumeRocker = findViewById(R.id.seekBar);
valumeRocker.setMax(myMaxValume);
valumeRocker.setProgress(myCurrentValume);
valumeRocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//timeline part of music app
final SeekBar timeline = findViewById(R.id.timeline);
timeline.setMax(mediaPlayer.getDuration());
//set onChange listner on timeline
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//customise timeline seekbar
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}
, 0, 1000);
}
public void playMe(View view){
mediaPlayer.start();
}
public void pauseMe(View view){
mediaPlayer.pause();
}
}

Runtime error in thread of music app

**Down here I am trying to build a music app in android studio,my app is working fine displaying all my SD card songs and play too ,all button works but when I click on next button it works normally but don't know why app sometimes gets stopped working and shows runtime exception ->>please open the image of run log of the application.
package com.example.ramandeepsingh.tunes;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.jar.Manifest;
public class PlayerActivity extends AppCompatActivity{
static MediaPlayer mp;//assigning memory loc once or else multiple songs will play at once
int position;
SeekBar sb;
ArrayList<File> mySongs;
Thread updateSeekBar;
Button pause,forward,reverse,next,previous;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
pause = (Button)findViewById(R.id.pause);
forward = (Button)findViewById(R.id.forward);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
reverse = (Button)findViewById(R.id.reverse);
sb=(SeekBar)findViewById(R.id.seekBar);
updateSeekBar=new Thread(){
#Override
public void run(){
int totalDuration = mp.getDuration();
int currentPosition = 0;
// sb.setMax(totalDuration);
while(currentPosition < totalDuration){
try{
sleep(500);
currentPosition=mp.getCurrentPosition();
}
catch (InterruptedException e){
e.printStackTrace();
System.out.println(e);
}
sb.setProgress(currentPosition);
}
}
};
if(mp != null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
if(mp.isPlaying()){
pause.setText(">");
mp.pause();
}
else {
pause.setText("||");
mp.start();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()+5000);
}
});
reverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()-5000);
}
});
next.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.reset();
position=((position+1)%mySongs.size());
Uri u = Uri.parse(mySongs.get( position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.release();
position=((position-1)<0)?(mySongs.size()-1):(position-1);
Uri u = Uri.parse(mySongs.get( position).toString());//%mysongs so that it do not go to invalid position
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
}
});
}}

Change the image transparency using a SeekBar

I have to do an Android Studio project that it has to change the transparency of an ImageView using a SeekBar.
I have this program:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int seekTransparent;
ImageView color;
SeekBar seekBarTransparent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color = (ImageView)findViewById(R.id.changeColor);
seekBarTransparent = (SeekBar) findViewById(R.id.seekBar);
updateNow();
seekBarTransparent.setOnSeekBarChangeListener(seekBarChangeListener);
}
private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new
SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
updateNow();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast in uso",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast non in uso",
Toast.LENGTH_SHORT).show();
}
};
private void updateNow(){
seekTransparent = seekBarTransparent.getProgress();
color.getBackground().setAlpha(192 + seekTransparent * 0x10000);
}
}
I run the application but on my smartphone it doesn't work anyone can help me?
This is the xml of the seekbar
<SeekBar
android:id="#+id/opacitybar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="10"
android:progress="10"
android:layout_alignTop="#+id/opacitylbl"
android:layout_centerHorizontal="true" />
And this is the code of the Activity
logoseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int opacity = 100; // from 0 to 255
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
im_move_zoom_rotate.setAlpha(progresValue * 25);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can change the values if you want

Error: setOnClickListener cannot be resolved

I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.

Android Image Button

Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,
Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});

Categories