vulcain1972
vulcain1972

Reputation: 1

how do a Context in Android

i m french and my english is not good,so i use google to translate,sorry for mistakes I've been stuck on a problem for quite some time

here you go: I'm looking to make a small application that makes audio announcements when I click on a "button"

when I do my code in the MainActivity class, no problem

but for practical reasons, I would like to call a java class which does the audio part

in fact, it is to allow me to create a class for each scenario, but also to be able to take parts of my code more easily in my opinion, and reuse them according to my ideas

so when I create my java class that I call audio, I send to a method called vocal a table where, depending on the int sent I read this or that mp3 track; I made a raw folder where I put the desired sounds and I I put it in res of my project

I noticed that, in my vocal java class, I had to give the full access path of these audio tracks

on the other hand, eclipse (yes I use eclipse to program under Android and in addition I have an old 32-bit PC) gives me an error on the ".create":

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (voice, int)

after searching, I thought I understood that it came from the fact that I had no context in my class, the this did not refer to anything

so, I was thinking of importing the context of MainActivity, but I don't understand at all how to do it

I am an amateur and beginner and self-taught in programming, alone in front of my keyboard, which explains my many gaps and imperfections

here is my code

package com.example.scorepetforum;
 
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
 
public  class MainActivity extends Activity  {
     
    int compteur=0;
    int compteur2=0;
    static int[]memaff=new int[2];
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
         
        super.onCreate(savedInstanceState);
         
        setContentView(R.layout.activity_main);
        
         
         
    }
     
    public void audio(View view) {
        ;
memaff[0]=compteur;
memaff[1]=compteur2;
        vocale a=new vocale();
        a.causer(memaff);
 
        };
}

the xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2085e1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.petanque.MainActivity" >
 
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="audio"
        android:text="audio" />
 
</RelativeLayout>

and the java class name by me "vocale"

package com.example.scorepetforum;
 
 
import android.R;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
 
public class vocale {
     
    private MediaPlayer eux;
    private MediaPlayer p12;
    ;
     
        public void causer(int[] memaff) {
            p12 = MediaPlayer.create(this, com.example.scorepetforum.R.raw.p12);
            eux=(MediaPlayer.create(this,com.example.scorepetforum.R.raw.eux));
             switch(memaff[0]){
                
               case 1:
                   eux.start();
                   break;
            
               case 2:
                   p12.start();
                   break;
            
               
           }
             
        }

if you can tell me how to do,thanks

i try to find google solution

Upvotes: 0

Views: 34

Answers (1)

Sasi Kumar
Sasi Kumar

Reputation: 13358

change the class like this

public class vocale (Context context)

then use this context

p12 = MediaPlayer.create(context, com.example.scorepetforum.R.raw.p12);
eux=(MediaPlayer.create(context,com.example.scorepetforum.R.raw.eux));

In your MainActivity call like this

vocale a=new vocale(this);

Upvotes: 0

Related Questions