Android:媒体播放器从网址流式传输mp3时无法准备

时间:2016-06-26 07:07:27

标签: android url crash media-player

在网上搜索了几个小时后,我仍然找不到在android中传输mp3文件的方法。 这是我简单的java代码:

DD:MM:YYYY:11:59:00 (PM)

我在清单

中添加了权限
package com.khagendra.livestreamaudio;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;


public class BlurPlayer extends AppCompatActivity {
    Button buttonStop,buttonStart ;

    String AudioURL = "http://www.android-examples.com/wp-content/uploads/2016/04/Thunder-rumble.mp3";

    MediaPlayer mediaplayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_blur_player);

        buttonStart = (Button)findViewById(R.id.button1);
        buttonStop = (Button)findViewById(R.id.button2);

        mediaplayer = new MediaPlayer();
        mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"start",Toast.LENGTH_SHORT).show();
                try {

                    mediaplayer.setDataSource(AudioURL);
                    mediaplayer.prepare();


                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                 mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                     @Override
                     public void onPrepared(MediaPlayer mp) {
                         Toast.makeText(getApplicationContext(),"prepared",Toast.LENGTH_SHORT).show();

                         mediaplayer.start();
                     }
                 });

            }
        });

        buttonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"stop",Toast.LENGTH_SHORT).show();
                mediaplayer.stop();
            }
        });
    }
}

我在布局中只有两个按钮,其中包含id button1和button2。 起初我使用<uses-permission android:name="android.permission.INTERNET"/> 并且logcat输出是

mediaplayer.prepare();

但后来我读到06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve interface method 15147: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve interface method 15151: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 06-26 12:03:57.680 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve virtual method 446: Landroid/content/res/TypedArray;.getType (I)I 06-26 12:03:57.760 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:03:57.821 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 06-26 12:03:57.821 3249-3249/com.khagendra.livestreamaudio 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 12:03:57.821 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:03:57.821 3249-3249/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 06-26 12:03:57.821 3249-3249/com.khagendra.livestreamaudio 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 12:03:57.821 3249-3249/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:03:57.831 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer-JNI﹕ native_setup 06-26 12:03:57.831 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ constructor 06-26 12:03:57.841 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ setListener 06-26 12:03:57.841 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer-JNI﹕ setAudioStreamType: 3 06-26 12:03:57.841 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ MediaPlayer::setAudioStreamType 06-26 12:03:57.941 3249-3249/com.khagendra.livestreamaudio D/libEGL﹕ loaded /vendor/lib/egl/libGLES_vc4.so 06-26 12:03:57.941 3249-3249/com.khagendra.livestreamaudio D/﹕ GPU monitor inactive - could not find libgpumonitor.so [Symbol not found: ] or not enabled (debug.egl.hw.gpumonitor = 0) 06-26 12:03:57.941 3249-3249/com.khagendra.livestreamaudio W/khrn_client﹕ init_window num_buffers 3 min undequeued buffers 1 06-26 12:03:57.941 3249-3249/com.khagendra.livestreamaudio W/khrn_client﹕ init_window window 0x4f50c158, 480x800 hintTransform 0x0 do_pre 1 06-26 12:03:57.961 3249-3249/com.khagendra.livestreamaudio D/OpenGLRenderer﹕ Enabling debug mode 0 06-26 12:04:03.626 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ setVideoSurfaceTexture 06-26 12:04:03.626 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ prepare 06-26 12:04:03.696 3249-3260/com.khagendra.livestreamaudio V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004 06-26 12:04:03.696 3249-3260/com.khagendra.livestreamaudio E/MediaPlayer﹕ error (1, -1004) 06-26 12:04:03.696 3249-3260/com.khagendra.livestreamaudio V/MediaPlayer﹕ signal application thread 06-26 12:04:03.696 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ prepare complete - status=1 06-26 12:04:03.706 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ java.io.IOException: Prepare failed.: status=0x1 06-26 12:04:03.716 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.media.MediaPlayer.prepare(Native Method) 06-26 12:04:03.716 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.khagendra.livestreamaudio.BlurPlayer$1.onClick(BlurPlayer.java:44) 06-26 12:04:03.716 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.view.View.performClick(View.java:4432) 06-26 12:04:03.716 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.view.View$PerformClick.run(View.java:18338) 06-26 12:04:03.716 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Looper.loop(Looper.java:137) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5283) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 06-26 12:04:03.726 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 06-26 12:04:03.736 3249-3249/com.khagendra.livestreamaudio W/Choreographer﹕ Already have a pending vsync event. There should only be one at a time. 06-26 12:04:03.746 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ setVideoSurfaceTexture 06-26 12:04:03.746 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ prepare 06-26 12:04:03.856 3249-3261/com.khagendra.livestreamaudio V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004 06-26 12:04:03.856 3249-3261/com.khagendra.livestreamaudio E/MediaPlayer﹕ error (1, -1004) 06-26 12:04:03.856 3249-3261/com.khagendra.livestreamaudio V/MediaPlayer﹕ signal application thread 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio V/MediaPlayer﹕ prepare complete - status=1 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ java.io.IOException: Prepare failed.: status=0x1 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.media.MediaPlayer.prepare(Native Method) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.khagendra.livestreamaudio.BlurPlayer$1.onClick(BlurPlayer.java:44) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.view.View.performClick(View.java:4432) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.view.View$PerformClick.run(View.java:18338) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.os.Looper.loop(Looper.java:137) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5283) 06-26 12:04:03.856 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 06-26 12:04:03.866 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 06-26 12:04:03.866 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 06-26 12:04:03.866 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 06-26 12:04:03.866 3249-3249/com.khagendra.livestreamaudio W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 06-26 12:04:03.906 3249-3249/com.khagendra.livestreamaudio I/brcm-gr﹕ [gralloc_lock]: new usage 0x933 06-26 12:04:05.628 3249-3249/com.khagendra.livestreamaudio I/brcm-gr﹕ [gralloc_lock]: new usage 0x933 但没有用。这次的logcat输出是:

mediaplayer.prepareAsync();
  1. 我在不同的Android手机上测试了这个代码。(三星和小米)
  2. 链接:The mp3 url正在运作。
  3. 当我在sdcard中测试此代码中的mp3文件(具有权限)时,此代码可以正常工作。
  4. 我也试过06-26 12:04:52.634 3249-3249/com.khagendra.livestreamaudio W/egl_server﹕ Allocating storage 1x1 surface type 0x1 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve interface method 15147: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve interface method 15151: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 06-26 12:05:13.785 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 06-26 12:05:14.455 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 06-26 12:05:14.455 3645-3645/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 06-26 12:05:14.455 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:05:14.465 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 06-26 12:05:14.465 3645-3645/com.khagendra.livestreamaudio W/dalvikvm﹕ VFY: unable to resolve virtual method 446: Landroid/content/res/TypedArray;.getType (I)I 06-26 12:05:14.465 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:05:15.056 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 06-26 12:05:15.056 3645-3645/com.khagendra.livestreamaudio 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 12:05:15.056 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:05:15.056 3645-3645/com.khagendra.livestreamaudio I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 06-26 12:05:15.056 3645-3645/com.khagendra.livestreamaudio 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 12:05:15.056 3645-3645/com.khagendra.livestreamaudio D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 06-26 12:05:15.086 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer-JNI﹕ native_setup 06-26 12:05:15.086 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer﹕ constructor 06-26 12:05:15.086 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer﹕ setListener 06-26 12:05:15.086 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer-JNI﹕ setAudioStreamType: 3 06-26 12:05:15.086 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer﹕ MediaPlayer::setAudioStreamType 06-26 12:05:15.536 3645-3645/com.khagendra.livestreamaudio D/libEGL﹕ loaded /vendor/lib/egl/libGLES_vc4.so 06-26 12:05:15.546 3645-3645/com.khagendra.livestreamaudio D/﹕ GPU monitor inactive - could not find libgpumonitor.so [Symbol not found: ] or not enabled (debug.egl.hw.gpumonitor = 0) 06-26 12:05:15.546 3645-3645/com.khagendra.livestreamaudio W/khrn_client﹕ init_window num_buffers 3 min undequeued buffers 1 06-26 12:05:15.546 3645-3645/com.khagendra.livestreamaudio W/khrn_client﹕ init_window window 0x4f50c158, 480x800 hintTransform 0x0 do_pre 1 06-26 12:05:15.576 3645-3645/com.khagendra.livestreamaudio D/OpenGLRenderer﹕ Enabling debug mode 0 06-26 12:05:20.892 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer﹕ setVideoSurfaceTexture 06-26 12:05:20.892 3645-3645/com.khagendra.livestreamaudio V/MediaPlayer﹕ prepareAsync 06-26 12:05:20.982 3645-3657/com.khagendra.livestreamaudio V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004 06-26 12:05:20.982 3645-3657/com.khagendra.livestreamaudio E/MediaPlayer﹕ error (1, -1004) 06-26 12:05:20.982 3645-3657/com.khagendra.livestreamaudio V/MediaPlayer﹕ callback application 06-26 12:05:20.982 3645-3657/com.khagendra.livestreamaudio V/MediaPlayer﹕ back from callback 06-26 12:05:21.482 3645-3645/com.khagendra.livestreamaudio E/MediaPlayer﹕ Error (1,-1004) 06-26 12:05:21.502 3645-3645/com.khagendra.livestreamaudio I/brcm-gr﹕ [gralloc_lock]: new usage 0x933
  5. 我也尝试将链接更改为another link
  6. 我已经失去了使这段代码运行良好的希望。 任何帮助都将受到高度赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

我使用这个媒体播放器类及其作品。

        `
public class MediaService extends Service implements  MediaPlayer.OnPreparedListener

         //init mediaplayer
         private MediaPlayer mMediaPlayer = new MediaPlayer();

         // and later in the class
         public void playSong(String songPath){
             mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(songPath)); 
             mMediaPlayer.prepareAsync();
         }

         public void onPrepared(MediaPlayer player){
            mMediaPlayer.start();
         }`

其中songPath是来自本地设备内存或互联网网址的歌曲uri。它适用于这两种情况。 还可以在某些浏览器中尝试使用url来检查实际上是否有效并播放音频。