Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2013, 10:29 AM   PM User | #1
Spudster
New Coder

 
Join Date: Jul 2012
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
Spudster is an unknown quantity at this point
Java Not Playing Songs Within A function?

I am fairly new to java Ive only started yesterday I do have programming background with PHP! C++,HTML,CSS and Java Script.

Anyway I have written this out for an android development application and its not working, I want it to play a song it does it when Its inserted up the top of the page but not in a certain bracket, More info in in //Commenting in tne code

PHP Code:
package com.mycompany.myapp2;

import android.app.Activity;
import android.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.Button;
import android.widget.TextView;
import android.media.*;
import android.media.MediaPlayer;

public class 
MainActivity extends Activity 
    
//** called when the activity is first created 
    
int counter;
    
Button add,sub;
    
TextView display;
    


    @
Override


    
public void onCreate(Bundle savedInstanceState


    { 
        
super.onCreate (savedInstanceState);

        
setContentView(R.layout.main); 
        
counter 0;
        
add = (ButtonfindViewById(R.id.bAdd);
        
sub = (ButtonfindViewById(R.id.bSub); 
        
display = (TextViewfindViewById(R.id.tvDisplay);
       
it plays here?
    
MediaPlayer mp3 MediaPlayer.create(thisR.raw.son);
    
mp3.start();
    
        
add.setOnClickListener(new View.OnClickListener() {

                public 
void onClick(View v) {
                    
                    
//ERROR BELLOW Wont excute here 
                    
MediaPlayer mp3 MediaPlayer.create(thisR.raw.son); 
                    
                    
// Make the button do something 
                        
                    
                        
                    
counter ++;
                    
display.setText(" " counter); 
                    
                    


                }



            });


        
sub.setOnClickListener(new View.OnClickListener() {

                public 
void onClick(View v) {

                    
// Take data away
                    
counter --;
                    
display.setText(" " counter);


                }



            });





    }





Spudster is offline   Reply With Quote
Old 01-22-2013, 03:07 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I haven't a clue about android development, but these do stand out here:
Code:
        display = (TextView) findViewById(R.id.tvDisplay);
       it plays here?
    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.son);
    mp3.start();

// and here:
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.son);
'this' has changed its scope. The first one's 'this' refers to the instance of Activity. The second's 'this' refers to the instance created by new View.OnClickListener. Perhaps that will be your issue?
If so, you can apply the outer's 'this' by creating a final variable and referring to it in the inner class:
PHP Code:
final Activity thisActivity this;
//...
                    
MediaPlayer mp3 MediaPlayer.create(thisActivityR.raw.son); 
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:01 PM.


Advertisement
Log in to turn off these ads.