Solved

Using the mediaplayer class and java

Posted on 2014-02-23
7
260 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

791 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