Why MediaPlayer can't be set in MainActivity? - java

Why when I create the MediaPlayer variable in the Main Activity, the app crashes?
( This code works fine )
public class MainActivity extends AppCompatActivity {
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audio = MediaPlayer.create(this, R.raw.my_audio);
}
}
( But this code doesn't work; the app crashes)
public class MainActivity extends AppCompatActivity {
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
PLEASE give me an explanation :(

in second case you havent created MediaPlayer instance at all
MediaPlayer audio;
so every audio.anyMethodCall() will throw NullPointerException
just keep audio = MediaPlayer.create(this, R.raw.my_audio); line in onCreate, thats proper place for initing player
and please learn some basics about programming...

private MediaPlayer audio;
oncreate method
audio= MediaPlayer.create(this, R.raw.shape_of_you);
audio.start();
You can do something like this

The Correct code should be:
public class MainActivity extends AppCompatActivity {
MediaPlayer audio;
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the only change I did was removing declaration before oncreate method:
Earlier:
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
Correct Code:
MediaPlayer audio;
Explanation:
onCreate() method is for definition like adding audio to MediaPlayer, defining textView, etc, since onCreate() gets triggered after creation of activity(activity_main).
You added audio 2 times, which is not needed, you added an audio at first and then replace it with another audio in onCreate() method

Related

Can not extract resource error in Android Studio

I am trying to make a video(movie.mp4) play when I click the imageButton but it keeps saying "Can not extract resource from com.android.aaptcompiler.ParsedResource#7282664d."
this is my code:
`
public class Activity2 extends AppCompatActivity {
VideoView VideoView2;
Uri uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
VideoView2=(VideoView) findViewById(R.id.videoView);
uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.movie);
VideoView2.setVideoURI(uri);
}
public void play(View view) {
VideoView2.start();
}
}
`
I tried changing the source video(movie.mp4) in "raw" directory multiple time, didn't work.
There is a "?" symbol next to the video

How to combine two onClick for change text of Button and play/stop sound

How to combine two functions in one button?
How to make when button clicked - the button text change and sound will play
Now I can only play the sound or just change the button text
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button one = (Button) this.findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String ButtonText=one.getText().toString();
if(ButtonText.equals("Stop")){
one.setText("Play");
}else {
one.setText("Stop");
}
}
});
It seems that you're already changing the button text in your onClick event handler. If you're using some method call to play the desired sound, you can just put that in your onClick handler method as well. Eg:
public void onClick(View v) {
String ButtonText=one.getText().toString();
// method call that plays the sound
if(ButtonText.equals("Stop")){
one.setText("Play");
} else {
one.setText("Stop");
}
}

How to fix the message passing error between the activities

I had created a simple message passing application in android studio to include this code into my Project work.But I was not able to switch the activity by passing a string into another activity.please help me out to find the problem.
When the button is triggered the application got ended itself instead of switching the activity.I had tried many ways to figure it out.
First Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
EditText text;
btn = (Button)findViewById(R.id.button);
text = (EditText)findViewById(R.id.editText);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String mess = text.getText().toString();
Intent i= new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("EXTRA",mess);
startActivity(i);
}
});
}
}
Second Activity(Main2Activity)
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
EditText editText = (EditText)findViewById(R.id.editText2);
editText.setText(getIntent().getStringExtra("EXTRA"));
}
}
The expected output was to exchange the data from one activity to another,
but now the application gets stopped.

How to open new activity that contain RecyclerView and CardView?

I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it

I don't know how to implement interfaces in Java

I have problems with opening an activity, I know that I have to implement interfaces to these interfaces of OnClickListener, OnInitListener. I have tried with #Override. In my android studio it says that I have to implement an abstract on onclick() method in onclicklistener. I am a newbie in programing so any help is much appreciated!
public void addGlossary(View v){
Intent intent = new Intent (this, addGlossary.class);
Button buttonZero = (Button) findViewById(R.id.buttonZero);
startActivity(intent);
}
Here is the code for the activity:
public class addGlossary extends Activity implements OnClickListener, OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech myTTS;
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_glossary);
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.stop();
}
});
When I have implemented those interfaces, and I try to connect to the activity my phone says that the application has stopped. I do not know what the problem is. Any help is appreciated.
What I implemented: onInit was already done
#Override
public void onClick(View v){
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
}
You must implement all the abstract methods in addGlossary , add following code to the class,
1) onClick for OnClickListener
#Override
public void onClick (View v) {
//do someting when the view clicked
}
2) onInit for OnInitListener
#Override
public void onInit (int status) {
//do someting when tts onInit
}
Click here to learn more about Java Interface.

Categories