如何在ANDROID EMULATOR中播放Mp3文件

时间:2016-04-17 14:42:32

标签: android android-studio mp3 media-player genymotion

我正在尝试在我的genymotion模拟器中播放mp3文件,并且我将我的歌曲存储在/mnt/shared/New_folder(2)中。但是我收到了一些像这样的错误

 10:22:14.023 2102-2102/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.union.mp1, PID:     
                                             java.lang.RuntimeException:    Unable to instantiate activity ComponentInfo{com.union.mp1/com.union.mp1.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                 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:903)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                              Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                 at android.app.Activity.getSystemService(Activity.java:5035)
                                                 at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
                                                 at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
                                                 at com.union.mp1.MainActivity.<init>(MainActivity.java:46)
                                                 at java.lang.reflect.Constructor.newInstance(Native Method)
                                                 at java.lang.Class.newInstance(Class.java:1606)
                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:135) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 

我的java代码是

 import android.app.ListActivity;
 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.View;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.ArrayAdapter;

 import java.io.File;
 import java.io.FilenameFilter;
 import java.util.ArrayList;
 import java.util.List;

 public class MainActivity extends ListActivity {
public static  final  String SD_PATH =new String("/New_folder_(2)/");
private List<String>songs =new ArrayList<String>();
private MediaPlayer mp =new MediaPlayer();
private void updatePlayList(){
    File home =new File(SD_PATH);
    if (home.listFiles(new Mp3Filter()).length>0){

        for (File file:home.listFiles(new Mp3Filter())){
            songs.add(file.getName());
        }
    }
    ArrayAdapter<String>songlist =new ArrayAdapter<String>(this,R.layout.song_item,songs);
    {
        setListAdapter(songlist);
    }
    }

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



}
class Mp3Filter implements FilenameFilter{

@Override
public boolean accept(File dir, String filename) {
    return (getLocalClassName().endsWith(".mp3"));
}
 }
  }

我的xml代码 这是在任何布局下

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@id/android:list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingBottom="60dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

通过这样做我的应用程序没有运行。我已经在列表视图中显示了我的歌曲。那么我将在列表视图中显示我的歌曲。 HERE&#34; song_item&#34;是要在列表视图中显示的文本视图文件

2 个答案:

答案 0 :(得分:0)

您是否尝试将updatePlayList()放在onCreate()之后?

我认为这可能是一个解决方案。希望它有所帮助;)

答案 1 :(得分:0)

这是因为您正在尝试内联实例化MediaPlayer成员变量。只需将实例化移动到onCreate()。

而不是:

private MediaPlayer mp =new MediaPlayer();

有:

private MediaPlayer mp;

然后在onCreate中:

mp = new MediaPlayer();