2016-06-25 100 views
0

我想讓我的android應用程序播放來自url的mp3文件,但我得到這個錯誤,我簡直不明白。它說:「無法在客戶端打開文件,嘗試在服務器端。無法創建媒體播放器」我知道該文件在那裏並可用。從url播放mp3的Android

這是我的代碼。


package com.tenn.klaszetterlund.mp3; 

import android.media.MediaPlayer; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

import java.io.IOException; 
import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

    private ArrayList<String> urlsformp3s=new ArrayList<>(); 
    private MediaPlayer firstmediaplayer; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     urlsformp3s.add("http://www.mfiles.co.uk/mp3-downloads/Dvorak-Symphony9-2-from-the-New-World.mp3"); 
     urlsformp3s.add("http://www.mfiles.co.uk/mp3-downloads/saint-saens-carnival-of-the-animals-the-swan.mp3"); 
     Log.d("we're at", "1"); 

     firstmediaplayer = new MediaPlayer(); 

     Log.d("we're at", "2"); 

     try { 
        Log.d("we're at", "3"); 
        firstmediaplayer.setDataSource(MainActivity.this, Uri.parse(urlsformp3s.get(0))); 
        Log.d("we're at", "4"); 
        firstmediaplayer.prepare(); 
        Log.d("we're at", "5"); 
        firstmediaplayer.start(); 
        Log.d("we're at", "6"); 
       } catch (IOException e) { 
        Log.d("Oh no!", "error"); 
        e.printStackTrace(); 
       } 
    } 
} 

和日誌說


> 06-26 00:59:18.530 2920-2920/? D/dalvikvm: Late-enabling CheckJNI 
> 06-26 00:59:18.840 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: 
> VFY: unable to find class referenced in signature 
> (Landroid/view/SearchEvent;) 06-26 00:59:18.840 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.view.Window$Callback.onSearchRequested, referenced from 
> method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
> 06-26 00:59:18.840 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: 
> VFY: unable to resolve interface method 15144: 
> Landroid/view/Window$Callback;.onSearchRequested 
> (Landroid/view/SearchEvent;)Z 06-26 00:59:18.840 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: VFY: replacing 
> opcode 0x72 at 0x0002 06-26 00:59:18.840 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.view.Window$Callback.onWindowStartingActionMode, 
> referenced from method 
> android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
> 06-26 00:59:18.840 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: 
> VFY: unable to resolve interface method 15148: 
> Landroid/view/Window$Callback;.onWindowStartingActionMode 
> (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 06-26 
> 00:59:18.840 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: VFY: 
> replacing opcode 0x72 at 0x0002 06-26 00:59:18.910 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, 
> referenced from method 
> android.support.v7.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged 
> 06-26 00:59:18.910 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: 
> VFY: unable to resolve virtual method 15047: 
> Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V 06-26 
> 00:59:18.910 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: VFY: 
> replacing opcode 0x6f at 0x0008 06-26 00:59:18.980 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.view.ViewGroup.onRtlPropertiesChanged, referenced from 
> method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 06-26 
> 00:59:18.980 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: VFY: 
> unable to resolve virtual method 15043: 
> Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 06-26 
> 00:59:18.980 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: VFY: 
> replacing opcode 0x6f at 0x0007 06-26 00:59:18.990 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.content.res.TypedArray.getChangingConfigurations, 
> referenced from method 
> android.support.v7.widget.TintTypedArray.getChangingConfigurations 
> 06-26 00:59:18.990 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: 
> VFY: unable to resolve virtual method 424: 
> Landroid/content/res/TypedArray;.getChangingConfigurations()I 06-26 
> 00:59:18.990 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: VFY: 
> replacing opcode 0x6e at 0x0002 06-26 00:59:18.990 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.content.res.TypedArray.getType, referenced from method 
> android.support.v7.widget.TintTypedArray.getType 06-26 00:59:18.990 
> 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: VFY: unable to 
> resolve virtual method 446: Landroid/content/res/TypedArray;.getType 
> (I)I 06-26 00:59:18.990 2920-2920/com.tenn.klaszetterlund.mp3 
> D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 06-26 00:59:19.020 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.content.res.Resources.getDrawable, referenced from 
> method android.support.v7.widget.ResourcesWrapper.getDrawable 06-26 
> 00:59:19.020 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: VFY: 
> unable to resolve virtual method 387: 
> Landroid/content/res/Resources;.getDrawable 
> (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
> 06-26 00:59:19.020 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: 
> VFY: replacing opcode 0x6e at 0x0002 06-26 00:59:19.020 
> 2920-2920/com.tenn.klaszetterlund.mp3 I/dalvikvm: Could not find 
> method android.content.res.Resources.getDrawableForDensity, referenced 
> from method 
> android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 06-26 
> 00:59:19.020 2920-2920/com.tenn.klaszetterlund.mp3 W/dalvikvm: VFY: 
> unable to resolve virtual method 389: 
> Landroid/content/res/Resources;.getDrawableForDensity 
> (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
> 06-26 00:59:19.020 2920-2920/com.tenn.klaszetterlund.mp3 D/dalvikvm: 
> VFY: replacing opcode 0x6e at 0x0002 06-26 00:59:19.030 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/we're at: 1 06-26 00:59:19.040 
> 2920-2923/com.tenn.klaszetterlund.mp3 D/dalvikvm: GC_CONCURRENT freed 
> 233K, 5% free 6837K/7175K, paused 2ms+2ms 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/we're at: 2 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/we're at: 3 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/MediaPlayer: Couldn't open 
> file on client side, trying server side 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 E/MediaPlayer: Unable to to 
> create media player 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/Oh no!: error 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> java.io.IOException: setDataSource failed.: status=0x80000000 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at android.media.MediaPlayer._setDataSource(Native Method) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at android.media.MediaPlayer.setDataSource(MediaPlayer.java:844) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at android.media.MediaPlayer.setDataSource(MediaPlayer.java:806) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at android.media.MediaPlayer.setDataSource(MediaPlayer.java:761) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at 
> com.tenn.klaszetterlund.mp3.MainActivity.onCreate(MainActivity.java:34) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at android.app.Activity.performCreate(Activity.java:4466) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at 
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at 
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at 
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at android.app.ActivityThread.access$600(ActivityThread.java:127) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at 
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at android.os.Handler.dispatchMessage(Handler.java:99) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at android.os.Looper.loop(Looper.java:137) 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  at 
> android.app.ActivityThread.main(ActivityThread.java:4447) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at java.lang.reflect.Method.invokeNative(Native Method) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at java.lang.reflect.Method.invoke(Method.java:511) 06-26 00:59:19.060 
> 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
> 06-26 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err: 
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-26 
> 00:59:19.060 2920-2920/com.tenn.klaszetterlund.mp3 W/System.err:  
> at dalvik.system.NativeStart.main(Native Method) 06-26 00:59:19.110 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/libEGL: loaded 
> /system/lib/egl/libEGL_tegra.so 06-26 00:59:19.160 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/libEGL: loaded 
> /system/lib/egl/libGLESv1_CM_tegra.so 06-26 00:59:19.180 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/libEGL: loaded 
> /system/lib/egl/libGLESv2_tegra.so 06-26 00:59:19.200 
> 2920-2920/com.tenn.klaszetterlund.mp3 D/OpenGLRenderer: Enabling debug 
> mode 0 

請幫助!

回答

1

我是一個dumbkopf。問題在於互聯網權限未添加到AndroidManifest.xml中 只需加上

<uses-permission android:name="android.permission.INTERNET"/>