在Android上的流媒体上出错

时间:2013-04-25 16:15:56

标签: android

我在互联网上有播放广播流的代码,这段代码如下: -

package com.example.kam;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.media.AudioManager;
import android.media.MediaPlayer;


public class MainActivity extends Activity {
    public MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    protected void onResume (){
        super.onResume();

        mp = new MediaPlayer();
        try {
            mp.setDataSource("http://radio.arabhosters.com:8015/");
        } 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();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //also consider mp.prepareAsync().
        mp.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

当我运行此代码时,我看到了这个例外: -

04-25 14:11:16.448: E/MediaPlayer(533): Unable to to create media player
04-25 14:11:16.458: W/System.err(533): java.io.IOException: setDataSource failed.: status=0x80000000
04-25 14:11:16.458: W/System.err(533):  at android.media.MediaPlayer.setDataSource(Native Method)
04-25 14:11:16.458: W/System.err(533):  at com.example.kam.MainActivity.onResume(MainActivity.java:26)
04-25 14:11:16.468: W/System.err(533):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
04-25 14:11:16.468: W/System.err(533):  at android.app.Activity.performResume(Activity.java:4539)
04-25 14:11:16.468: W/System.err(533):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2433)
04-25 14:11:16.468: W/System.err(533):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2471)
04-25 14:11:16.468: W/System.err(533):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
04-25 14:11:16.478: W/System.err(533):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-25 14:11:16.478: W/System.err(533):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-25 14:11:16.478: W/System.err(533):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 14:11:16.478: W/System.err(533):  at android.os.Looper.loop(Looper.java:137)
04-25 14:11:16.478: W/System.err(533):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-25 14:11:16.478: W/System.err(533):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 14:11:16.489: W/System.err(533):  at java.lang.reflect.Method.invoke(Method.java:511)
04-25 14:11:16.489: W/System.err(533):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-25 14:11:16.489: W/System.err(533):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-25 14:11:16.489: W/System.err(533):  at dalvik.system.NativeStart.main(Native Method)
04-25 14:11:16.489: E/MediaPlayer(533): prepareAsync called in state 1
04-25 14:11:16.489: W/System.err(533): java.lang.IllegalStateException
04-25 14:11:16.499: W/System.err(533):  at android.media.MediaPlayer.prepare(Native Method)
04-25 14:11:16.499: W/System.err(533):  at com.example.kam.MainActivity.onResume(MainActivity.java:42)
04-25 14:11:16.499: W/System.err(533):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
04-25 14:11:16.499: W/System.err(533):  at android.app.Activity.performResume(Activity.java:4539)
04-25 14:11:16.499: W/System.err(533):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2433)
04-25 14:11:16.499: W/System.err(533):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2471)
04-25 14:11:16.509: W/System.err(533):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
04-25 14:11:16.509: W/System.err(533):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-25 14:11:16.509: W/System.err(533):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-25 14:11:16.509: W/System.err(533):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 14:11:16.509: W/System.err(533):  at android.os.Looper.loop(Looper.java:137)
04-25 14:11:16.509: W/System.err(533):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-25 14:11:16.509: W/System.err(533):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 14:11:16.509: W/System.err(533):  at java.lang.reflect.Method.invoke(Method.java:511)
04-25 14:11:16.519: W/System.err(533):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-25 14:11:16.519: W/System.err(533):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-25 14:11:16.519: W/System.err(533):  at dalvik.system.NativeStart.main(Native Method)
04-25 14:11:16.519: E/MediaPlayer(533): start called in state 1
04-25 14:11:16.519: E/MediaPlayer(533): error (-38, 0)
04-25 14:11:16.669: D/gralloc_goldfish(533): Emulator without GPU emulation detected.

但我将此代码导入另一台PC,它将毫无问题地播放。

我的错误成立

1 个答案:

答案 0 :(得分:1)

我的猜测是,您永远不会在release()上致电MediaPlayer。一个简单的方法(至少如果我正确地阅读logcat)来解决您的问题,那就是在实例化之前进行reset()调用。像这样:

mp.reset();
mp.setDataSource("http://radio.arabhosters.com:8015/");

但有一件事:您应该阅读整个MediaPlayer state diagram以真正理解您应该做什么。