3

I have this problem I want to pass a raw from Activity 1 to be played in Activity 2

Here is my code

Activity 1

package com.ze.zeggar.tewt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        int ThePlayedSound= R.raw.waterdrop;

        Intent MyIntent= new Intent(this, MainActivity.class);
               MyIntent.putExtra("key",ThePlayedSound);
        startActivity(MyIntent);
    }
}

Activity 2

package com.ze.zeggar.tewt;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;
    SoundPool Sound_Pool_thing;
    int Click;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent MyIntent=getIntent();
        int ThePlayedSound= MyIntent.getIntExtra("key", 0);

        Sound_Pool_thing= new SoundPool(1, AudioManager.STREAM_MUSIC,0);

        Click = Sound_Pool_thing.load(this,ThePlayedSound , 1);

        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sound_Pool_thing.play(Click,1,1,1,0,1);

            }
        });

    }
}

and when I try to open the app I got "Unfortunately , My_App has stopped" The app crashes and here's the new crash Log after suggestions:

06-06 10:49:09.069 3424-3424/com.ze.zeggar.tewt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ze.zeggar.tewt, PID: 3424
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ze.zeggar.tewt/com.ze.zeggar.tewt.MainActivity}:
 android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
 at android.content.res.Resources.getValue(Resources.java:1269)
at android.content.res.Resources.openRawResourceFd(Resources.java:1227)
at android.media.SoundPool$SoundPoolImpl.load(SoundPool.java:566)
at android.media.SoundPool.load(SoundPool.java:229)
at com.ze.zeggar.tewt.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$800(ActivityThread.java:148) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5272) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

Any idea ?

Sara
  • 31
  • 5
  • we cannot access resource file like that – Avinash Roy Jun 06 '17 at 09:17
  • where is the audio file located inside ur android studio?? – Avinash Roy Jun 06 '17 at 09:23
  • @AvinashRoy yes \app\src\main\res\raw\waterdrop.wav – Sara Jun 06 '17 at 09:25
  • @AvinashRoy And did used the edited code ... I'm getting Cannot resolve symbol 'waterdrop' ? – Sara Jun 06 '17 at 09:41
  • since ur using sound pool u need the resource id therfore do like this and moreover u need not send the id from one activity to another coz u can access the sound file in the second activity too – Avinash Roy Jun 06 '17 at 10:04
  • @AvinashRoy My idea was to select a sound from an activity to be played in an other activity ..it's like the first activity is devoted for settings and the second activity to play the sound I won't use media player , because the second activity has a button that plays the sound on clicking ..continues clicking ..this is my idea – Sara Jun 06 '17 at 10:10
  • @AvinashRoy done nooot working same exceptions again and again and again ='( – Sara Jun 06 '17 at 10:19
  • delete the audio file and again add audio file to raw type directory – Avinash Roy Jun 06 '17 at 10:21
  • Create a new directory in res folder. Make sure to create new "Android Resource Directory" and not new "Directory". – Avinash Roy Jun 06 '17 at 10:24
  • @AvinashRoy Not working ..and nothing changed with the exceptions =@ – Sara Jun 06 '17 at 10:35
  • are u using eclipse or android studio? – Avinash Roy Jun 06 '17 at 10:36
  • @AvinashRoy Android Studio – Sara Jun 06 '17 at 10:39
  • u can try closing the studio and restart it again and try once more,if it still persists,then u need to find an alternative way of saving audio files in the device internal memory and then retreiving the Uri from the internal memory and try playing the sound – Avinash Roy Jun 06 '17 at 10:51
  • @AvinashRoy yes this is it perhaps in the raw folder the audio file icon is with a interrogation marque (?)..so yeah I'll try this ..or change the code at all ..I'm depressed ! – Sara Jun 06 '17 at 11:29
  • maybe it's a compressed zip audio file – Avinash Roy Jun 06 '17 at 11:31
  • once check this link https://stackoverflow.com/questions/5777413/android-raw-folders-creation-and-reference – Avinash Roy Jun 06 '17 at 11:32

2 Answers2

1

Ur getting resource not found exception since your not opening the raw type Resources properly

Try getting the raw audio file from resources like this:

            int ThePlayedSound = this.getResources().getIdentifier("waterdrop", "raw", getPackageName());

    Intent MyIntent= new Intent(this, MainActivity.class);
           MyIntent.putExtra("key",ThePlayedSound);
    startActivity(MyIntent);
Avinash Roy
  • 953
  • 1
  • 8
  • 25
0

What about using the MediaPlayer class?

Check this code below:

MainActivity class (or whatever class you want to start the new activity from)

Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("key",R.raw.waterdrop);
startActivity(intent);

Main2Activity (class that will play the audio)

public class Main2Activity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitiy_main2);
    int soundId = getIntent().getIntExtra("key",0);

    final MediaPlayer mPlayer =new MediaPlayer();
    mPlayer.setAudioSessionId(soundId);
    mPlayer.setLooping(true);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPlayer.start();
        }
    });
}
}

This code worked for me.

If it says that that specific constructor doesnt work try the below one:

final MediaPlayer mPlayer =new MediaPlayer(this,soundId);

Hope it helps you out!

Ellisan
  • 563
  • 8
  • 22
  • Hi, thank you for your replay for the first activity do i use "R.raw.waterdrop" or soundId as an int to pass it ?..cuz here I'm getting nothing ? – Sara Jun 08 '17 at 08:09
  • R.raw.waterdrop points to a resource ID which in this case the integer. You can choose to do "int resId = R.raw.waterdrop" and push that through thr intent, or you can pass it directly in the intent and retrieve it in the next activity. – Ellisan Jun 09 '17 at 09:13
  • Sorry I'm late ..but it didn't work ..but the good news i figured a way to do ...I'll post that ... Thank you so much for your time =) – Sara Jun 11 '17 at 11:29