【问题标题】:SoundPool won't play a passed raw from an other ActivitySoundPool 不会播放来自其他 Activity 的传递的原始数据
【发布时间】:2017-06-06 09:04:19
【问题描述】:

我有这个问题,我想从 Activity 1 传递一个 raw 在 Activity 2 中播放

这是我的代码

活动一

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

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

            }
        });

    }
}

当我尝试打开应用程序时,我得到“不幸的是,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) 

有什么想法吗?

【问题讨论】:

  • 我们不能像那样访问资源文件
  • 你的android studio里面的音频文件在哪里??
  • @AvinashRoy 是 \app\src\main\res\raw\waterdrop.wav
  • @AvinashRoy 并且确实使用了编辑后的代码......我得到无法解析符号'waterdrop'?

标签: java android


【解决方案1】:

由于您没有正确打开原始类型资源,因此您获取资源未找到异常

尝试从以下资源中获取原始音频文件:

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

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

【讨论】:

  • 你的代码中的ThePlayedSound在这段代码中被替换为soundId
  • 完成我确实使用了它,应用程序再次出现此异常:android.content.res.Resources$NotFoundException: Resource ID #0x0
  • 去构建和清理项目并再次运行它
  • 我尝试了没有 getApplicationContext() 的编辑代码.. 将无法工作..相同的异常
  • 是的......地球上不可能发生这种情况!!!!同样的例外,我用干净的构建尝试了所有编辑过的代码 ='(
【解决方案2】:

如何使用 MediaPlayer 类?

检查以下代码:

MainActivity 类(或您想从其开始新活动的任何类)

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

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

这段代码对我有用。

如果它说那个特定的构造函数不起作用,试试下面的:

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

希望对你有所帮助!

【讨论】:

  • 嗨,感谢您对第一个活动的重播,我是否使用“R.raw.waterdrop”或 soundId 作为 int 来传递它?..cuz 在这里我什么也没得到?跨度>
  • R.raw.waterdrop 指向资源 ID,在本例中为整数。您可以选择执行“int resId = R.raw.waterdrop”并将其推送到意图中,或者您可以直接在意图中传递它并在下一个活动中检索它。
  • 对不起,我迟到了..但它没有用..但好消息我想出了一个办法......我会发布......非常感谢你的时间 =)
猜你喜欢
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多