i am trying to insert a audio file into the application which is not showing any kind of error but while am running the program then it is showing some kind of exception that to force close kind of dialog box and here is my code
This is my first activity where am trying to invoke the button into the second activity playing the audio file.
public class Audio extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b2=(Button)findViewById(R.id.b1);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 =new Intent(Audio.this,Audio1.class);
startActivity(i1);
}
});
}
}
public class Audio1 extends Activity {
private MediaPlayer eMediaPlayer= new MediaPlayer();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
eMediaPlayer=MediaPlayer.create(this, R.raw.ab);
eMediaPlayer.start();
}
}
audio is my xml file which is empty presently
R.raw.ab is the resource file which is 830kb audio file
you should call
eMediaPlayer.prepare();
before
eMediaPlayer.start();
Related
I have 3 activity, when I move from 1st act.to 2nd act. one music will start, and when I move from 2nd act. to 3rd act., 2nd activity's music should stop and 3rd activity's music should start. As per my coding, 2nd activity's music is stopping, but 3rd activity's music does not start. my code is given here-under: please help me.
//1st activity code start
public class MainActivity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this,R.raw.bakaratwozeroone);
b = (Button) findViewById(R.id.butmove);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
}
}
//1st activity code end
//2nd activity code start
public class Main2Activity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this, R.raw.mumfive);
b = (Button) findViewById(R.id.butmove2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(Main2Activity.this, Main3Activity.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
if(mp !=null);
mp.release();
mp = null;
}
}
//2nd activity code end
No need to keep MediaPlayer instance on every activity, Use BoundService to run media player and make communication with Service and activities.
For reference,use this link
https://github.com/SimpleMobileTools/Simple-Music-Player
How to run an audio file instantly when clicking on the application button and not when button returns with Android Media Player
Any help is welcome because I did not find solution.
1.you add button
2.
public class Setting extends Activity implements OnClickListener{
private MediaPlayer Music;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_setting);
Music = MediaPlayer.create(this, R.raw.musicname);
findViewById(R.id.button).setOnClickListener(this);
}
public void onClick(View view) {
if (view.getId() == R.id.button) {
Music.start();
}
}
}
only Preview :D
Hi!
I have a service that should be playing an audio file when it is started from an activity (by a click on the button). The problem is though, the service won't start when i click the required button in my activity.
Service class (with imports and some other methods omitted):
public class ClockService extends Service {
MediaPlayer myMediaPlayer;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
myMediaPlayer = MediaPlayer.create(this, R.raw.audio);
myMediaPlayer.setLooping(true);
}
#Override
public void onStart(Intent intent, int startid) {
myMediaPlayer.start();
}
}
The activity it is called from:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startService(new Intent(getBaseContext(), ClockService.class));
}
});
}
}
I've struggled with it for an hour already, and i can't see what's wrong.
u got problem on below code line.u cant use this for context inside service.u need to pass context of mainactivity to service to get mediaplayer instance inside service.
myMediaPlayer = MediaPlayer.create(this, R.raw.audio);//wrong one
The problem is how you started the service
Change this line to startservice(new intent(mainactivity.this, clockservice));
here is a link you can refer to.
I have an Android App That requires a button to open up a new xml page. This is what it's like now, could someone add the necessary code to make it open Page2Activity when I click on the button? Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Void onClick;View arg0; {
// TODO Auto-generated method stub
}
};
}
I figured this out using this method : http://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,but I will try All of yours as well.
Try this code:
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,Page2Activity.class);
startActivity(intent);
}
Then create a new cLass called Page2Activity.
Hope this helps and don't forget to add your activity to the manifest file.
i think you mean something like this:
public class MyClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("com.myaction");
startActivity(i);
}
});
}
}
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying to do call new intents, but my app is always unexpectedly stopped. Do am I correctly call new intents?
First class:
public class first extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button SignIn = (Button) findViewById(R.id.SignIn);
SignIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText pw = (EditText) findViewById(R.id.editPasswd);
if(pw.getText().toString().equals("123")) {
Intent intent = new Intent(first.this, second.class);
startActivity(intent);
}
else {
Toast.makeText(getBaseContext(), "Wrong PIN" ,
Toast.LENGTH_LONG).show();
}
}});
}
}
Second class:
public class second extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginas);
Button Button01 = (Button) findViewById(R.id.Button01);
Button01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(v.getId() == R.id.Button01) {
Intent intent = new Intent(second.this, third.class);
startActivity(intent);
}}
});
}
}
Third class:
public class third extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock);
}}
When I put correct pin code the app show messages "the application () has stopped unexpectedly. Please try again". First I want to know that my code is correct.
whatever the activity u want to display must and mension those activities in AndroidManifest.xml file.For example in ur application first,second,third activities mention in AndroidManifest.xml file