Solved

Using the mediaplayer class and java

Posted on 2014-02-23
7
258 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 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

25 Experts available now in Live!

Get 1:1 Help Now