Go Back   Android Forums > Android Discussion > Android Applications
Android Applications All the information you could ever want about Android Applications. Learn about apps and get help with them... all here! New apps can be found and announced in the Applications Announcements forum linked below.

New Forums: Nexus Player | Nexus 9
test: Reply
 
LinkBack Thread Tools
Old April 18th, 2011, 10:27 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2011
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Need help in coding

I've just started learning to build android apps a week ago and now I'm playing with MediaPlayer class.

I'm building an app that will play a sound once the user clicks the button

here's my code :
Quote:
package com.jason.shootemup;

import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class game extends Activity implements OnClickListener {

View shoot, reload;
int bullet = 5;
public static final Random r = new Random();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gun);

shoot = findViewById (R.id.shoot);
reload = findViewById (R.id.reload);
reload.setVisibility(2);
shoot.setOnClickListener (this);
shoot.setOnClickListener (this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shoot :
shoot_sequence ();
break;
case R.id.reload :
Toast.makeText(this, "Reload !!", 10);
reload_sequence();
break;
}
// TODO Auto-generated method stub

}

private void shoot_sequence() {
if (bullet > 0) {
play_shoot_sound ();
Toast.makeText(this, bullet+" left !!", 10);
bullet = bullet - 1;
}
else {
reload.setVisibility(0);
shoot.setVisibility(2);
}
// TODO Auto-generated method stub
}

private void play_shoot_sound() {
MediaPlayer mp = MediaPlayer.create(this, R.raw.shoot);
mp.start();
mp.stop();
mp.release();
// TODO Auto-generated method stub
}

private void reload_sequence() {
play_reload_sound();
bullet = r.nextInt(4) + 1;
reload.setVisibility(2);
shoot.setVisibility(0);
// TODO Auto-generated method stub
}

private void play_reload_sound() {
MediaPlayer mp = MediaPlayer.create(this, R.raw.reload);
mp.start();
mp.stop();
mp.release();
// TODO Auto-generated method stub
}
}

It was compiled successfully and worked on my emulator. but when I installed it on my motorola droid, the sound won't come out. I've set the volume to max, but nothing happens. I think it's probably because of the source code

I'll be very grateful if you can point out my mistakes in my source code

THX for the help...^^

Advertisements
namdaemun is offline  
Reply With Quote
sponsored links
Reply
Tags
coding, mediaplayer, problem


Go Back   Android Forums > Android Discussion > Android Applications
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:42 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.