简体   繁体   English

SoundPool不会播放其他活动传递的原始内容

[英]SoundPool won't play a passed raw from an other Activity

I have this problem I want to pass a raw from Activity 1 to be played in Activity 2 我有这个问题,我想从活动1中传递一个原始文件,以便在活动2中播放

Here is my code 这是我的代码

Activity 1 活动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 活动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: 当我尝试打开应用程序时,我得到“不幸的是,My_App已经停止”应用程序崩溃, 这是新的崩溃日志后建议:

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 ? 任何想法 ?

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);

What about using the MediaPlayer class? 使用MediaPlayer类怎么样?

Check this code below: 请检查以下代码:

MainActivity class (or whatever class you want to start the new activity from) MainActivity类(或任何您想要从中开始新活动的类)

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

Main2Activity (class that will play the audio) Main2Activity(将播放音频的类)

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! 希望它可以帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM