2011-08-02 62 views
0

我要玩android中的MP3文件,但我得到這個錯誤:錯誤的播放文件:NullPointerException異常Android中

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(); 
    } 
} 

將我做些什麼來解決這個錯誤?

感謝

UPDATE

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

回答

0

你的問題是你不檢查mp調用方法之前,在上面。根據the documentation,如果MediaPlayer.create失敗,它將返回null

經驗法則NullPointerException s:回去檢查您的退貨。當你不期待它時,通常會返回null,並且您嘗試調用它。在這種情況下,您不打電話fileName,所以這是安全的。你呼籲mp東西,所以這可能是什麼做你的

[編輯]

爲什麼音頻是不是在玩。

你是不是叫setAudioStreamType(AudioManager.STREAM_MUSIC);和使用URI在創建MediaPlayer實例時僅適用於本地存儲的媒體。網絡媒體需要通過撥打setDataSource(context, uri);來設置其位置。

the (very good) documentation

Uri myUri = ....; // initialize Uri here 
MediaPlayer mediaPlayer = new MediaPlayer(); 
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mediaPlayer.setDataSource(getApplicationContext(), myUri); 
mediaPlayer.prepare(); 
mediaPlayer.start();` 
+0

好的,我該如何處理它以使我能夠播放文件? –

+0

由於@Ilango J注意下面,你需要調用mp.prepare();.我花了一些時間,看看在文檔中創建的流程圖,是我嗎:http://developer.android.com/reference/android/media/MediaPlayer.html – Paddy

+0

也許有一些東西我沒有得到正確的,但沒有任何更改 –

1

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

+0

我剛添加它,似乎沒有幫助 –

+0

轉介此鏈接http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/ –

相關問題