2017-06-06 66 views
3

我有這個問題,我想通過從活動一原在活動2的Soundpool不會從其他活動中發揮過生

這裏播放的是我的代碼

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

活動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)  

任何想法?

+0

我們無法訪問這樣的資源文件 –

+0

其中是位於您的android工作室內的音頻文件? –

+0

@AvinashRoy yes \ app \ src \ main \ res \ raw \ waterdrop.wav – Sara

回答

1

烏爾獲得資源沒有發現,因爲你不打開原始類型資源的例外正常

嘗試獲得來自資源的原始音頻文件是這樣的:關於使用MediaPlayer的類

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

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

該代碼中的ThePlayedSound被代碼中的soundId替換 –

+0

我做了使用它和應用程序再次兌現這個異常:android.content.res.Resources $ NotFoundException:資源ID#0x0 – Sara

+0

去建立和清理項目,並再次運行它 –

0

什麼?

檢查下面這段代碼:

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

希望它可以幫助你!

+0

嗨,感謝您爲第一次活動的重播,我使用「R.raw.waterdrop」或soundId作爲int來傳遞它?..在這裏cuz我沒有得到任何東西? – Sara

+0

R.raw.waterdrop指向一個資源ID,在這種情況下,整數。你可以選擇做「int resId = R.raw.waterdrop」並通過thr意圖推送它,或者你可以直接通過它的意圖,並在下一個活動中檢索它 – Ellisan

+0

對不起,我遲到了..但它沒有工作..但好消息,我想辦法做...我會張貼。 ..非常感謝你的時間=) – Sara