Solved

Using the mediaplayer class and java

Posted on 2014-02-23
7
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Suggested Courses

710 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