Solved

Using the mediaplayer class and java

Posted on 2014-02-23
7
259 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 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