簡単なライブ ストリーミング ラジオ アプリケーションを作成しました。ここでは、.pls ファイルが url からのオーディオのストリーミングを解析しています。を取得していNullPointerException
ます。
以下に私のlogcatを与えます:
02-04 04:54:54.409: E/AndroidRuntime(1603): FATAL EXCEPTION: main
02-04 04:54:54.409: E/AndroidRuntime(1603): Process: com.rad, PID: 1603
02-04 04:54:54.409: E/AndroidRuntime(1603): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rad/com.rad.MainActivity}: java.lang.NullPointerException
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.os.Looper.loop(Looper.java:136)
02-04 04:54:54.409: E/AndroidRuntime(1603): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-04 04:54:54.409: E/AndroidRuntime(1603): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 04:54:54.409: E/AndroidRuntime(1603): at java.lang.reflect.Method.invoke(Method.java:515)
02-04 04:54:54.409: E/AndroidRuntime(1603): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-04 04:54:54.409: E/AndroidRuntime(1603): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-04 04:54:54.409: E/AndroidRuntime(1603): at dalvik.system.NativeStart.main(Native Method)
02-04 04:54:54.409: E/AndroidRuntime(1603): Caused by: java.lang.NullPointerException
02-04 04:54:54.409: E/AndroidRuntime(1603): at com.rad.MainActivity.initializeMediaPlayer(MainActivity.java:133)
私のmainactivityクラスを与える:
import java.io.IOException;
import java.util.LinkedList;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
LinkedList<String> urls;
public String mPls="//.pls uri here";
/*StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); */
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUIElements();
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
fGetPlayableUrl(mPls);
try {
initializeMediaPlayer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
}
private LinkedList<String> fGetPlayableUrl( String mPls) {
GetStreamingUrl oGetStreamingUrl = new GetStreamingUrl(MainActivity.this);
urls = oGetStreamingUrl.getStreamingUrl(mPls);
return urls;
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.button1);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.button2);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
try {
initializeMediaPlayer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() throws IOException {
player = new MediaPlayer();
try {
//player=MediaPlayer.create(this,Uri.parse("http://198.27.80.37:5412/listen.pls?sid=2"));
player.setDataSource(urls.toString());
//player.prepareAsync();
//player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
GetStreamingUrl.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import android.content.Context;
import android.util.Log;
public class GetStreamingUrl {
private static String LOGTAG = "GetStreamingUrl";
private Context mContext;
public GetStreamingUrl(Context context) {
Log.i(LOGTAG, "call to constructor");
this.mContext = context;
}
public LinkedList<String> getStreamingUrl(String url) {
Log.i(LOGTAG, "get streaming url");
final BufferedReader br;
String murl = null;
LinkedList<String> murls = null;
try {
URLConnection mUrl = new URL(url).openConnection();
br = new BufferedReader(
new InputStreamReader(mUrl.getInputStream()));
murls = new LinkedList<String>();
while (true) {
try {
String line = br.readLine();
if (line == null) {
break;
}
murl = parseLine(line);
if (murl != null && !murl.equals("")) {
murls.add(murl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(LOGTAG, "url to stream :" + murl);
return murls;
}
private String parseLine(String line) {
if (line == null) {
return null;
}
String trimmed = line.trim();
if (trimmed.indexOf("http") >= 0) {
return trimmed.substring(trimmed.indexOf("http"));
}
return "";
}
}