Solved

Using the mediaplayer class and java

Posted on 2014-02-23
7
256 Views
Last Modified: 2014-02-28
I would like to create a simple program that I can  play 2 sounds and then stop them and restart them together. Im working on and Android app.  Im new to java and seem to be having a problem with the if statements.  It appears that if I put a send if statement under any first if statement my program crashes when I test it with that function.  

package com.example.newsoundplayer;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
MediaPlayer mpplayer;
MediaPlayer mpplayer2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		Button play = (Button) findViewById(R.id.playbtn);
		Button play2 = (Button) findViewById(R.id.playbtn2);
		Button stop = (Button) findViewById(R.id.stopbtn);
		Button syncerme = (Button) findViewById(R.id.syncer);
		
		play.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
				mpplayer.setLooping(true);
				mpplayer.start();
			}
		});
		play2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep4);
				mpplayer2.setLooping(true);
				mpplayer2.start();
			}
		});
		
		stop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(mpplayer.isPlaying()){	
					
				mpplayer.stop();
				mpplayer.reset();
				}
				
				if(mpplayer2.isPlaying()){	
					mpplayer2.stop();
					mpplayer2.reset();
					}
			
			}
		});
		
	syncerme.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				if(mpplayer.isPlaying()){
					mpplayer.stop();
					mpplayer.reset();
					mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
					mpplayer.start();
					} 
				
				if(mpplayer2.isPlaying()){
					mpplayer2.stop();
					mpplayer2.reset();
					mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep5);
					mpplayer2.start();
					} 
				
				
				
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Open in new window

0
Comment
Question by:ForLoop5
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39880892
I could be blind, stupid or both, but I can't spot any "send" command in your code.
0
 
LVL 1

Author Comment

by:ForLoop5
ID: 39880900
I apolagize for saying "send"  in my question.  I was referring to the if statement.  It wass supposed to say "second if statement"  and not "send if statement"
0
 
LVL 1

Author Comment

by:ForLoop5
ID: 39880938
I started over and I am having better luck this time. Do you have any idea why?  Here is my new code.

package com.example.ifstatementtester;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	MediaPlayer mpplayer;
	MediaPlayer mpplayer2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button syncer = (Button) findViewById(R.id.syncerme);
		Button stopme = (Button) findViewById(R.id.stopper);
		Button playme = (Button) findViewById(R.id.player5);
		
	playme.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
		mpplayer.setLooping(true);
		mpplayer.start();
		
		mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep5);
		mpplayer2.setLooping(true);
		mpplayer2.start();
			}
	});
		
		Context context = getApplicationContext();
		CharSequence text = "MpAudio is playing";
		CharSequence text2 = "MpAudio2 is playing";
		int duration = 5000;
		final Toast toast = Toast.makeText(context, text, duration);
		final Toast toast2 = Toast.makeText(context, text2, duration);
		
		
		syncer.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mpplayer.isPlaying()){	
					toast.show();
					mpplayer.stop();
					mpplayer.release();
					mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
					mpplayer.start();
					}
					if(mpplayer2.isPlaying()){	
					toast2.show();
					mpplayer2.stop();
					mpplayer2.release();
					mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep5);
					mpplayer2.start();
					}
				
			}
		});
		
		stopme.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mpplayer.isPlaying()){	
					toast.show();
					mpplayer.stop();
					mpplayer.release();
					mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
					
					}
					if(mpplayer2.isPlaying()){	
					toast2.show();
					mpplayer2.stop();
					mpplayer2.release();
					mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep5);
				
					}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:krakatoa
ID: 39880952
I don't do Android, so without trawling over your code, no, in short, I don't. But from what I've done loosely with this sort of thing in Java, it could be the order in which you've called the commands, and possibly the effect on the pre-roll? But that's pure guesswork right now.
0
 
LVL 1

Assisted Solution

by:ForLoop5
ForLoop5 earned 0 total points
ID: 39880994
After some time I discoveed that I was declaring some of my variables in the wrong function. i moved       mpplayer = MediaPlayer.create(MainActivity.this, R.raw.beep5);
            mpplayer2 = MediaPlayer.create(MainActivity.this, R.raw.beep5);   to its parent function and seem to be working now.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 39881001
ok. good it worked.
0
 
LVL 1

Author Closing Comment

by:ForLoop5
ID: 39894358
I declared the variables in the wrong function and did not have access to them from other functions.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now