0

我想在 android 中播放一个 mp3 文件,但我得到了这个错误:

java.lang.NullPointerException at HelloAndroid.playMusic

我的代码如下

package com.bestvalue.hello;
/*import android.util.Log;*/
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.media.MediaPlayer;
import android.net.Uri;

public class HelloAndroid extends Activity {

    public static final String DebugTag = "LogInfo";
    public MediaPlayer mp;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);        
        tv.setText("keke napep");
        playMusic();
        setContentView(tv);        
        Log.i(DebugTag, "Info about my app na");                
    }


    public void playMusic () {
        try {
            Uri fileName = Uri.parse("http://www.perlgurl.org/podcast/archives/podcasts/PerlgurlPromo.mp3");
            mp= MediaPlayer.create(this, fileName);
            mp.start();
        } catch (Exception e){
            Log.e(DebugTag, "Error Playing File", e);
        }
    }

    @Override
    protected void onStop() {
        if (mp != null) {
            mp.stop();
            mp.release();
        }
        super.onStop();
    }
}

我将如何解决此错误?

谢谢

更新

package com.bestvalue.hello;
/*import android.util.Log;*/
import android.app.Activity;
//import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.media.AudioManager;
import android.media.MediaPlayer;


public class HelloAndroid extends Activity {

    public static final String DebugTag = "LogInfo";
    private MediaPlayer mp = new MediaPlayer();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);        
        tv.setText("keke napep");
        playMusic();
        setContentView(tv);                           
    }

    public void playMusic () {
        try {
            String url = "http://www.perlgurl.org/podcast/archives/podcasts/PerlgurlPromo.mp3";
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setDataSource(url);
            mp.prepare(); 
            mp.start();         
        } catch (Exception e){
            Log.e(DebugTag, "Error Playing File", e);
        }
    }

    @Override
    protected void onStop() {
        if (mp != null) {
            mp.stop();
            mp.release();
        }
        super.onStop();
    }
}

实施一些答案后,我现在收到此错误:java.io.IOException: Prepare Failed .: status = 0x1

4

2 回答 2

1

mp.prepare();在 mp.start() 之前添加;

于 2011-08-02T08:50:04.647 回答
0

您的问题是您mp在调用方法之前没有进行检查。根据文档,如果MediaPlayer.create失败,它将返回null

s的经验法则NullPointerException:回去检查你的回报。null当您不期望它返回时,通常会返回某些东西,并且您尝试对其调用方法。在这种情况下,您不会在 上调用任何东西fileName,所以这是安全的。你在打电话mp,所以这可能就是你在做什么。

[编辑]

为什么没有播放音频:

您没有调用setAudioStreamType(AudioManager.STREAM_MUSIC);,并且在创建MediaPlayer实例时使用 URI 仅适用于本地存储的媒体。网络媒体需要通过调用来设置其位置setDataSource(context, uri);

(非常好的)文档中

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();`
于 2011-08-02T08:32:38.227 回答