public class MainActivity extends Activity implements Runnable,OnSeekBarChangeListener,OnClickListener{
private Button startMedia,pauseMedia;
private SeekBar seekBar;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
AudioControl();
}
private void AudioControl() {
// TODO Auto-generated method stub
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (Button) findViewById(R.id.button1);
pauseMedia = (Button) findViewById(R.id.button2);
startMedia.setOnClickListener(this);
pauseMedia.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
if(seekBar.getProgress() > 0) {
mp.start();
return;
}
try {
mp.setDataSource("http://videos.oneindia.in/temp/sitaram.mp3");
} 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.start();
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(pauseMedia) && mp!=null) {
mp.pause();
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if(fromUser) mp.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}
}
我想使用seekbar选项从url播放mp3。 以下例外即将来临......有人请帮助我
04-02 03:20:24.498: E/AndroidRuntime(1879): FATAL EXCEPTION: main
04-02 03:20:24.498: E/AndroidRuntime(1879): Process: com.example.mediaplayerdemo, PID: 1879
04-02 03:20:24.498: E/AndroidRuntime(1879): java.lang.NullPointerException
04-02 03:20:24.498: E/AndroidRuntime(1879): at com.example.mediaplayerdemo.MainActivity.onClick(MainActivity.java:52)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.view.View.performClick(View.java:4438)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.view.View$PerformClick.run(View.java:18422)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.os.Handler.handleCallback(Handler.java:733)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.os.Handler.dispatchMessage(Handler.java:95)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.os.Looper.loop(Looper.java:136)
04-02 03:20:24.498: E/AndroidRuntime(1879): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 03:20:24.498: E/AndroidRuntime(1879): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 03:20:24.498: E/AndroidRuntime(1879): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 03:20:24.498: E/AndroidRuntime(1879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 03:20:24.498: E/AndroidRuntime(1879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 03:20:24.498: E/AndroidRuntime(1879): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个..
您尚未初始化MediaPlayer
(mp)尝试初始化并尝试。
mp = new MediaPlayer();
http://typicaljava.blogspot.in/2013/03/streaming-media-player-with-song-url.html
http://android.programmerguru.com/android-mediaplayer-example-play-from-internet/
修改强>
if (v.equals(startMedia)) {
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.setDataSource(url);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}