带有音频流的java.lang.OutOfMemoryError

时间:2016-07-13 13:19:29

标签: java android streaming

我是Android和Java的初学者。我已经开发了这个简单的应用程序来传输音频,但它会意外地崩溃。 Android Studio没有出现任何错误,到目前为止我还没有能够解决这个问题。

这是我的代码:

  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jacob.wutk" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".radio" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.INTERNET"/>

</manifest> 
  

activity_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jacob.wutk.radio"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_gravity="center"
        android:src="@drawable/background"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:paddingLeft="4.0dip"
        android:paddingTop="5.0dip"
        android:paddingRight="4.0dip"
        android:paddingBottom="1.0dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
       <ImageButton
           android:id="@+id/play"
           android:layout_width="0.0dip"
           android:layout_height="wrap_content"
           android:src="@drawable/play"
           android:background="?android:selectableItemBackground"
           android:layout_weight="1.0"
           android:scaleType="fitCenter"
           android:clickable="true"
           android:onClick="playMusic"/>
       <ImageButton
           android:id="@+id/pause"
           android:layout_width="0.0dip"
           android:layout_height="fill_parent"
           android:src="@drawable/pause"
           android:background="?android:selectableItemBackground"
           android:layout_weight="1.0"
           android:scaleType="fitCenter"
           android:clickable="true"
           android:onClick="pauseMusic"/>
    </LinearLayout>
</FrameLayout>
  

radio.java

public class radio extends AppCompatActivity {

    /** Called when the user touches the button */
    public void playMusic(View view) throws IOException {
        String url = "http://streamer.cci.utk.edu:8000/wutk-vorbis"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
        mediaPlayer.start();
    }

    public void pauseMusic(MediaPlayer mediaPlayer) {
        mediaPlayer.pause();
    }

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

崩溃日志。

--------- beginning of crash
07-13 09:34:35.175 2764-2764/com.example.jacob.wutk E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.jacob.wutk, PID: 2764
                                                                      java.lang.OutOfMemoryError: Failed to allocate a 579537516 byte allocation with 1048576 free bytes and 382MB until OOM
                                                                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                          at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                          at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                          at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                          at android.widget.ImageView.<init>(ImageView.java:152)
                                                                          at android.widget.ImageView.<init>(ImageView.java:140)
                                                                          at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                          at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                          at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
                                                                          at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                          at com.example.jacob.wutk.radio.onCreate(radio.java:30)
                                                                          at android.app.Activity.performCreate(Activity.java:6237)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-13 09:34:42.066 2764-2764/com.example.jacob.wutk I/Process: Sending signal. PID: 2764 SIG: 9

1 个答案:

答案 0 :(得分:1)

OutOfMemoryError异常的原因是因为您在xml布局'activity_radio.xml'中设置了一个大位图作为ImageView的源。当您的Activity / Fragment中的布局被夸大时,ImageView会尝试在内存中加载完整大小的'src'图像,这可能看起来不是很大,但系统可能没有适当的内存来容纳它。

要解决此问题的目的是删除xml代码内部,设置图像,然后按比例缩小/向上加载活动/片段内部的图像以适合您的设备配置

以下是Android开发者网站上一篇文章的link,该文章讨论了正确加载的缩放位图。请记住,您希望在后台线程中处理加载和缩放位图以不阻止您的UI线程,否则您将获得另一个异常。